Filename | /usr/share/perl5/core_perl/constant.pm |
Statements | Executed 51 statements in 3.67ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 1.49ms | 1.95ms | BEGIN@6 | constant::
1 | 1 | 1 | 147µs | 195µs | BEGIN@3 | constant::
1 | 1 | 1 | 106µs | 151µs | import | constant::
1 | 1 | 1 | 59µs | 59µs | BEGIN@2 | constant::
1 | 1 | 1 | 50µs | 50µs | BEGIN@24 | constant::
1 | 1 | 1 | 24µs | 60µs | BEGIN@116 | constant::
1 | 1 | 1 | 22µs | 59µs | BEGIN@28 | constant::
1 | 1 | 1 | 22µs | 266µs | BEGIN@4 | constant::
1 | 1 | 1 | 20µs | 51µs | BEGIN@54 | constant::
1 | 1 | 1 | 19µs | 19µs | CORE:regcomp (opcode) | constant::
3 | 3 | 1 | 12µs | 12µs | CORE:qr (opcode) | constant::
1 | 1 | 1 | 10µs | 10µs | CORE:match (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 | 172µs | 1 | 59µs | # spent 59µs within constant::BEGIN@2 which was called:
# once (59µs+0s) by Data::Dumper::BEGIN@271 at line 2 # spent 59µs making 1 call to constant::BEGIN@2 |
3 | 2 | 91µs | 2 | 242µs | # spent 195µs (147+47) within constant::BEGIN@3 which was called:
# once (147µs+47µs) by Data::Dumper::BEGIN@271 at line 3 # spent 195µs making 1 call to constant::BEGIN@3
# spent 47µs making 1 call to strict::import |
4 | 2 | 97µs | 2 | 510µs | # spent 266µs (22+244) within constant::BEGIN@4 which was called:
# once (22µs+244µs) by Data::Dumper::BEGIN@271 at line 4 # spent 266µs making 1 call to constant::BEGIN@4
# spent 244µs making 1 call to warnings::register::import |
5 | |||||
6 | 2 | 682µs | 2 | 2.07ms | # spent 1.95ms (1.49+463µs) within constant::BEGIN@6 which was called:
# once (1.49ms+463µs) by Data::Dumper::BEGIN@271 at line 6 # spent 1.95ms making 1 call to constant::BEGIN@6
# spent 120µs making 1 call to vars::import |
7 | 1 | 2µs | $VERSION = '1.27'; | ||
8 | |||||
9 | #======================================================================= | ||||
10 | |||||
11 | # Some names are evil choices. | ||||
12 | 1 | 24µs | my %keywords = map +($_, 1), qw{ BEGIN INIT CHECK END DESTROY AUTOLOAD }; | ||
13 | 1 | 5µs | $keywords{UNITCHECK}++ if $] > 5.009; | ||
14 | |||||
15 | 1 | 17µs | my %forced_into_main = map +($_, 1), | ||
16 | qw{ STDIN STDOUT STDERR ARGV ARGVOUT ENV INC SIG }; | ||||
17 | |||||
18 | 1 | 17µs | my %forbidden = (%keywords, %forced_into_main); | ||
19 | |||||
20 | 1 | 23µs | 1 | 6µs | my $normal_constant_name = qr/^_?[^\W_0-9]\w*\z/; # spent 6µs making 1 call to constant::CORE:qr |
21 | 1 | 9µs | 1 | 3µs | my $tolerable = qr/^[A-Za-z_]\w*\z/; # spent 3µs making 1 call to constant::CORE:qr |
22 | 1 | 8µs | 1 | 2µs | my $boolean = qr/^[01]?\z/; # spent 2µs making 1 call to constant::CORE:qr |
23 | |||||
24 | # spent 50µs within constant::BEGIN@24 which was called:
# once (50µs+0s) by Data::Dumper::BEGIN@271 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 | 248µs | 2 | 96µs | # spent 59µs (22+37) within constant::BEGIN@28 which was called:
# once (22µs+37µs) by Data::Dumper::BEGIN@271 at line 28 # spent 59µs making 1 call to constant::BEGIN@28
# spent 37µs making 1 call to strict::unimport |
29 | 1 | 4µs | my $const = $] > 5.009002; | ||
30 | 1 | 24µs | *_CAN_PCS = sub () {$const}; | ||
31 | |||||
32 | 1 | 2µs | my $downgrade = $] < 5.015004; # && $] >= 5.008 | ||
33 | 1 | 23µs | *_DOWNGRADE = sub () { $downgrade }; | ||
34 | 1 | 249µs | 1 | 50µs | } # spent 50µ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 151µs (106+45) within constant::import which was called:
# once (106µs+45µs) by Data::Dumper::BEGIN@271 at line 271 of Data/Dumper.pm | ||||
45 | 1 | 2µs | my $class = shift; | ||
46 | 1 | 900ns | return unless @_; # Ignore 'use constant;' | ||
47 | 1 | 300ns | my $constants; | ||
48 | 1 | 2µs | my $multiple = ref $_[0]; | ||
49 | 1 | 2µs | my $pkg = caller; | ||
50 | 1 | 300ns | my $flush_mro; | ||
51 | 1 | 200ns | my $symtab; | ||
52 | |||||
53 | 1 | 1µs | if (_CAN_PCS) { | ||
54 | 2 | 962µs | 2 | 82µs | # spent 51µs (20+32) within constant::BEGIN@54 which was called:
# once (20µs+32µs) by Data::Dumper::BEGIN@271 at line 54 # spent 51µs making 1 call to constant::BEGIN@54
# spent 32µs making 1 call to strict::unimport |
55 | 1 | 4µs | $symtab = \%{$pkg . '::'}; | ||
56 | }; | ||||
57 | |||||
58 | 1 | 800ns | if ( $multiple ) { | ||
59 | if (ref $_[0] ne 'HASH') { | ||||
60 | require Carp; | ||||
61 | Carp::croak("Invalid reference type '".ref(shift)."' not 'HASH'"); | ||||
62 | } | ||||
63 | $constants = shift; | ||||
64 | } else { | ||||
65 | 1 | 800ns | unless (defined $_[0]) { | ||
66 | require Carp; | ||||
67 | Carp::croak("Can't use undef as constant name"); | ||||
68 | } | ||||
69 | 1 | 3µs | $constants->{+shift} = undef; | ||
70 | } | ||||
71 | |||||
72 | 1 | 5µs | foreach my $name ( keys %$constants ) { | ||
73 | # Normal constant name | ||||
74 | 1 | 54µs | 2 | 29µs | if ($name =~ $normal_constant_name and !$forbidden{$name}) { # spent 19µs making 1 call to constant::CORE:regcomp
# spent 10µs making 1 call to constant::CORE:match |
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 | 3 | 843µs | 2 | 97µs | # spent 60µs (24+36) within constant::BEGIN@116 which was called:
# once (24µs+36µs) by Data::Dumper::BEGIN@271 at line 116 # spent 60µs making 1 call to constant::BEGIN@116
# spent 36µs making 1 call to strict::unimport |
117 | 1 | 3µs | my $full_name = "${pkg}::$name"; | ||
118 | 1 | 3µs | $declared{$full_name}++; | ||
119 | 1 | 2µs | if ($multiple || @_ == 1) { | ||
120 | 1 | 2µ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 | 1 | 3µ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 | 1 | 24µs | 1 | 10µs | Internals::SvREADONLY($scalar, 1); # spent 10µs making 1 call to Internals::SvREADONLY |
137 | 1 | 2µs | $symtab->{$name} = \$scalar; | ||
138 | 1 | 1µ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 | 1 | 31µs | 1 | 5µs | mro::method_changed_in($pkg) if _CAN_PCS && $flush_mro; # spent 5µs making 1 call to mro::method_changed_in |
152 | } | ||||
153 | |||||
154 | 1 | 24µs | 1; | ||
155 | |||||
156 | __END__ | ||||
# spent 10µs within constant::CORE:match which was called:
# once (10µs+0s) by constant::import at line 74 | |||||
sub constant::CORE:qr; # opcode | |||||
# spent 19µs within constant::CORE:regcomp which was called:
# once (19µs+0s) by constant::import at line 74 |