← Index
NYTProf Performance Profile   « line view »
For ../prof.pl
  Run on Thu Dec 15 15:23:56 2022
Reported on Thu Dec 15 15:27:04 2022

Filename/Users/ether/.perlbrew/libs/36.0@std/lib/perl5/YAML/PP/Dumper.pm
StatementsExecuted 34 statements in 2.96ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
1113.99ms4.14msYAML::PP::Dumper::::BEGIN@9YAML::PP::Dumper::BEGIN@9
1111.26ms1.40msYAML::PP::Dumper::::BEGIN@10YAML::PP::Dumper::BEGIN@10
111781µs979µsYAML::PP::Dumper::::BEGIN@12YAML::PP::Dumper::BEGIN@12
111484µs531µsYAML::PP::Dumper::::BEGIN@11YAML::PP::Dumper::BEGIN@11
11124µs27µsYAML::PP::::BEGIN@1.293 YAML::PP::BEGIN@1.293
11122µs70µsYAML::PP::Dumper::::newYAML::PP::Dumper::new
1118µs8µsYAML::PP::Dumper::::BEGIN@8YAML::PP::Dumper::BEGIN@8
1116µs29µsYAML::PP::::BEGIN@2.294 YAML::PP::BEGIN@2.294
1115µs38µsYAML::PP::Dumper::::BEGIN@13YAML::PP::Dumper::BEGIN@13
1115µs27µsYAML::PP::Dumper::::BEGIN@7YAML::PP::Dumper::BEGIN@7
5513µs3µsYAML::PP::Dumper::::__ANON__YAML::PP::Dumper::__ANON__ (xsub)
0000s0sYAML::PP::Dumper::::_check_referencesYAML::PP::Dumper::_check_references
0000s0sYAML::PP::Dumper::::_dump_nodeYAML::PP::Dumper::_dump_node
0000s0sYAML::PP::Dumper::::_emit_nodeYAML::PP::Dumper::_emit_node
0000s0sYAML::PP::Dumper::::cloneYAML::PP::Dumper::clone
0000s0sYAML::PP::Dumper::::dumpYAML::PP::Dumper::dump
0000s0sYAML::PP::Dumper::::dump_fileYAML::PP::Dumper::dump_file
0000s0sYAML::PP::Dumper::::dump_stringYAML::PP::Dumper::dump_string
0000s0sYAML::PP::Dumper::::emitterYAML::PP::Dumper::emitter
0000s0sYAML::PP::Dumper::::footerYAML::PP::Dumper::footer
0000s0sYAML::PP::Dumper::::headerYAML::PP::Dumper::header
0000s0sYAML::PP::Dumper::::initYAML::PP::Dumper::init
0000s0sYAML::PP::Dumper::::representerYAML::PP::Dumper::representer
0000s0sYAML::PP::Dumper::::set_representerYAML::PP::Dumper::set_representer
0000s0sYAML::PP::Dumper::::version_directiveYAML::PP::Dumper::version_directive
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1226µs230µs
# spent 27µs (24+3) within YAML::PP::BEGIN@1.293 which was called: # once (24µs+3µs) by YAML::PP::BEGIN@11 at line 1
use strict;
# spent 27µs making 1 call to YAML::PP::BEGIN@1.293 # spent 3µs making 1 call to strict::import
2232µs252µs
# spent 29µs (6+23) within YAML::PP::BEGIN@2.294 which was called: # once (6µs+23µs) by YAML::PP::BEGIN@11 at line 2
use warnings;
# spent 29µs making 1 call to YAML::PP::BEGIN@2.294 # spent 23µs making 1 call to warnings::import
3package YAML::PP::Dumper;
4
510sour $VERSION = '0.035'; # VERSION
6
7215µs249µs
# spent 27µs (5+22) within YAML::PP::Dumper::BEGIN@7 which was called: # once (5µs+22µs) by YAML::PP::BEGIN@11 at line 7
use Scalar::Util qw/ blessed refaddr reftype /;
# spent 27µs making 1 call to YAML::PP::Dumper::BEGIN@7 # spent 22µs making 1 call to Exporter::import
8218µs28µs
# spent 8µs within YAML::PP::Dumper::BEGIN@8 which was called: # once (8µs+0s) by YAML::PP::BEGIN@11 at line 8
use YAML::PP;
# spent 8µs making 1 call to YAML::PP::Dumper::BEGIN@8 # spent 0s making 1 call to YAML::PP::Dumper::__ANON__
92397µs24.14ms
# spent 4.14ms (3.99+154µs) within YAML::PP::Dumper::BEGIN@9 which was called: # once (3.99ms+154µs) by YAML::PP::BEGIN@11 at line 9
use YAML::PP::Emitter;
# spent 4.14ms making 1 call to YAML::PP::Dumper::BEGIN@9 # spent 2µs making 1 call to YAML::PP::Dumper::__ANON__
102481µs21.40ms
# spent 1.40ms (1.26+141µs) within YAML::PP::Dumper::BEGIN@10 which was called: # once (1.26ms+141µs) by YAML::PP::BEGIN@11 at line 10
use YAML::PP::Representer;
# spent 1.40ms making 1 call to YAML::PP::Dumper::BEGIN@10 # spent 1µs making 1 call to YAML::PP::Dumper::__ANON__
112328µs2531µs
# spent 531µs (484+47) within YAML::PP::Dumper::BEGIN@11 which was called: # once (484µs+47µs) by YAML::PP::BEGIN@11 at line 11
use YAML::PP::Writer;
# spent 531µs making 1 call to YAML::PP::Dumper::BEGIN@11 # spent 0s making 1 call to YAML::PP::Dumper::__ANON__
122580µs2979µs
# spent 979µs (781+198) within YAML::PP::Dumper::BEGIN@12 which was called: # once (781µs+198µs) by YAML::PP::BEGIN@11 at line 12
use YAML::PP::Writer::File;
# spent 979µs making 1 call to YAML::PP::Dumper::BEGIN@12 # spent 0s making 1 call to YAML::PP::Dumper::__ANON__
1310s
# spent 38µs (5+33) within YAML::PP::Dumper::BEGIN@13 which was called: # once (5µs+33µs) by YAML::PP::BEGIN@11 at line 20
use YAML::PP::Common qw/
14 YAML_PLAIN_SCALAR_STYLE YAML_SINGLE_QUOTED_SCALAR_STYLE
15 YAML_DOUBLE_QUOTED_SCALAR_STYLE
16 YAML_ANY_SCALAR_STYLE
17 YAML_LITERAL_SCALAR_STYLE YAML_FOLDED_SCALAR_STYLE
18 YAML_FLOW_SEQUENCE_STYLE YAML_FLOW_MAPPING_STYLE
19 YAML_BLOCK_MAPPING_STYLE YAML_BLOCK_SEQUENCE_STYLE
2011.05ms271µs/;
# spent 38µs making 1 call to YAML::PP::Dumper::BEGIN@13 # spent 33µs making 1 call to Exporter::import
21
22
# spent 70µs (22+48) within YAML::PP::Dumper::new which was called: # once (22µs+48µs) by YAML::PP::new at line 73 of YAML/PP.pm
sub new {
2312µs my ($class, %args) = @_;
24
2511µs my $header = delete $args{header};
2610s $header = 1 unless defined $header;
2710s my $footer = delete $args{footer};
2811µs $footer = 0 unless defined $footer;
2910s my $version_directive = delete $args{version_directive};
3010s my $preserve = delete $args{preserve};
31
3210s my $schema = delete $args{schema} || YAML::PP->default_schema(
33 boolean => 'perl',
34 );
35
3610s my $emitter = delete $args{emitter} || YAML::PP::Emitter->new;
3718µs244µs unless (blessed($emitter)) {
# spent 42µs making 1 call to YAML::PP::Emitter::new # spent 2µs making 1 call to Scalar::Util::blessed
38 $emitter = YAML::PP::Emitter->new(
39 %$emitter
40 );
41 }
42
4310s if (keys %args) {
44 die "Unexpected arguments: " . join ', ', sort keys %args;
45 }
4615µs14µs my $self = bless {
# spent 4µs making 1 call to YAML::PP::Representer::new
47 representer => YAML::PP::Representer->new(
48 schema => $schema,
49 preserve => $preserve,
50 ),
51 version_directive => $version_directive,
52 emitter => $emitter,
53 seen => {},
54 anchors => {},
55 anchor_num => 0,
56 header => $header,
57 footer => $footer,
58 }, $class;
5913µs return $self;
60}
61
62sub clone {
63 my ($self) = @_;
64 my $clone = {
65 representer => $self->representer->clone,
66 emitter => $self->emitter->clone,
67 version_directive => $self->version_directive,
68 seen => {},
69 anchors => {},
70 anchor_num => 0,
71 header => $self->header,
72 footer => $self->footer,
73 };
74 return bless $clone, ref $self;
75}
76
77sub init {
78 my ($self) = @_;
79 $self->{seen} = {};
80 $self->{anchors} = {};
81 $self->{anchor_num} = 0;
82}
83
84sub emitter { return $_[0]->{emitter} }
85sub representer { return $_[0]->{representer} }
86sub set_representer { $_[0]->{representer} = $_[1] }
87sub header { return $_[0]->{header} }
88sub footer { return $_[0]->{footer} }
89sub version_directive { return $_[0]->{version_directive} }
90
91sub dump {
92 my ($self, @docs) = @_;
93 $self->emitter->init;
94
95 $self->emitter->stream_start_event({});
96
97 for my $i (0 .. $#docs) {
98 my $header_implicit = ($i == 0 and not $self->header);
99 my %args = (
100 implicit => $header_implicit,
101 );
102 if ($self->version_directive) {
103 my ($major, $minor) = split m/\./, $self->representer->schema->yaml_version;
104 $args{version_directive} = { major => $major, minor => $minor };
105 }
106 $self->emitter->document_start_event( \%args );
107 $self->init;
108 $self->_check_references($docs[ $i ]);
109 $self->_dump_node($docs[ $i ]);
110 my $footer_implicit = (not $self->footer);
111 $self->emitter->document_end_event({ implicit => $footer_implicit });
112 }
113
114 $self->emitter->stream_end_event({});
115
116 my $output = $self->emitter->writer->output;
117 $self->emitter->finish;
118 return $output;
119}
120
121sub _dump_node {
122 my ($self, $value) = @_;
123 my $node = {
124 value => $value,
125 };
126 if (ref $value) {
127
128 my $seen = $self->{seen};
129 my $refaddr = refaddr $value;
130 if ($seen->{ $refaddr } and $seen->{ $refaddr } > 1) {
131 my $anchor = $self->{anchors}->{ $refaddr };
132 unless (defined $anchor) {
133 if ($self->representer->preserve_alias) {
134 if (ref $node->{value} eq 'YAML::PP::Preserve::Scalar') {
135 if (defined $node->{value}->alias) {
136 $node->{anchor} = $node->{value}->alias;
137 $self->{anchors}->{ $refaddr } = $node->{value}->alias;
138 }
139 }
140 elsif (reftype $node->{value} eq 'HASH') {
141 if (my $tied = tied %{ $node->{value} } ) {
142 if (defined $tied->{alias}) {
143 $node->{anchor} = $tied->{alias};
144 $self->{anchors}->{ $refaddr } = $node->{anchor};
145 }
146 }
147 }
148 elsif (reftype $node->{value} eq 'ARRAY') {
149 if (my $tied = tied @{ $node->{value} } ) {
150 if (defined $tied->{alias}) {
151 $node->{anchor} = $tied->{alias};
152 $self->{anchors}->{ $refaddr } = $node->{anchor};
153 }
154 }
155 }
156 }
157 unless (defined $node->{anchor}) {
158 my $num = ++$self->{anchor_num};
159 $self->{anchors}->{ $refaddr } = $num;
160 $node->{anchor} = $num;
161 }
162 }
163 else {
164 $node->{value} = $anchor;
165 $self->_emit_node([ alias => $node ]);
166 return;
167 }
168
169 }
170 }
171 $node = $self->representer->represent_node($node);
172 $self->_emit_node($node);
173}
174
175sub _emit_node {
176 my ($self, $item) = @_;
177 my ($type, $node, %args) = @$item;
178 if ($type eq 'alias') {
179 $self->emitter->alias_event({ value => $node->{value} });
180 return;
181 }
182 if ($type eq 'mapping') {
183 my $style = $args{style} || YAML_BLOCK_MAPPING_STYLE;
184 # TODO
185 if ($node->{items} and @{ $node->{items} } == 0) {
186# $style = YAML_FLOW_MAPPING_STYLE;
187 }
188 $self->emitter->mapping_start_event({
189 anchor => $node->{anchor},
190 style => $style,
191 tag => $node->{tag},
192 });
193 for (@{ $node->{items} }) {
194 $self->_dump_node($_);
195 }
196 $self->emitter->mapping_end_event;
197 return;
198 }
199 if ($type eq 'sequence') {
200 my $style = $args{style} || YAML_BLOCK_SEQUENCE_STYLE;
201 if (@{ $node->{items} } == 0) {
202# $style = YAML_FLOW_SEQUENCE_STYLE;
203 }
204 $self->emitter->sequence_start_event({
205 anchor => $node->{anchor},
206 style => $style,
207 tag => $node->{tag},
208 });
209 for (@{ $node->{items} }) {
210 $self->_dump_node($_);
211 }
212 $self->emitter->sequence_end_event;
213 return;
214 }
215 $self->emitter->scalar_event({
216 value => $node->{items}->[0],
217 style => $node->{style},
218 anchor => $node->{anchor},
219 tag => $node->{tag},
220 });
221}
222
223
224sub dump_string {
225 my ($self, @docs) = @_;
226 my $writer = YAML::PP::Writer->new;
227 $self->emitter->set_writer($writer);
228 my $output = $self->dump(@docs);
229 return $output;
230}
231
232sub dump_file {
233 my ($self, $file, @docs) = @_;
234 my $writer = YAML::PP::Writer::File->new(output => $file);
235 $self->emitter->set_writer($writer);
236 my $output = $self->dump(@docs);
237 return $output;
238}
239
24014µsmy %_reftypes = (
241 HASH => 1,
242 ARRAY => 1,
243 Regexp => 1,
244 REGEXP => 1,
245 CODE => 1,
246 SCALAR => 1,
247 REF => 1,
248 GLOB => 1,
249);
250
251sub _check_references {
252 my ($self, $doc) = @_;
253 my $reftype = reftype $doc or return;
254 my $seen = $self->{seen};
255 # check which references are used more than once
256 if ($reftype eq 'SCALAR' and
257 grep { ref $doc eq $_ } @{ $self->representer->schema->bool_class || [] }) {
258 # JSON::PP and boolean.pm always return the same reference for booleans
259 # Avoid printing *aliases in those case
260 if (ref $doc eq 'boolean' or ref $doc eq 'JSON::PP::Boolean') {
261 return;
262 }
263 }
264 if (++$seen->{ refaddr $doc } > 1) {
265 # seen already
266 return;
267 }
268 unless ($_reftypes{ $reftype }) {
269 die sprintf "Reference %s not implemented",
270 $reftype;
271 }
272 if ($reftype eq 'HASH') {
273 $self->_check_references($doc->{ $_ }) for keys %$doc;
274 }
275 elsif ($reftype eq 'ARRAY') {
276 $self->_check_references($_) for @$doc;
277 }
278 elsif ($reftype eq 'REF') {
279 $self->_check_references($$doc);
280 }
281}
282
28315µs1;
 
# spent 3µs within YAML::PP::Dumper::__ANON__ which was called 5 times, avg 600ns/call: # once (2µs+0s) by YAML::PP::Dumper::BEGIN@9 at line 9 # once (1µs+0s) by YAML::PP::Dumper::BEGIN@10 at line 10 # once (0s+0s) by YAML::PP::Dumper::BEGIN@8 at line 8 # once (0s+0s) by YAML::PP::Dumper::BEGIN@12 at line 12 # once (0s+0s) by YAML::PP::Dumper::BEGIN@11 at line 11
sub YAML::PP::Dumper::__ANON__; # xsub