| Filename | /home/ss5/perl5/perlbrew/perls/perl-5.14.1/lib/5.14.1/constant.pm |
| Statements | Executed 310 statements in 5.73ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 3 | 3 | 3 | 1.23ms | 1.64ms | constant::import |
| 25 | 4 | 1 | 180µs | 180µs | constant::CORE:regcomp (opcode) |
| 22 | 1 | 1 | 149µs | 149µs | constant::CORE:match (opcode) |
| 1 | 1 | 1 | 88µs | 88µs | constant::BEGIN@2 |
| 3 | 3 | 1 | 79µs | 79µs | constant::CORE:qr (opcode) |
| 1 | 1 | 1 | 48µs | 48µs | constant::BEGIN@25 |
| 1 | 1 | 1 | 32µs | 67µs | constant::BEGIN@52 |
| 1 | 1 | 1 | 30µs | 71µs | constant::BEGIN@114 |
| 1 | 1 | 1 | 28µs | 75µs | constant::BEGIN@29 |
| 1 | 1 | 1 | 28µs | 449µs | constant::BEGIN@4 |
| 1 | 1 | 1 | 27µs | 172µs | constant::BEGIN@6 |
| 1 | 1 | 1 | 27µs | 41µs | constant::BEGIN@3 |
| 0 | 0 | 0 | 0s | 0s | constant::__ANON__[:136] |
| 0 | 0 | 0 | 0s | 0s | constant::__ANON__[:140] |
| 0 | 0 | 0 | 0s | 0s | constant::__ANON__[:142] |
| 0 | 0 | 0 | 0s | 0s | constant::__ANON__[:31] |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package constant; | ||||
| 2 | 2 | 178µs | 1 | 88µs | # spent 88µs within constant::BEGIN@2 which was called:
# once (88µs+0s) by Data::DPath::Filters::BEGIN@15 at line 2 # spent 88µs making 1 call to constant::BEGIN@2 |
| 3 | 2 | 86µs | 2 | 55µs | # spent 41µs (27+14) within constant::BEGIN@3 which was called:
# once (27µs+14µs) by Data::DPath::Filters::BEGIN@15 at line 3 # spent 41µs making 1 call to constant::BEGIN@3
# spent 14µs making 1 call to strict::import |
| 4 | 2 | 162µs | 2 | 870µs | # spent 449µs (28+421) within constant::BEGIN@4 which was called:
# once (28µs+421µs) by Data::DPath::Filters::BEGIN@15 at line 4 # spent 449µs making 1 call to constant::BEGIN@4
# spent 421µs making 1 call to warnings::register::import |
| 5 | |||||
| 6 | 2 | 554µs | 2 | 316µs | # spent 172µs (27+144) within constant::BEGIN@6 which was called:
# once (27µs+144µs) by Data::DPath::Filters::BEGIN@15 at line 6 # spent 172µs making 1 call to constant::BEGIN@6
# spent 144µs making 1 call to vars::import |
| 7 | 1 | 2µs | $VERSION = '1.21'; | ||
| 8 | |||||
| 9 | #======================================================================= | ||||
| 10 | |||||
| 11 | # Some names are evil choices. | ||||
| 12 | 1 | 23µs | my %keywords = map +($_, 1), qw{ BEGIN INIT CHECK END DESTROY AUTOLOAD }; | ||
| 13 | 1 | 6µs | $keywords{UNITCHECK}++ if $] > 5.009; | ||
| 14 | |||||
| 15 | 1 | 22µs | my %forced_into_main = map +($_, 1), | ||
| 16 | qw{ STDIN STDOUT STDERR ARGV ARGVOUT ENV INC SIG }; | ||||
| 17 | |||||
| 18 | 1 | 21µs | my %forbidden = (%keywords, %forced_into_main); | ||
| 19 | |||||
| 20 | 1 | 3µs | my $str_end = $] >= 5.006 ? "\\z" : "\\Z"; | ||
| 21 | 1 | 170µs | 2 | 131µs | my $normal_constant_name = qr/^_?[^\W_0-9]\w*$str_end/; # spent 69µs making 1 call to constant::CORE:qr
# spent 62µs making 1 call to constant::CORE:regcomp |
| 22 | 1 | 45µs | 2 | 26µs | my $tolerable = qr/^[A-Za-z_]\w*$str_end/; # spent 20µs making 1 call to constant::CORE:regcomp
# spent 6µs making 1 call to constant::CORE:qr |
| 23 | 1 | 41µs | 2 | 24µs | my $boolean = qr/^[01]?$str_end/; # spent 20µs making 1 call to constant::CORE:regcomp
# spent 5µs making 1 call to constant::CORE:qr |
| 24 | |||||
| 25 | # spent 48µs within constant::BEGIN@25 which was called:
# once (48µs+0s) by Data::DPath::Filters::BEGIN@15 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 | 193µs | 2 | 122µs | # spent 75µs (28+47) within constant::BEGIN@29 which was called:
# once (28µs+47µs) by Data::DPath::Filters::BEGIN@15 at line 29 # spent 75µs making 1 call to constant::BEGIN@29
# spent 47µs making 1 call to strict::unimport |
| 30 | 1 | 5µs | my $const = $] > 5.009002; | ||
| 31 | 1 | 45µs | *_CAN_PCS = sub () {$const}; | ||
| 32 | 1 | 264µs | 1 | 48µs | } # spent 48µ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.64ms (1.23+414µs) within constant::import which was called 3 times, avg 547µs/call:
# once (550µs+190µs) by Data::DPath::Context::BEGIN@64 at line 64 of Data/DPath/Context.pm
# once (429µs+141µs) by Data::DPath::Path::BEGIN@27 at line 27 of Data/DPath/Path.pm
# once (249µs+82µs) by Data::DPath::Filters::BEGIN@15 at line 16 of Data/DPath/Filters.pm | ||||
| 43 | 3 | 7µs | my $class = shift; | ||
| 44 | 3 | 6µs | return unless @_; # Ignore 'use constant;' | ||
| 45 | 3 | 2µs | my $constants; | ||
| 46 | 3 | 9µs | my $multiple = ref $_[0]; | ||
| 47 | 3 | 7µs | my $pkg = caller; | ||
| 48 | 3 | 2µs | my $flush_mro; | ||
| 49 | 3 | 2µs | my $symtab; | ||
| 50 | |||||
| 51 | 3 | 5µs | if (_CAN_PCS) { | ||
| 52 | 2 | 1.21ms | 2 | 103µs | # spent 67µs (32+35) within constant::BEGIN@52 which was called:
# once (32µs+35µs) by Data::DPath::Filters::BEGIN@15 at line 52 # spent 67µs making 1 call to constant::BEGIN@52
# spent 35µs making 1 call to strict::unimport |
| 53 | 3 | 19µs | $symtab = \%{$pkg . '::'}; | ||
| 54 | }; | ||||
| 55 | |||||
| 56 | 3 | 5µs | if ( $multiple ) { | ||
| 57 | 3 | 9µs | if (ref $_[0] ne 'HASH') { | ||
| 58 | require Carp; | ||||
| 59 | Carp::croak("Invalid reference type '".ref(shift)."' not 'HASH'"); | ||||
| 60 | } | ||||
| 61 | 3 | 3µs | $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 | 3 | 30µs | foreach my $name ( keys %$constants ) { | ||
| 71 | # Normal constant name | ||||
| 72 | 22 | 510µs | 44 | 228µs | if ($name =~ $normal_constant_name and !$forbidden{$name}) { # spent 149µs making 22 calls to constant::CORE:match, avg 7µs/call
# spent 78µs making 22 calls to constant::CORE:regcomp, avg 4µ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 | 24 | 1.05ms | 2 | 112µs | # spent 71µs (30+41) within constant::BEGIN@114 which was called:
# once (30µs+41µs) by Data::DPath::Filters::BEGIN@15 at line 114 # spent 71µs making 1 call to constant::BEGIN@114
# spent 41µs making 1 call to strict::unimport |
| 115 | 22 | 60µs | my $full_name = "${pkg}::$name"; | ||
| 116 | 22 | 84µs | $declared{$full_name}++; | ||
| 117 | 22 | 43µs | if ($multiple || @_ == 1) { | ||
| 118 | 22 | 49µs | my $scalar = $multiple ? $constants->{$name} : $_[0]; | ||
| 119 | |||||
| 120 | # Work around perl bug #xxxxx: Sub names (actually glob | ||||
| 121 | # names in general) ignore the UTF8 flag. So we have to | ||||
| 122 | # turn it off to get the "right" symbol table entry. | ||||
| 123 | 22 | 241µs | 22 | 80µs | utf8::is_utf8 $name and utf8::encode $name; # spent 80µs making 22 calls to utf8::is_utf8, avg 4µs/call |
| 124 | |||||
| 125 | # The constant serves to optimise this entire block out on | ||||
| 126 | # 5.8 and earlier. | ||||
| 127 | 22 | 68µs | if (_CAN_PCS && $symtab && !exists $symtab->{$name}) { | ||
| 128 | # No typeglob yet, so we can use a reference as space- | ||||
| 129 | # efficient proxy for a constant subroutine | ||||
| 130 | # The check in Perl_ck_rvconst knows that inlinable | ||||
| 131 | # constants from cv_const_sv are read only. So we have to: | ||||
| 132 | 22 | 240µs | 22 | 82µs | Internals::SvREADONLY($scalar, 1); # spent 82µs making 22 calls to Internals::SvREADONLY, avg 4µs/call |
| 133 | 22 | 60µs | $symtab->{$name} = \$scalar; | ||
| 134 | 22 | 32µs | ++$flush_mro; | ||
| 135 | } else { | ||||
| 136 | *$full_name = sub () { $scalar }; | ||||
| 137 | } | ||||
| 138 | } elsif (@_) { | ||||
| 139 | my @list = @_; | ||||
| 140 | *$full_name = sub () { @list }; | ||||
| 141 | } else { | ||||
| 142 | *$full_name = sub () { }; | ||||
| 143 | } | ||||
| 144 | } | ||||
| 145 | } | ||||
| 146 | # Flush the cache exactly once if we make any direct symbol table changes. | ||||
| 147 | 3 | 108µs | 3 | 24µs | mro::method_changed_in($pkg) if _CAN_PCS && $flush_mro; # spent 24µs making 3 calls to mro::method_changed_in, avg 8µs/call |
| 148 | } | ||||
| 149 | |||||
| 150 | 1 | 45µs | 1; | ||
| 151 | |||||
| 152 | __END__ | ||||
# spent 149µs within constant::CORE:match which was called 22 times, avg 7µs/call:
# 22 times (149µs+0s) by constant::import at line 72, avg 7µs/call | |||||
sub constant::CORE:qr; # opcode | |||||
# spent 180µs within constant::CORE:regcomp which was called 25 times, avg 7µs/call:
# 22 times (78µs+0s) by constant::import at line 72, avg 4µs/call
# once (62µs+0s) by Data::DPath::Filters::BEGIN@15 at line 21
# once (20µs+0s) by Data::DPath::Filters::BEGIN@15 at line 22
# once (20µs+0s) by Data::DPath::Filters::BEGIN@15 at line 23 |