Filename | /usr/lib/perl5/5.12.4/constant.pm |
Statements | Executed 411 statements in 8.97ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
16 | 16 | 3 | 3.10ms | 3.79ms | import | constant::
19 | 4 | 1 | 267µs | 267µs | CORE:regcomp (opcode) | constant::
1 | 1 | 1 | 240µs | 240µs | BEGIN@2 | constant::
16 | 1 | 1 | 232µs | 232µs | CORE:match (opcode) | constant::
1 | 1 | 1 | 63µs | 63µs | BEGIN@25 | constant::
1 | 1 | 1 | 53µs | 138µs | BEGIN@114 | constant::
1 | 1 | 1 | 50µs | 136µs | BEGIN@52 | constant::
1 | 1 | 1 | 50µs | 75µs | BEGIN@3 | constant::
1 | 1 | 1 | 49µs | 139µs | BEGIN@29 | constant::
1 | 1 | 1 | 48µs | 279µs | BEGIN@6 | constant::
1 | 1 | 1 | 47µs | 656µs | BEGIN@4 | constant::
3 | 3 | 1 | 46µs | 46µs | CORE:qr (opcode) | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:130] | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:134] | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:136] | constant::
0 | 0 | 0 | 0s | 0s | __ANON__[:31] | constant::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package constant; | ||||
2 | 3 | 318µs | 1 | 240µs | # spent 240µs within constant::BEGIN@2 which was called:
# once (240µs+0s) by DateTime::Duration::BEGIN@23 at line 2 # spent 240µs making 1 call to constant::BEGIN@2 |
3 | 3 | 134µs | 2 | 101µs | # spent 75µs (50+26) within constant::BEGIN@3 which was called:
# once (50µs+26µs) by DateTime::Duration::BEGIN@23 at line 3 # spent 75µs making 1 call to constant::BEGIN@3
# spent 26µs making 1 call to strict::import |
4 | 3 | 158µs | 2 | 1.26ms | # spent 656µs (47+609) within constant::BEGIN@4 which was called:
# once (47µs+609µs) by DateTime::Duration::BEGIN@23 at line 4 # spent 656µs making 1 call to constant::BEGIN@4
# spent 609µs making 1 call to warnings::register::import |
5 | |||||
6 | 3 | 697µs | 2 | 511µs | # spent 279µs (48+232) within constant::BEGIN@6 which was called:
# once (48µs+232µs) by DateTime::Duration::BEGIN@23 at line 6 # spent 279µs making 1 call to constant::BEGIN@6
# spent 232µs making 1 call to vars::import |
7 | 1 | 7µs | $VERSION = '1.20'; | ||
8 | |||||
9 | #======================================================================= | ||||
10 | |||||
11 | # Some names are evil choices. | ||||
12 | 1 | 35µs | my %keywords = map +($_, 1), qw{ BEGIN INIT CHECK END DESTROY AUTOLOAD }; | ||
13 | 1 | 9µs | $keywords{UNITCHECK}++ if $] > 5.009; | ||
14 | |||||
15 | 1 | 36µs | my %forced_into_main = map +($_, 1), | ||
16 | qw{ STDIN STDOUT STDERR ARGV ARGVOUT ENV INC SIG }; | ||||
17 | |||||
18 | 1 | 27µs | my %forbidden = (%keywords, %forced_into_main); | ||
19 | |||||
20 | 1 | 7µs | my $str_end = $] >= 5.006 ? "\\z" : "\\Z"; | ||
21 | 1 | 165µs | 2 | 98µs | my $normal_constant_name = qr/^_?[^\W_0-9]\w*$str_end/; # spent 71µs making 1 call to constant::CORE:regcomp
# spent 26µs making 1 call to constant::CORE:qr |
22 | 1 | 73µs | 2 | 40µs | my $tolerable = qr/^[A-Za-z_]\w*$str_end/; # spent 31µs making 1 call to constant::CORE:regcomp
# spent 10µs making 1 call to constant::CORE:qr |
23 | 1 | 73µs | 2 | 41µs | my $boolean = qr/^[01]?$str_end/; # spent 31µs making 1 call to constant::CORE:regcomp
# spent 10µs making 1 call to constant::CORE:qr |
24 | |||||
25 | # spent 63µs within constant::BEGIN@25 which was called:
# once (63µs+0s) by DateTime::Duration::BEGIN@23 at line 32 | ||||
26 | # We'd like to do use constant _CAN_PCS => $] > 5.009002 | ||||
27 | # but that's a bit tricky before we load the constant module :-) | ||||
28 | # By doing this, we save 1 run time check for *every* call to import. | ||||
29 | 3 | 250µs | 2 | 229µs | # spent 139µs (49+90) within constant::BEGIN@29 which was called:
# once (49µs+90µs) by DateTime::Duration::BEGIN@23 at line 29 # spent 139µs making 1 call to constant::BEGIN@29
# spent 90µs making 1 call to strict::unimport |
30 | 1 | 12µs | my $const = $] > 5.009002; | ||
31 | 1 | 52µs | *_CAN_PCS = sub () {$const}; | ||
32 | 1 | 361µs | 1 | 63µs | } # spent 63µs making 1 call to constant::BEGIN@25 |
33 | |||||
34 | #======================================================================= | ||||
35 | # import() - import symbols into user's namespace | ||||
36 | # | ||||
37 | # What we actually do is define a function in the caller's namespace | ||||
38 | # which returns the value. The function we create will normally | ||||
39 | # be inlined as a constant, thereby avoiding further sub calling | ||||
40 | # overhead. | ||||
41 | #======================================================================= | ||||
42 | # spent 3.79ms (3.10+690µs) within constant::import which was called 16 times, avg 237µs/call:
# once (248µs+58µs) by DateTime::Duration::BEGIN@23 at line 23 of DateTime/Duration.pm
# once (230µs+60µs) by DateTime::TimeZone::BEGIN@17 at line 17 of DateTime/TimeZone.pm
# once (202µs+52µs) by DateTime::BEGIN@72 at line 72 of DateTime.pm
# once (199µs+47µs) by DateTime::TimeZone::BEGIN@27 at line 27 of DateTime/TimeZone.pm
# once (194µs+48µs) by DateTime::TimeZone::BEGIN@23 at line 23 of DateTime/TimeZone.pm
# once (182µs+50µs) by DateTime::TimeZone::BEGIN@21 at line 21 of DateTime/TimeZone.pm
# once (209µs+22µs) by DateTime::BEGIN@74 at line 74 of DateTime.pm
# once (182µs+48µs) by DateTime::TimeZone::BEGIN@25 at line 25 of DateTime/TimeZone.pm
# once (182µs+47µs) by DateTime::TimeZone::BEGIN@18 at line 18 of DateTime/TimeZone.pm
# once (187µs+41µs) by DateTime::BEGIN@78 at line 78 of DateTime.pm
# once (186µs+38µs) by DateTime::BEGIN@80 at line 80 of DateTime.pm
# once (179µs+45µs) by DateTime::TimeZone::BEGIN@24 at line 24 of DateTime/TimeZone.pm
# once (178µs+45µs) by DateTime::TimeZone::BEGIN@22 at line 22 of DateTime/TimeZone.pm
# once (176µs+45µs) by DateTime::TimeZone::BEGIN@26 at line 26 of DateTime/TimeZone.pm
# once (185µs+23µs) by DateTime::BEGIN@75 at line 75 of DateTime.pm
# once (174µs+21µs) by DateTime::BEGIN@76 at line 76 of DateTime.pm | ||||
43 | 16 | 83µs | my $class = shift; | ||
44 | 16 | 44µs | return unless @_; # Ignore 'use constant;' | ||
45 | 16 | 31µs | my $constants; | ||
46 | 16 | 60µs | my $multiple = ref $_[0]; | ||
47 | 16 | 76µs | my $pkg = caller; | ||
48 | 16 | 31µs | my $flush_mro; | ||
49 | 16 | 31µs | my $symtab; | ||
50 | |||||
51 | 16 | 82µs | if (_CAN_PCS) { | ||
52 | 3 | 1.44ms | 2 | 223µs | # spent 136µs (50+87) within constant::BEGIN@52 which was called:
# once (50µs+87µs) by DateTime::Duration::BEGIN@23 at line 52 # spent 136µs making 1 call to constant::BEGIN@52
# spent 87µs making 1 call to strict::unimport |
53 | 16 | 156µs | $symtab = \%{$pkg . '::'}; | ||
54 | }; | ||||
55 | |||||
56 | 16 | 77µs | if ( $multiple ) { | ||
57 | if (ref $_[0] ne 'HASH') { | ||||
58 | require Carp; | ||||
59 | Carp::croak("Invalid reference type '".ref(shift)."' not 'HASH'"); | ||||
60 | } | ||||
61 | $constants = shift; | ||||
62 | } else { | ||||
63 | 16 | 40µs | unless (defined $_[0]) { | ||
64 | require Carp; | ||||
65 | Carp::croak("Can't use undef as constant name"); | ||||
66 | } | ||||
67 | 16 | 158µs | $constants->{+shift} = undef; | ||
68 | } | ||||
69 | |||||
70 | 16 | 223µs | foreach my $name ( keys %$constants ) { | ||
71 | # Normal constant name | ||||
72 | 16 | 806µs | 32 | 366µs | if ($name =~ $normal_constant_name and !$forbidden{$name}) { # spent 232µs making 16 calls to constant::CORE:match, avg 15µs/call
# spent 134µs making 16 calls to constant::CORE:regcomp, avg 8µs/call |
73 | # Everything is okay | ||||
74 | |||||
75 | # Name forced into main, but we're not in main. Fatal. | ||||
76 | } elsif ($forced_into_main{$name} and $pkg ne 'main') { | ||||
77 | require Carp; | ||||
78 | Carp::croak("Constant name '$name' is forced into main::"); | ||||
79 | |||||
80 | # Starts with double underscore. Fatal. | ||||
81 | } elsif ($name =~ /^__/) { | ||||
82 | require Carp; | ||||
83 | Carp::croak("Constant name '$name' begins with '__'"); | ||||
84 | |||||
85 | # Maybe the name is tolerable | ||||
86 | } elsif ($name =~ $tolerable) { | ||||
87 | # Then we'll warn only if you've asked for warnings | ||||
88 | if (warnings::enabled()) { | ||||
89 | if ($keywords{$name}) { | ||||
90 | warnings::warn("Constant name '$name' is a Perl keyword"); | ||||
91 | } elsif ($forced_into_main{$name}) { | ||||
92 | warnings::warn("Constant name '$name' is " . | ||||
93 | "forced into package main::"); | ||||
94 | } | ||||
95 | } | ||||
96 | |||||
97 | # Looks like a boolean | ||||
98 | # use constant FRED == fred; | ||||
99 | } elsif ($name =~ $boolean) { | ||||
100 | require Carp; | ||||
101 | if (@_) { | ||||
102 | Carp::croak("Constant name '$name' is invalid"); | ||||
103 | } else { | ||||
104 | Carp::croak("Constant name looks like boolean value"); | ||||
105 | } | ||||
106 | |||||
107 | } else { | ||||
108 | # Must have bad characters | ||||
109 | require Carp; | ||||
110 | Carp::croak("Constant name '$name' has invalid characters"); | ||||
111 | } | ||||
112 | |||||
113 | { | ||||
114 | 19 | 1.29ms | 2 | 224µs | # spent 138µs (53+86) within constant::BEGIN@114 which was called:
# once (53µs+86µs) by DateTime::Duration::BEGIN@23 at line 114 # spent 138µs making 1 call to constant::BEGIN@114
# spent 86µs making 1 call to strict::unimport |
115 | 16 | 87µs | my $full_name = "${pkg}::$name"; | ||
116 | 16 | 119µs | $declared{$full_name}++; | ||
117 | 16 | 109µs | if ($multiple || @_ == 1) { | ||
118 | 16 | 77µs | my $scalar = $multiple ? $constants->{$name} : $_[0]; | ||
119 | # The constant serves to optimise this entire block out on | ||||
120 | # 5.8 and earlier. | ||||
121 | 16 | 124µs | if (_CAN_PCS && $symtab && !exists $symtab->{$name}) { | ||
122 | # No typeglob yet, so we can use a reference as space- | ||||
123 | # efficient proxy for a constant subroutine | ||||
124 | # The check in Perl_ck_rvconst knows that inlinable | ||||
125 | # constants from cv_const_sv are read only. So we have to: | ||||
126 | 13 | 322µs | 13 | 115µs | Internals::SvREADONLY($scalar, 1); # spent 115µs making 13 calls to Internals::SvREADONLY, avg 9µs/call |
127 | 13 | 90µs | $symtab->{$name} = \$scalar; | ||
128 | 13 | 50µs | ++$flush_mro; | ||
129 | } else { | ||||
130 | 3 | 123µs | *$full_name = sub () { $scalar }; | ||
131 | } | ||||
132 | } elsif (@_) { | ||||
133 | my @list = @_; | ||||
134 | *$full_name = sub () { @list }; | ||||
135 | } else { | ||||
136 | *$full_name = sub () { }; | ||||
137 | } | ||||
138 | } | ||||
139 | } | ||||
140 | # Flush the cache exactly once if we make any direct symbol table changes. | ||||
141 | 16 | 768µs | 13 | 209µs | mro::method_changed_in($pkg) if _CAN_PCS && $flush_mro; # spent 209µs making 13 calls to mro::method_changed_in, avg 16µs/call |
142 | } | ||||
143 | |||||
144 | 1 | 52µs | 1; | ||
145 | |||||
146 | __END__ | ||||
# spent 232µs within constant::CORE:match which was called 16 times, avg 15µs/call:
# 16 times (232µs+0s) by constant::import at line 72, avg 15µs/call | |||||
sub constant::CORE:qr; # opcode | |||||
# spent 267µs within constant::CORE:regcomp which was called 19 times, avg 14µs/call:
# 16 times (134µs+0s) by constant::import at line 72, avg 8µs/call
# once (71µs+0s) by DateTime::Duration::BEGIN@23 at line 21
# once (31µs+0s) by DateTime::Duration::BEGIN@23 at line 23
# once (31µs+0s) by DateTime::Duration::BEGIN@23 at line 22 |