Filename | /home/hinrik/perl5/perlbrew/perls/perl-5.13.5/lib/5.13.5/constant.pm |
Statements | Executed 554 statements in 2.51ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
22 | 22 | 4 | 1.27ms | 1.50ms | import | constant::
22 | 1 | 1 | 74µs | 74µs | CORE:match (opcode) | constant::
25 | 4 | 1 | 71µs | 71µs | CORE:regcomp (opcode) | constant::
1 | 1 | 1 | 51µs | 51µs | BEGIN@2 | constant::
1 | 1 | 1 | 15µs | 15µs | BEGIN@25 | constant::
1 | 1 | 1 | 13µs | 22µs | BEGIN@3 | constant::
1 | 1 | 1 | 12µs | 229µs | BEGIN@4 | constant::
1 | 1 | 1 | 12µs | 40µs | BEGIN@29 | constant::
1 | 1 | 1 | 11µs | 71µs | BEGIN@6 | constant::
3 | 3 | 1 | 11µs | 11µs | CORE:qr (opcode) | constant::
1 | 1 | 1 | 11µs | 35µs | BEGIN@114 | constant::
1 | 1 | 1 | 11µs | 33µs | BEGIN@52 | 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 | 55µs | 1 | 51µs | # spent 51µs within constant::BEGIN@2 which was called:
# once (51µs+0s) by Fatal::BEGIN@10 at line 2 # spent 51µs making 1 call to constant::BEGIN@2 |
3 | 2 | 28µs | 2 | 31µs | # spent 22µs (13+9) within constant::BEGIN@3 which was called:
# once (13µs+9µs) by Fatal::BEGIN@10 at line 3 # spent 22µs making 1 call to constant::BEGIN@3
# spent 9µs making 1 call to strict::import |
4 | 2 | 32µs | 2 | 446µs | # spent 229µs (12+217) within constant::BEGIN@4 which was called:
# once (12µs+217µs) by Fatal::BEGIN@10 at line 4 # spent 229µs making 1 call to constant::BEGIN@4
# spent 217µs making 1 call to warnings::register::import |
5 | |||||
6 | 2 | 130µs | 2 | 131µs | # spent 71µs (11+60) within constant::BEGIN@6 which was called:
# once (11µs+60µs) by Fatal::BEGIN@10 at line 6 # spent 71µs making 1 call to constant::BEGIN@6
# spent 60µs making 1 call to vars::import |
7 | 1 | 2µ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 | 38µs | 2 | 26µs | my $normal_constant_name = qr/^_?[^\W_0-9]\w*$str_end/; # spent 19µs making 1 call to constant::CORE:regcomp
# spent 7µ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 | 8µ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 15µs within constant::BEGIN@25 which was called:
# once (15µs+0s) by Fatal::BEGIN@10 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 | 55µs | 2 | 68µs | # spent 40µs (12+28) within constant::BEGIN@29 which was called:
# once (12µs+28µs) by Fatal::BEGIN@10 at line 29 # spent 40µs making 1 call to constant::BEGIN@29
# spent 28µs making 1 call to strict::unimport |
30 | 2 | 16µs | my $const = $] > 5.009002; | ||
31 | *_CAN_PCS = sub () {$const}; | ||||
32 | 1 | 60µs | 1 | 15µs | } # spent 15µ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 1.50ms (1.27+228µs) within constant::import which was called 22 times, avg 68µs/call:
# once (141µs+9µs) by Fatal::BEGIN@13 at line 13 of Fatal.pm
# once (69µs+34µs) by Fatal::BEGIN@10 at line 10 of Fatal.pm
# once (72µs+18µs) by autodie::BEGIN@14 at line 14 of autodie.pm
# once (64µs+15µs) by Hailo::BEGIN@17 at line 17 of Hailo.pm
# once (64µs+13µs) by DBD::SQLite::BEGIN@30 at line 30 of DBD/SQLite.pm
# once (52µs+8µs) by Fatal::BEGIN@31 at line 31 of Fatal.pm
# once (51µs+9µs) by Fatal::BEGIN@20 at line 20 of Fatal.pm
# once (51µs+9µs) by Fatal::BEGIN@14 at line 14 of Fatal.pm
# once (52µs+8µs) by Fatal::BEGIN@19 at line 19 of Fatal.pm
# once (51µs+8µs) by Fatal::BEGIN@21 at line 21 of Fatal.pm
# once (51µs+9µs) by Fatal::BEGIN@12 at line 12 of Fatal.pm
# once (51µs+8µs) by Fatal::BEGIN@22 at line 22 of Fatal.pm
# once (50µs+9µs) by Fatal::BEGIN@17 at line 17 of Fatal.pm
# once (50µs+8µs) by Fatal::BEGIN@16 at line 16 of Fatal.pm
# once (51µs+8µs) by Fatal::BEGIN@18 at line 18 of Fatal.pm
# once (51µs+8µs) by Fatal::BEGIN@35 at line 35 of Fatal.pm
# once (50µs+9µs) by Fatal::BEGIN@29 at line 29 of Fatal.pm
# once (51µs+8µs) by Fatal::BEGIN@25 at line 25 of Fatal.pm
# once (51µs+8µs) by Fatal::BEGIN@33 at line 33 of Fatal.pm
# once (50µs+8µs) by Fatal::BEGIN@23 at line 23 of Fatal.pm
# once (49µs+8µs) by Fatal::BEGIN@40 at line 40 of Fatal.pm
# once (49µs+8µs) by Fatal::BEGIN@27 at line 27 of Fatal.pm | ||||
43 | 242 | 554µs | my $class = shift; | ||
44 | return unless @_; # Ignore 'use constant;' | ||||
45 | my $constants; | ||||
46 | my $multiple = ref $_[0]; | ||||
47 | my $pkg = caller; | ||||
48 | my $flush_mro; | ||||
49 | my $symtab; | ||||
50 | |||||
51 | 22 | 43µs | if (_CAN_PCS) { | ||
52 | 2 | 261µs | 2 | 55µs | # spent 33µs (11+22) within constant::BEGIN@52 which was called:
# once (11µs+22µs) by Fatal::BEGIN@10 at line 52 # spent 33µs making 1 call to constant::BEGIN@52
# spent 22µs making 1 call to strict::unimport |
53 | $symtab = \%{$pkg . '::'}; | ||||
54 | }; | ||||
55 | |||||
56 | 44 | 72µ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 | unless (defined $_[0]) { | ||||
64 | require Carp; | ||||
65 | Carp::croak("Can't use undef as constant name"); | ||||
66 | } | ||||
67 | $constants->{+shift} = undef; | ||||
68 | } | ||||
69 | |||||
70 | foreach my $name ( keys %$constants ) { | ||||
71 | # Normal constant name | ||||
72 | 44 | 345µs | 44 | 115µs | if ($name =~ $normal_constant_name and !$forbidden{$name}) { # spent 74µs making 22 calls to constant::CORE:match, avg 3µs/call
# spent 41µs making 22 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 | 2 | 226µs | 2 | 58µs | # spent 35µs (11+23) within constant::BEGIN@114 which was called:
# once (11µs+23µs) by Fatal::BEGIN@10 at line 114 # spent 35µs making 1 call to constant::BEGIN@114
# spent 24µs making 1 call to strict::unimport |
115 | 66 | 221µs | my $full_name = "${pkg}::$name"; | ||
116 | $declared{$full_name}++; | ||||
117 | 44 | 87µs | if ($multiple || @_ == 1) { | ||
118 | my $scalar = $multiple ? $constants->{$name} : $_[0]; | ||||
119 | # The constant serves to optimise this entire block out on | ||||
120 | # 5.8 and earlier. | ||||
121 | 66 | 215µ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 | 22 | 64µs | Internals::SvREADONLY($scalar, 1); # spent 64µs making 22 calls to Internals::SvREADONLY, avg 3µs/call | ||
127 | $symtab->{$name} = \$scalar; | ||||
128 | ++$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 | 22 | 48µs | mro::method_changed_in($pkg) if _CAN_PCS && $flush_mro; # spent 48µs making 22 calls to mro::method_changed_in, avg 2µs/call | ||
142 | } | ||||
143 | |||||
144 | 1 | 12µs | 1; | ||
145 | |||||
146 | __END__ | ||||
# spent 74µs within constant::CORE:match which was called 22 times, avg 3µs/call:
# 22 times (74µs+0s) by constant::import at line 72, avg 3µs/call | |||||
sub constant::CORE:qr; # opcode | |||||
sub constant::CORE:regcomp; # opcode |