| 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 | constant::import |
| 35 | 1 | 1 | 97µs | 97µs | constant::CORE:match (opcode) |
| 38 | 4 | 1 | 85µs | 85µs | constant::CORE:regcomp (opcode) |
| 1 | 1 | 1 | 32µs | 32µs | constant::BEGIN@2 |
| 1 | 1 | 1 | 13µs | 35µs | constant::BEGIN@114 |
| 1 | 1 | 1 | 12µs | 12µs | constant::BEGIN@25 |
| 1 | 1 | 1 | 12µs | 198µs | constant::BEGIN@4 |
| 1 | 1 | 1 | 11µs | 16µs | constant::BEGIN@3 |
| 1 | 1 | 1 | 11µs | 35µs | constant::BEGIN@29 |
| 1 | 1 | 1 | 11µs | 32µs | constant::BEGIN@52 |
| 1 | 1 | 1 | 10µs | 62µs | constant::BEGIN@6 |
| 3 | 3 | 1 | 10µs | 10µs | constant::CORE:qr (opcode) |
| 0 | 0 | 0 | 0s | 0s | constant::__ANON__[:130] |
| 0 | 0 | 0 | 0s | 0s | constant::__ANON__[:134] |
| 0 | 0 | 0 | 0s | 0s | constant::__ANON__[:136] |
| 0 | 0 | 0 | 0s | 0s | constant::__ANON__[:31] |
| 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 | 2 | 12µs | my $const = $] > 5.009002; | ||
| 31 | *_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 | 385 | 815µ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 | 35 | 66µ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 | $symtab = \%{$pkg . '::'}; | ||||
| 54 | }; | ||||
| 55 | |||||
| 56 | 70 | 118µ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 | 70 | 498µ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 | 2 | 205µ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 | 105 | 205µs | my $full_name = "${pkg}::$name"; | ||
| 116 | $declared{$full_name}++; | ||||
| 117 | 70 | 138µ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 | 105 | 292µ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 | 63µs | Internals::SvREADONLY($scalar, 1); # spent 63µs making 35 calls to Internals::SvREADONLY, avg 2µ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 | 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 |