← Index
NYTProf Performance Profile   « line view »
For examples/Atom-timer.pl
  Run on Mon Aug 12 14:45:28 2013
Reported on Mon Aug 12 14:46:14 2013

Filename/Users/dde/perl5/perlbrew/perls/5.18.0t/lib/site_perl/5.18.0/Try/Tiny.pm
StatementsExecuted 16473 statements in 11.2ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
65013108.61ms402msTry::Tiny::::try Try::Tiny::try (recurses: max depth 4, inclusive time 315ms)
6211181.39ms1.39msTry::Tiny::::catch Try::Tiny::catch
111826µs1.04msTry::Tiny::ScopeGuard::::BEGIN@144Try::Tiny::ScopeGuard::BEGIN@144
11131µs604µsTry::Tiny::::BEGIN@20 Try::Tiny::BEGIN@20
1118µs8µsTry::Tiny::::BEGIN@8 Try::Tiny::BEGIN@8
1117µs7µsTry::Tiny::::BEGIN@2 Try::Tiny::BEGIN@2
1116µs34µsTry::Tiny::::BEGIN@17 Try::Tiny::BEGIN@17
1116µs9µsTry::Tiny::::BEGIN@12 Try::Tiny::BEGIN@12
1115µs53µsTry::Tiny::::BEGIN@14 Try::Tiny::BEGIN@14
1115µs16µsTry::Tiny::::BEGIN@11 Try::Tiny::BEGIN@11
0000s0sTry::Tiny::ScopeGuard::::DESTROYTry::Tiny::ScopeGuard::DESTROY
0000s0sTry::Tiny::ScopeGuard::::_newTry::Tiny::ScopeGuard::_new
0000s0sTry::Tiny::::__ANON__[:20] Try::Tiny::__ANON__[:20]
0000s0sTry::Tiny::::finally Try::Tiny::finally
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Try::Tiny;
2
# spent 7µs within Try::Tiny::BEGIN@2 which was called: # once (7µs+0s) by Module::Implementation::BEGIN@13 at line 4
BEGIN {
314µs $Try::Tiny::AUTHORITY = 'cpan:NUFFIN';
4124µs17µs}
# spent 7µs making 1 call to Try::Tiny::BEGIN@2
5{
62800ns $Try::Tiny::VERSION = '0.16';
7}
8235µs18µs
# spent 8µs within Try::Tiny::BEGIN@8 which was called: # once (8µs+0s) by Module::Implementation::BEGIN@13 at line 8
use 5.006;
# spent 8µs making 1 call to Try::Tiny::BEGIN@8
9# ABSTRACT: minimal try/catch with proper preservation of $@
10
11218µs227µs
# spent 16µs (5+11) within Try::Tiny::BEGIN@11 which was called: # once (5µs+11µs) by Module::Implementation::BEGIN@13 at line 11
use strict;
# spent 16µs making 1 call to Try::Tiny::BEGIN@11 # spent 11µs making 1 call to strict::import
12270µs212µs
# spent 9µs (6+3) within Try::Tiny::BEGIN@12 which was called: # once (6µs+3µs) by Module::Implementation::BEGIN@13 at line 12
use warnings;
# spent 9µs making 1 call to Try::Tiny::BEGIN@12 # spent 3µs making 1 call to warnings::import
13
14234µs2101µs
# spent 53µs (5+48) within Try::Tiny::BEGIN@14 which was called: # once (5µs+48µs) by Module::Implementation::BEGIN@13 at line 14
use base 'Exporter';
# spent 53µs making 1 call to Try::Tiny::BEGIN@14 # spent 48µs making 1 call to base::import
1511µsour @EXPORT = our @EXPORT_OK = qw(try catch finally);
16
17240µs262µs
# spent 34µs (6+28) within Try::Tiny::BEGIN@17 which was called: # once (6µs+28µs) by Module::Implementation::BEGIN@13 at line 17
use Carp;
# spent 34µs making 1 call to Try::Tiny::BEGIN@17 # spent 28µs making 1 call to Exporter::import
181500ns$Carp::Internal{+__PACKAGE__}++;
19
201373µs1604µs
# spent 604µs (31+573) within Try::Tiny::BEGIN@20 which was called: # once (31µs+573µs) by Module::Implementation::BEGIN@13 at line 20
BEGIN { eval "use Sub::Name; 1" or *{subname} = sub {1} }
# spent 604µs making 1 call to Try::Tiny::BEGIN@20
# spent 94µs executing statements in string eval
# includes 243µs spent executing 1 call to 1 sub defined therein.
21
22# Need to prototype as @ not $$ because of the way Perl evaluates the prototype.
23# Keeping it at $$ means you only ever get 1 sub because we need to eval in a list
24# context & not a scalar one
25
26
# spent 402ms (8.61+393) within Try::Tiny::try which was called 650 times, avg 618µs/call: # 223 times (2.74ms+29.9ms) by Class::MOP::Attribute::_process_accessors at line 401 of Class/MOP/Attribute.pm, avg 146µs/call # 151 times (1.99ms+282ms) by Class::MOP::Class::_post_add_attribute at line 896 of Class/MOP/Class.pm, avg 1.88ms/call # 61 times (816µs+-816µs) by Moose::Meta::Method::Accessor::_compile_code at line 47 of Moose/Meta/Method/Accessor.pm, avg 0s/call # 58 times (828µs+-828µs) by Class::MOP::Method::Accessor::_generate_reader_method_inline at line 157 of Class/MOP/Method/Accessor.pm, avg 0s/call # 44 times (746µs+-746µs) by Class::MOP::Method::Accessor::_generate_accessor_method_inline at line 123 of Class/MOP/Method/Accessor.pm, avg 0s/call # 31 times (417µs+22.7ms) by Class::MOP::Method::Constructor::_generate_constructor_method_inline at line 115 of Class/MOP/Method/Constructor.pm, avg 747µs/call # 25 times (259µs+-184µs) by Moose::Util::does_role at line 54 of Moose/Util.pm, avg 3µs/call # 24 times (339µs+-339µs) by Class::MOP::Method::Accessor::_generate_predicate_method_inline at line 212 of Class/MOP/Method/Accessor.pm, avg 0s/call # 22 times (332µs+58.0ms) by Class::Load::try_load_class at line 183 of Class/Load.pm, avg 2.65ms/call # 4 times (43µs+292µs) by Moose::Meta::Attribute::does at line 45 of Moose/Meta/Attribute.pm, avg 84µs/call # 3 times (53µs+2.72ms) by Module::Implementation::_load_implementation at line 93 of Module/Implementation.pm, avg 925µs/call # 3 times (39µs+-39µs) by Class::MOP::Method::Accessor::_generate_writer_method_inline at line 187 of Class/MOP/Method/Accessor.pm, avg 0s/call # once (15µs+152µs) by Moose::Meta::Method::Destructor::_initialize_body at line 106 of Moose/Meta/Method/Destructor.pm
sub try (&;@) {
27650200µs my ( $try, @code_refs ) = @_;
28
29 # we need to save this here, the eval block will be in scalar context due
30 # to $failed
3165089µs my $wantarray = wantarray;
32
336507µs my ( $catch, @finally );
34
35 # find labeled blocks in the argument list.
36 # catch and finally tag the blocks by blessing a scalar reference to them.
37650233µs foreach my $code_ref (@code_refs) {
38
39621513µs if ( ref($code_ref) eq 'Try::Tiny::Catch' ) {
406212µs croak 'A try() may not be followed by multiple catch() blocks'
41 if $catch;
42621127µs $catch = ${$code_ref};
43 } elsif ( ref($code_ref) eq 'Try::Tiny::Finally' ) {
44 push @finally, ${$code_ref};
45 } else {
46 croak(
47 'try() encountered an unexpected argument ('
48 . ( defined $code_ref ? $code_ref : 'undef' )
49 . ') - perhaps a missing semi-colon before or'
50 );
51 }
52 }
53
54 # FIXME consider using local $SIG{__DIE__} to accumulate all errors. It's
55 # not perfect, but we could provide a list of additional errors for
56 # $catch->();
57
58 # name the blocks if we have Sub::Name installed
59650197µs my $caller = caller;
606502.27ms6501.29ms subname("${caller}::try {...} " => $try);
# spent 1.29ms making 650 calls to Sub::Name::subname, avg 2µs/call
616501.67ms621878µs subname("${caller}::catch {...} " => $catch) if $catch;
# spent 878µs making 621 calls to Sub::Name::subname, avg 1µs/call
62650206µs subname("${caller}::finally {...} " => $_) foreach @finally;
63
64 # save the value of $@ so we can set $@ back to it in the beginning of the eval
65 # and restore $@ after the eval finishes
6665071µs my $prev_error = $@;
67
686502µs my ( @ret, $error );
69
70 # failed will be true if the eval dies, because 1 will not be returned
71 # from the eval body
72650234µs my $failed = not eval {
7365044µs $@ = $prev_error;
74
75 # evaluate the try block in the correct context
76650712µs273319ms if ( $wantarray ) {
# spent 218ms making 22 calls to Class::Load::try {...} , avg 9.90ms/call # spent 54.6ms making 129 calls to Class::MOP::Method::Accessor::try {...} , avg 423µs/call # spent 23.1ms making 61 calls to Moose::Meta::Method::Accessor::try {...} , avg 379µs/call # spent 22.6ms making 31 calls to Class::MOP::Method::Constructor::try {...} , avg 729µs/call # spent 284µs making 4 calls to Moose::Meta::Attribute::try {...} , avg 71µs/call # spent 148µs making 1 call to Moose::Meta::Method::Destructor::try {...} # spent 89µs making 25 calls to Moose::Util::try {...} , avg 4µs/call
77 @ret = $try->();
78 } elsif ( defined $wantarray ) {
79 $ret[0] = $try->();
80 } else {
81377259µs377387ms $try->();
# spent 291ms making 151 calls to Class::MOP::Class::try {...} , avg 1.93ms/call # spent 93.6ms making 223 calls to Class::MOP::Attribute::try {...} , avg 420µs/call # spent 2.70ms making 3 calls to Module::Implementation::try {...} , avg 901µs/call
82 };
83
84649169µs return 1; # properly set $fail to false
85 };
86
87 # preserve the current error and reset the original value of $@
8865075µs $error = $@;
8965051µs $@ = $prev_error;
90
91 # set up a scope guard to invoke the finally block at the end
92 my @guards =
93650220µs map { Try::Tiny::ScopeGuard->_new($_, $failed ? $error : ()) }
94 @finally;
95
96 # at this point $failed contains a true value if the eval died, even if some
97 # destructor overwrote $@ as the eval was unwinding.
9865066µs if ( $failed ) {
99 # if we got an error, invoke the catch block.
1001600ns if ( $catch ) {
101 # This works like given($error), but is backwards compatible and
102 # sets $_ in the dynamic scope for the body of C<$catch>
103 for ($error) {
10413µs116µs return $catch->($error);
# spent 16µs making 1 call to Class::Load::catch {...}
105 }
106
107 # in case when() was used without an explicit return, the C<for>
108 # loop will be aborted and there's no useful return value
109 }
110
111 return;
112 } else {
113 # no failure, $@ is back to what it was, everything is fine
1146491.27ms return $wantarray ? @ret : $ret[0];
115 }
116}
117
118
# spent 1.39ms within Try::Tiny::catch which was called 621 times, avg 2µs/call: # 223 times (484µs+0s) by Class::MOP::Attribute::_process_accessors at line 401 of Class/MOP/Attribute.pm, avg 2µs/call # 151 times (348µs+0s) by Class::MOP::Class::_post_add_attribute at line 896 of Class/MOP/Class.pm, avg 2µs/call # 61 times (141µs+0s) by Moose::Meta::Method::Accessor::_compile_code at line 47 of Moose/Meta/Method/Accessor.pm, avg 2µs/call # 58 times (119µs+0s) by Class::MOP::Method::Accessor::_generate_reader_method_inline at line 157 of Class/MOP/Method/Accessor.pm, avg 2µs/call # 44 times (92µs+0s) by Class::MOP::Method::Accessor::_generate_accessor_method_inline at line 123 of Class/MOP/Method/Accessor.pm, avg 2µs/call # 31 times (75µs+0s) by Class::MOP::Method::Constructor::_generate_constructor_method_inline at line 115 of Class/MOP/Method/Constructor.pm, avg 2µs/call # 24 times (49µs+0s) by Class::MOP::Method::Accessor::_generate_predicate_method_inline at line 212 of Class/MOP/Method/Accessor.pm, avg 2µs/call # 22 times (64µs+0s) by Class::Load::try_load_class at line 183 of Class/Load.pm, avg 3µs/call # 3 times (10µs+0s) by Module::Implementation::_load_implementation at line 93 of Module/Implementation.pm, avg 3µs/call # 3 times (6µs+0s) by Class::MOP::Method::Accessor::_generate_writer_method_inline at line 187 of Class/MOP/Method/Accessor.pm, avg 2µs/call # once (2µs+0s) by Moose::Meta::Method::Destructor::_initialize_body at line 106 of Moose/Meta/Method/Destructor.pm
sub catch (&;@) {
119621174µs my ( $block, @rest ) = @_;
120
12162160µs croak 'Useless bare catch()' unless wantarray;
122
123 return (
1246211.48ms bless(\$block, 'Try::Tiny::Catch'),
125 @rest,
126 );
127}
128
129sub finally (&;@) {
130 my ( $block, @rest ) = @_;
131
132 croak 'Useless bare finally()' unless wantarray;
133
134 return (
135 bless(\$block, 'Try::Tiny::Finally'),
136 @rest,
137 );
138}
139
140{
1411100ns package # hide from PAUSE
142 Try::Tiny::ScopeGuard;
143
1442182µs21.08ms
# spent 1.04ms (826µs+217µs) within Try::Tiny::ScopeGuard::BEGIN@144 which was called: # once (826µs+217µs) by Module::Implementation::BEGIN@13 at line 144
use constant UNSTABLE_DOLLARAT => ($] < '5.013002') ? 1 : 0;
# spent 1.04ms making 1 call to Try::Tiny::ScopeGuard::BEGIN@144 # spent 34µs making 1 call to constant::import
145
146 sub _new {
147 shift;
148 bless [ @_ ];
149 }
150
151 sub DESTROY {
152 my ($code, @args) = @{ $_[0] };
153
154 local $@ if UNSTABLE_DOLLARAT;
155 eval {
156 $code->(@args);
157 1;
158 } or do {
159 warn
160 "Execution of finally() block $code resulted in an exception, which "
161 . '*CAN NOT BE PROPAGATED* due to fundamental limitations of Perl. '
162 . 'Your program will continue as if this event never took place. '
163 . "Original exception text follows:\n\n"
164 . (defined $@ ? $@ : '$@ left undefined...')
165 . "\n"
166 ;
167 }
168 }
169}
170
171__PACKAGE__
172
17314µs__END__