Filename | /usr/share/perl/5.18/constant.pm |
Statements | Executed 491 statements in 1.75ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
16 | 16 | 8 | 496µs | 625µs | import | constant::
24 | 1 | 1 | 50µs | 50µs | CORE:match (opcode) | constant::
24 | 1 | 1 | 33µs | 33µs | CORE:regcomp (opcode) | constant::
1 | 1 | 1 | 18µs | 18µs | BEGIN@2 | constant::
1 | 1 | 1 | 15µs | 15µs | BEGIN@24 | constant::
1 | 1 | 1 | 11µs | 82µs | BEGIN@4 | constant::
1 | 1 | 1 | 10µs | 22µs | BEGIN@116 | constant::
1 | 1 | 1 | 8µs | 21µs | BEGIN@28 | constant::
1 | 1 | 1 | 8µs | 19µs | BEGIN@54 | constant::
1 | 1 | 1 | 7µs | 40µs | BEGIN@6 | constant::
1 | 1 | 1 | 7µs | 21µs | BEGIN@3 | constant::
3 | 3 | 1 | 4µs | 4µs | CORE:qr (opcode) | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:140] | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:144] | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:146] | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:30] | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:33] | constant::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package constant; | ||||
2 | 2 | 54µs | 1 | 18µs | # spent 18µs within constant::BEGIN@2 which was called:
# once (18µs+0s) by Data::Dumper::BEGIN@273 at line 2 # spent 18µs making 1 call to constant::BEGIN@2 |
3 | 2 | 27µs | 2 | 35µs | # spent 21µs (7+14) within constant::BEGIN@3 which was called:
# once (7µs+14µs) by Data::Dumper::BEGIN@273 at line 3 # spent 21µs making 1 call to constant::BEGIN@3
# spent 14µs making 1 call to strict::import |
4 | 2 | 32µs | 2 | 154µs | # spent 82µs (11+72) within constant::BEGIN@4 which was called:
# once (11µs+72µs) by Data::Dumper::BEGIN@273 at line 4 # spent 82µs making 1 call to constant::BEGIN@4
# spent 72µs making 1 call to warnings::register::import |
5 | |||||
6 | 2 | 158µs | 2 | 72µs | # spent 40µs (7+32) within constant::BEGIN@6 which was called:
# once (7µs+32µs) by Data::Dumper::BEGIN@273 at line 6 # spent 40µs making 1 call to constant::BEGIN@6
# spent 32µs making 1 call to vars::import |
7 | 1 | 700ns | $VERSION = '1.27'; | ||
8 | |||||
9 | #======================================================================= | ||||
10 | |||||
11 | # Some names are evil choices. | ||||
12 | 1 | 6µs | my %keywords = map +($_, 1), qw{ BEGIN INIT CHECK END DESTROY AUTOLOAD }; | ||
13 | 1 | 1µs | $keywords{UNITCHECK}++ if $] > 5.009; | ||
14 | |||||
15 | 1 | 5µs | my %forced_into_main = map +($_, 1), | ||
16 | qw{ STDIN STDOUT STDERR ARGV ARGVOUT ENV INC SIG }; | ||||
17 | |||||
18 | 1 | 5µs | my %forbidden = (%keywords, %forced_into_main); | ||
19 | |||||
20 | 1 | 9µs | 1 | 2µs | my $normal_constant_name = qr/^_?[^\W_0-9]\w*\z/; # spent 2µs making 1 call to constant::CORE:qr |
21 | 1 | 3µs | 1 | 900ns | my $tolerable = qr/^[A-Za-z_]\w*\z/; # spent 900ns making 1 call to constant::CORE:qr |
22 | 1 | 3µs | 1 | 600ns | my $boolean = qr/^[01]?\z/; # spent 600ns making 1 call to constant::CORE:qr |
23 | |||||
24 | # spent 15µs within constant::BEGIN@24 which was called:
# once (15µs+0s) by Data::Dumper::BEGIN@273 at line 34 | ||||
25 | # We'd like to do use constant _CAN_PCS => $] > 5.009002 | ||||
26 | # but that's a bit tricky before we load the constant module :-) | ||||
27 | # By doing this, we save 1 run time check for *every* call to import. | ||||
28 | 2 | 87µs | 2 | 33µs | # spent 21µs (8+12) within constant::BEGIN@28 which was called:
# once (8µs+12µs) by Data::Dumper::BEGIN@273 at line 28 # spent 21µs making 1 call to constant::BEGIN@28
# spent 12µs making 1 call to strict::unimport |
29 | 1 | 1µs | my $const = $] > 5.009002; | ||
30 | 1 | 7µs | *_CAN_PCS = sub () {$const}; | ||
31 | |||||
32 | 1 | 500ns | my $downgrade = $] < 5.015004; # && $] >= 5.008 | ||
33 | 1 | 8µs | *_DOWNGRADE = sub () { $downgrade }; | ||
34 | 1 | 80µs | 1 | 15µs | } # spent 15µs making 1 call to constant::BEGIN@24 |
35 | |||||
36 | #======================================================================= | ||||
37 | # import() - import symbols into user's namespace | ||||
38 | # | ||||
39 | # What we actually do is define a function in the caller's namespace | ||||
40 | # which returns the value. The function we create will normally | ||||
41 | # be inlined as a constant, thereby avoiding further sub calling | ||||
42 | # overhead. | ||||
43 | #======================================================================= | ||||
44 | # spent 625µs (496+129) within constant::import which was called 16 times, avg 39µs/call:
# once (90µs+27µs) by Log::Any::Adapter::Util::BEGIN@28 at line 28 of /opt/flows/lib/lib/perl5/Log/Any/Adapter/Util.pm
# once (49µs+11µs) by Encode::BEGIN@8 at line 8 of Encode.pm
# once (34µs+6µs) by Moo::_Utils::BEGIN@9 at line 9 of Moo/_Utils.pm
# once (31µs+7µs) by Data::Dumper::BEGIN@273 at line 273 of Data/Dumper.pm
# once (29µs+8µs) by URI::_punycode::BEGIN@14 at line 14 of URI/_punycode.pm
# once (30µs+6µs) by URI::_punycode::BEGIN@20 at line 20 of URI/_punycode.pm
# once (27µs+8µs) by Try::Tiny::ScopeGuard::BEGIN@144 at line 144 of Try/Tiny.pm
# once (26µs+7µs) by namespace::clean::_Util::BEGIN@18 at line 18 of /opt/flows/lib/lib/perl5/namespace/clean/_Util.pm
# once (26µs+7µs) by Encode::Alias::BEGIN@6 at line 6 of Encode/Alias.pm
# once (25µs+8µs) by Moo::_Utils::BEGIN@8 at line 8 of Moo/_Utils.pm
# once (24µs+7µs) by namespace::clean::_Util::BEGIN@19 at line 19 of /opt/flows/lib/lib/perl5/namespace/clean/_Util.pm
# once (23µs+6µs) by URI::_punycode::BEGIN@15 at line 15 of URI/_punycode.pm
# once (22µs+5µs) by URI::_punycode::BEGIN@16 at line 16 of URI/_punycode.pm
# once (21µs+6µs) by URI::_punycode::BEGIN@18 at line 18 of URI/_punycode.pm
# once (21µs+6µs) by URI::_punycode::BEGIN@17 at line 17 of URI/_punycode.pm
# once (20µs+6µs) by URI::_punycode::BEGIN@19 at line 19 of URI/_punycode.pm | ||||
45 | 16 | 8µs | my $class = shift; | ||
46 | 16 | 6µs | return unless @_; # Ignore 'use constant;' | ||
47 | 16 | 2µs | my $constants; | ||
48 | 16 | 7µs | my $multiple = ref $_[0]; | ||
49 | 16 | 9µs | my $pkg = caller; | ||
50 | 16 | 1µs | my $flush_mro; | ||
51 | 16 | 1µs | my $symtab; | ||
52 | |||||
53 | 16 | 6µs | if (_CAN_PCS) { | ||
54 | 2 | 306µs | 2 | 31µs | # spent 19µs (8+12) within constant::BEGIN@54 which was called:
# once (8µs+12µs) by Data::Dumper::BEGIN@273 at line 54 # spent 19µs making 1 call to constant::BEGIN@54
# spent 12µs making 1 call to strict::unimport |
55 | 16 | 25µs | $symtab = \%{$pkg . '::'}; | ||
56 | }; | ||||
57 | |||||
58 | 16 | 5µs | if ( $multiple ) { | ||
59 | 1 | 1µs | if (ref $_[0] ne 'HASH') { | ||
60 | require Carp; | ||||
61 | Carp::croak("Invalid reference type '".ref(shift)."' not 'HASH'"); | ||||
62 | } | ||||
63 | 1 | 400ns | $constants = shift; | ||
64 | } else { | ||||
65 | 15 | 4µs | unless (defined $_[0]) { | ||
66 | require Carp; | ||||
67 | Carp::croak("Can't use undef as constant name"); | ||||
68 | } | ||||
69 | 15 | 32µs | $constants->{+shift} = undef; | ||
70 | } | ||||
71 | |||||
72 | 16 | 31µs | foreach my $name ( keys %$constants ) { | ||
73 | # Normal constant name | ||||
74 | 24 | 194µs | 48 | 84µs | if ($name =~ $normal_constant_name and !$forbidden{$name}) { # spent 50µs making 24 calls to constant::CORE:match, avg 2µs/call
# spent 33µs making 24 calls to constant::CORE:regcomp, avg 1µs/call |
75 | # Everything is okay | ||||
76 | |||||
77 | # Name forced into main, but we're not in main. Fatal. | ||||
78 | } elsif ($forced_into_main{$name} and $pkg ne 'main') { | ||||
79 | require Carp; | ||||
80 | Carp::croak("Constant name '$name' is forced into main::"); | ||||
81 | |||||
82 | # Starts with double underscore. Fatal. | ||||
83 | } elsif ($name =~ /^__/) { | ||||
84 | require Carp; | ||||
85 | Carp::croak("Constant name '$name' begins with '__'"); | ||||
86 | |||||
87 | # Maybe the name is tolerable | ||||
88 | } elsif ($name =~ $tolerable) { | ||||
89 | # Then we'll warn only if you've asked for warnings | ||||
90 | if (warnings::enabled()) { | ||||
91 | if ($keywords{$name}) { | ||||
92 | warnings::warn("Constant name '$name' is a Perl keyword"); | ||||
93 | } elsif ($forced_into_main{$name}) { | ||||
94 | warnings::warn("Constant name '$name' is " . | ||||
95 | "forced into package main::"); | ||||
96 | } | ||||
97 | } | ||||
98 | |||||
99 | # Looks like a boolean | ||||
100 | # use constant FRED == fred; | ||||
101 | } elsif ($name =~ $boolean) { | ||||
102 | require Carp; | ||||
103 | if (@_) { | ||||
104 | Carp::croak("Constant name '$name' is invalid"); | ||||
105 | } else { | ||||
106 | Carp::croak("Constant name looks like boolean value"); | ||||
107 | } | ||||
108 | |||||
109 | } else { | ||||
110 | # Must have bad characters | ||||
111 | require Carp; | ||||
112 | Carp::croak("Constant name '$name' has invalid characters"); | ||||
113 | } | ||||
114 | |||||
115 | { | ||||
116 | 26 | 313µs | 2 | 34µs | # spent 22µs (10+12) within constant::BEGIN@116 which was called:
# once (10µs+12µs) by Data::Dumper::BEGIN@273 at line 116 # spent 22µs making 1 call to constant::BEGIN@116
# spent 12µs making 1 call to strict::unimport |
117 | 24 | 19µs | my $full_name = "${pkg}::$name"; | ||
118 | 24 | 30µs | $declared{$full_name}++; | ||
119 | 24 | 14µs | if ($multiple || @_ == 1) { | ||
120 | 24 | 9µs | my $scalar = $multiple ? $constants->{$name} : $_[0]; | ||
121 | |||||
122 | if (_DOWNGRADE) { # for 5.8 to 5.14 | ||||
123 | # Work around perl bug #31991: Sub names (actually glob | ||||
124 | # names in general) ignore the UTF8 flag. So we have to | ||||
125 | # turn it off to get the "right" symbol table entry. | ||||
126 | utf8::is_utf8 $name and utf8::encode $name; | ||||
127 | } | ||||
128 | |||||
129 | # The constant serves to optimise this entire block out on | ||||
130 | # 5.8 and earlier. | ||||
131 | 24 | 17µs | if (_CAN_PCS && $symtab && !exists $symtab->{$name}) { | ||
132 | # No typeglob yet, so we can use a reference as space- | ||||
133 | # efficient proxy for a constant subroutine | ||||
134 | # The check in Perl_ck_rvconst knows that inlinable | ||||
135 | # constants from cv_const_sv are read only. So we have to: | ||||
136 | 24 | 72µs | 24 | 22µs | Internals::SvREADONLY($scalar, 1); # spent 22µs making 24 calls to Internals::SvREADONLY, avg 908ns/call |
137 | 24 | 15µs | $symtab->{$name} = \$scalar; | ||
138 | 24 | 8µs | ++$flush_mro; | ||
139 | } else { | ||||
140 | *$full_name = sub () { $scalar }; | ||||
141 | } | ||||
142 | } elsif (@_) { | ||||
143 | my @list = @_; | ||||
144 | *$full_name = sub () { @list }; | ||||
145 | } else { | ||||
146 | *$full_name = sub () { }; | ||||
147 | } | ||||
148 | } | ||||
149 | } | ||||
150 | # Flush the cache exactly once if we make any direct symbol table changes. | ||||
151 | 16 | 125µs | 16 | 23µs | mro::method_changed_in($pkg) if _CAN_PCS && $flush_mro; # spent 23µs making 16 calls to mro::method_changed_in, avg 1µs/call |
152 | } | ||||
153 | |||||
154 | 1 | 8µs | 1; | ||
155 | |||||
156 | __END__ | ||||
# spent 50µs within constant::CORE:match which was called 24 times, avg 2µs/call:
# 24 times (50µs+0s) by constant::import at line 74, avg 2µs/call | |||||
sub constant::CORE:qr; # opcode | |||||
# spent 33µs within constant::CORE:regcomp which was called 24 times, avg 1µs/call:
# 24 times (33µs+0s) by constant::import at line 74, avg 1µs/call |