Filename | /home/hinrik/perl5/perlbrew/perls/perl-5.13.5/lib/5.13.5/constant.pm |
Statements | Executed 866 statements in 3.00ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
35 | 35 | 5 | 1.79ms | 2.09ms | import | constant::
35 | 1 | 1 | 97µs | 97µs | CORE:match (opcode) | constant::
38 | 4 | 1 | 85µs | 85µs | CORE:regcomp (opcode) | constant::
1 | 1 | 1 | 32µs | 32µs | BEGIN@2 | constant::
1 | 1 | 1 | 13µs | 35µs | BEGIN@114 | constant::
1 | 1 | 1 | 12µs | 12µs | BEGIN@25 | constant::
1 | 1 | 1 | 12µs | 198µs | BEGIN@4 | constant::
1 | 1 | 1 | 11µs | 16µs | BEGIN@3 | constant::
1 | 1 | 1 | 11µs | 35µs | BEGIN@29 | constant::
1 | 1 | 1 | 11µs | 32µs | BEGIN@52 | constant::
1 | 1 | 1 | 10µs | 62µs | BEGIN@6 | constant::
3 | 3 | 1 | 10µs | 10µ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 | 2 | 38µs | 1 | 32µs | # spent 32µs within constant::BEGIN@2 which was called:
# once (32µs+0s) by Getopt::Long::BEGIN@208 at line 2 # spent 32µs making 1 call to constant::BEGIN@2 |
3 | 2 | 25µs | 2 | 22µs | # spent 16µs (11+5) within constant::BEGIN@3 which was called:
# once (11µs+5µs) by Getopt::Long::BEGIN@208 at line 3 # spent 16µs making 1 call to constant::BEGIN@3
# spent 5µs making 1 call to strict::import |
4 | 2 | 29µs | 2 | 383µs | # spent 198µs (12+186) within constant::BEGIN@4 which was called:
# once (12µs+186µs) by Getopt::Long::BEGIN@208 at line 4 # spent 198µs making 1 call to constant::BEGIN@4
# spent 186µs making 1 call to warnings::register::import |
5 | |||||
6 | 2 | 112µs | 2 | 114µs | # spent 62µs (10+52) within constant::BEGIN@6 which was called:
# once (10µs+52µs) by Getopt::Long::BEGIN@208 at line 6 # spent 62µs making 1 call to constant::BEGIN@6
# spent 52µs making 1 call to vars::import |
7 | 1 | 1µs | $VERSION = '1.20'; | ||
8 | |||||
9 | #======================================================================= | ||||
10 | |||||
11 | # Some names are evil choices. | ||||
12 | 1 | 8µs | my %keywords = map +($_, 1), qw{ BEGIN INIT CHECK END DESTROY AUTOLOAD }; | ||
13 | 1 | 2µs | $keywords{UNITCHECK}++ if $] > 5.009; | ||
14 | |||||
15 | 1 | 7µ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 | 2µs | my $str_end = $] >= 5.006 ? "\\z" : "\\Z"; | ||
21 | 1 | 28µs | 2 | 18µs | my $normal_constant_name = qr/^_?[^\W_0-9]\w*$str_end/; # spent 12µs making 1 call to constant::CORE:regcomp
# spent 6µs making 1 call to constant::CORE:qr |
22 | 1 | 14µs | 2 | 8µs | my $tolerable = qr/^[A-Za-z_]\w*$str_end/; # spent 6µs making 1 call to constant::CORE:regcomp
# spent 2µs making 1 call to constant::CORE:qr |
23 | 1 | 14µs | 2 | 7µs | my $boolean = qr/^[01]?$str_end/; # spent 6µs making 1 call to constant::CORE:regcomp
# spent 2µs making 1 call to constant::CORE:qr |
24 | |||||
25 | # spent 12µs within constant::BEGIN@25 which was called:
# once (12µs+0s) by Getopt::Long::BEGIN@208 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 | 2 | 50µs | 2 | 60µs | # spent 35µs (11+25) within constant::BEGIN@29 which was called:
# once (11µs+25µs) by Getopt::Long::BEGIN@208 at line 29 # spent 35µs making 1 call to constant::BEGIN@29
# spent 25µs making 1 call to strict::unimport |
30 | 1 | 2µs | my $const = $] > 5.009002; | ||
31 | 1 | 10µs | *_CAN_PCS = sub () {$const}; | ||
32 | 1 | 49µs | 1 | 12µs | } # spent 12µ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 2.09ms (1.79+292µs) within constant::import which was called 35 times, avg 60µs/call:
# once (64µs+12µs) by Fatal::BEGIN@10 at line 10 of Fatal.pm
# once (63µs+13µs) by DBD::SQLite::BEGIN@30 at line 30 of DBD/SQLite.pm
# once (60µs+13µs) by Getopt::Long::BEGIN@208 at line 208 of Getopt/Long.pm
# once (61µs+9µs) by Hailo::BEGIN@11 at line 11 of lib/Hailo.pm
# once (60µs+8µs) by Getopt::Long::BEGIN@237 at line 237 of Getopt/Long.pm
# once (59µs+8µs) by Getopt::Long::BEGIN@226 at line 226 of Getopt/Long.pm
# once (58µs+8µs) by Fatal::BEGIN@21 at line 21 of Fatal.pm
# once (54µs+10µs) by autodie::BEGIN@14 at line 14 of autodie.pm
# once (53µs+8µs) by Fatal::BEGIN@33 at line 33 of Fatal.pm
# once (52µs+7µs) by Fatal::BEGIN@23 at line 23 of Fatal.pm
# once (51µs+8µs) by Getopt::Long::BEGIN@220 at line 220 of Getopt/Long.pm
# once (50µs+8µs) by Fatal::BEGIN@13 at line 13 of Fatal.pm
# once (51µs+7µs) by Fatal::BEGIN@25 at line 25 of Fatal.pm
# once (50µs+8µs) by Getopt::Long::BEGIN@229 at line 229 of Getopt/Long.pm
# once (50µs+8µs) by Fatal::BEGIN@22 at line 22 of Fatal.pm
# once (50µs+8µs) by Fatal::BEGIN@27 at line 27 of Fatal.pm
# once (50µs+7µs) by Fatal::BEGIN@29 at line 29 of Fatal.pm
# once (49µs+8µs) by Getopt::Long::BEGIN@247 at line 247 of Getopt/Long.pm
# once (48µs+9µs) by Getopt::Long::BEGIN@218 at line 218 of Getopt/Long.pm
# once (49µs+8µs) by Fatal::BEGIN@17 at line 17 of Fatal.pm
# once (48µs+8µs) by Fatal::BEGIN@31 at line 31 of Fatal.pm
# once (48µs+8µs) by Getopt::Long::BEGIN@223 at line 223 of Getopt/Long.pm
# once (49µs+7µs) by Fatal::BEGIN@40 at line 40 of Fatal.pm
# once (48µs+8µs) by Getopt::Long::BEGIN@225 at line 225 of Getopt/Long.pm
# once (48µs+8µs) by Fatal::BEGIN@19 at line 19 of Fatal.pm
# once (48µs+8µs) by Getopt::Long::BEGIN@228 at line 228 of Getopt/Long.pm
# once (48µs+7µs) by Fatal::BEGIN@20 at line 20 of Fatal.pm
# once (48µs+7µs) by Fatal::BEGIN@18 at line 18 of Fatal.pm
# once (48µs+7µs) by Fatal::BEGIN@35 at line 35 of Fatal.pm
# once (47µs+8µs) by Fatal::BEGIN@14 at line 14 of Fatal.pm
# once (47µs+8µs) by Fatal::BEGIN@12 at line 12 of Fatal.pm
# once (47µs+8µs) by Getopt::Long::BEGIN@222 at line 222 of Getopt/Long.pm
# once (47µs+8µs) by Getopt::Long::BEGIN@224 at line 224 of Getopt/Long.pm
# once (47µs+7µs) by Fatal::BEGIN@16 at line 16 of Fatal.pm
# once (45µs+8µs) by Getopt::Long::BEGIN@236 at line 236 of Getopt/Long.pm | ||||
43 | 35 | 46µs | my $class = shift; | ||
44 | 35 | 40µs | return unless @_; # Ignore 'use constant;' | ||
45 | 35 | 29µs | my $constants; | ||
46 | 35 | 42µs | my $multiple = ref $_[0]; | ||
47 | 35 | 39µs | my $pkg = caller; | ||
48 | 35 | 31µs | my $flush_mro; | ||
49 | 35 | 29µs | my $symtab; | ||
50 | |||||
51 | 35 | 74µs | if (_CAN_PCS) { | ||
52 | 2 | 261µs | 2 | 53µs | # spent 32µs (11+21) within constant::BEGIN@52 which was called:
# once (11µs+21µs) by Getopt::Long::BEGIN@208 at line 52 # spent 32µs making 1 call to constant::BEGIN@52
# spent 21µs making 1 call to strict::unimport |
53 | 35 | 66µs | $symtab = \%{$pkg . '::'}; | ||
54 | }; | ||||
55 | |||||
56 | 35 | 64µ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 | 35 | 36µs | unless (defined $_[0]) { | ||
64 | require Carp; | ||||
65 | Carp::croak("Can't use undef as constant name"); | ||||
66 | } | ||||
67 | 35 | 83µs | $constants->{+shift} = undef; | ||
68 | } | ||||
69 | |||||
70 | 35 | 110µs | foreach my $name ( keys %$constants ) { | ||
71 | # Normal constant name | ||||
72 | 35 | 374µs | 70 | 159µs | if ($name =~ $normal_constant_name and !$forbidden{$name}) { # spent 97µs making 35 calls to constant::CORE:match, avg 3µs/call
# spent 61µs making 35 calls to constant::CORE:regcomp, avg 2µ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 | 37 | 329µs | 2 | 57µs | # spent 35µs (13+22) within constant::BEGIN@114 which was called:
# once (13µs+22µs) by Getopt::Long::BEGIN@208 at line 114 # spent 35µs making 1 call to constant::BEGIN@114
# spent 22µs making 1 call to strict::unimport |
115 | 35 | 58µs | my $full_name = "${pkg}::$name"; | ||
116 | 35 | 62µs | $declared{$full_name}++; | ||
117 | 35 | 85µs | if ($multiple || @_ == 1) { | ||
118 | 35 | 46µs | my $scalar = $multiple ? $constants->{$name} : $_[0]; | ||
119 | # The constant serves to optimise this entire block out on | ||||
120 | # 5.8 and earlier. | ||||
121 | 35 | 92µ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 | 35 | 197µs | 35 | 63µs | Internals::SvREADONLY($scalar, 1); # spent 63µs making 35 calls to Internals::SvREADONLY, avg 2µs/call |
127 | 35 | 53µs | $symtab->{$name} = \$scalar; | ||
128 | 35 | 42µs | ++$flush_mro; | ||
129 | } else { | ||||
130 | *$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 | 35 | 310µs | 35 | 71µs | mro::method_changed_in($pkg) if _CAN_PCS && $flush_mro; # spent 71µs making 35 calls to mro::method_changed_in, avg 2µs/call |
142 | } | ||||
143 | |||||
144 | 1 | 10µs | 1; | ||
145 | |||||
146 | __END__ | ||||
# spent 97µs within constant::CORE:match which was called 35 times, avg 3µs/call:
# 35 times (97µs+0s) by constant::import at line 72, avg 3µs/call | |||||
sub constant::CORE:qr; # opcode | |||||
# spent 85µs within constant::CORE:regcomp which was called 38 times, avg 2µs/call:
# 35 times (61µs+0s) by constant::import at line 72, avg 2µs/call
# once (12µs+0s) by Getopt::Long::BEGIN@208 at line 21
# once (6µs+0s) by Getopt::Long::BEGIN@208 at line 22
# once (6µs+0s) by Getopt::Long::BEGIN@208 at line 23 |