| Filename | /home/hinrik/perl5/perlbrew/perls/perl-5.13.5/lib/5.13.5/Tie/RefHash.pm |
| Statements | Executed 42 statements in 1.90ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 801µs | 3.31ms | Tie::RefHash::BEGIN@96 |
| 1 | 1 | 1 | 743µs | 1.02ms | Tie::RefHash::BEGIN@90 |
| 1 | 1 | 1 | 562µs | 644µs | Tie::RefHash::BEGIN@99 |
| 1 | 1 | 1 | 409µs | 925µs | Tie::RefHash::BEGIN@3 |
| 2 | 1 | 1 | 27µs | 36µs | Tie::RefHash::STORE |
| 1 | 1 | 1 | 24µs | 24µs | Tie::RefHash::TIEHASH |
| 1 | 1 | 1 | 19µs | 19µs | Tie::RefHash::BEGIN@7 |
| 1 | 1 | 1 | 16µs | 138µs | Tie::RefHash::BEGIN@106 |
| 1 | 1 | 1 | 16µs | 79µs | Tie::RefHash::Nestable::BEGIN@262 |
| 1 | 1 | 1 | 11µs | 87µs | Tie::RefHash::BEGIN@94 |
| 1 | 1 | 1 | 11µs | 16µs | Tie::RefHash::BEGIN@93 |
| 1 | 1 | 1 | 11µs | 45µs | Tie::RefHash::BEGIN@91 |
| 0 | 0 | 0 | 0s | 0s | Tie::RefHash::CLEAR |
| 0 | 0 | 0 | 0s | 0s | Tie::RefHash::CLONE |
| 0 | 0 | 0 | 0s | 0s | Tie::RefHash::DELETE |
| 0 | 0 | 0 | 0s | 0s | Tie::RefHash::EXISTS |
| 0 | 0 | 0 | 0s | 0s | Tie::RefHash::FETCH |
| 0 | 0 | 0 | 0s | 0s | Tie::RefHash::FIRSTKEY |
| 0 | 0 | 0 | 0s | 0s | Tie::RefHash::NEXTKEY |
| 0 | 0 | 0 | 0s | 0s | Tie::RefHash::Nestable::STORE |
| 0 | 0 | 0 | 0s | 0s | Tie::RefHash::STORABLE_freeze |
| 0 | 0 | 0 | 0s | 0s | Tie::RefHash::STORABLE_thaw |
| 0 | 0 | 0 | 0s | 0s | Tie::RefHash::__ANON__[:122] |
| 0 | 0 | 0 | 0s | 0s | Tie::RefHash::_reindex_keys |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package Tie::RefHash; | ||||
| 2 | |||||
| 3 | 2 | 153µs | 2 | 963µs | # spent 925µs (409+516) within Tie::RefHash::BEGIN@3 which was called:
# once (409µs+516µs) by Fatal::BEGIN@7 at line 3 # spent 925µs making 1 call to Tie::RefHash::BEGIN@3
# spent 38µs making 1 call to vars::import |
| 4 | |||||
| 5 | 1 | 2µs | $VERSION = "1.38"; | ||
| 6 | |||||
| 7 | 2 | 64µs | 1 | 19µs | # spent 19µs within Tie::RefHash::BEGIN@7 which was called:
# once (19µs+0s) by Fatal::BEGIN@7 at line 7 # spent 19µs making 1 call to Tie::RefHash::BEGIN@7 |
| 8 | |||||
| 9 | =head1 NAME | ||||
| 10 | |||||
| 11 | Tie::RefHash - use references as hash keys | ||||
| 12 | |||||
| 13 | =head1 SYNOPSIS | ||||
| 14 | |||||
| 15 | require 5.004; | ||||
| 16 | use Tie::RefHash; | ||||
| 17 | tie HASHVARIABLE, 'Tie::RefHash', LIST; | ||||
| 18 | tie HASHVARIABLE, 'Tie::RefHash::Nestable', LIST; | ||||
| 19 | |||||
| 20 | untie HASHVARIABLE; | ||||
| 21 | |||||
| 22 | =head1 DESCRIPTION | ||||
| 23 | |||||
| 24 | This module provides the ability to use references as hash keys if you | ||||
| 25 | first C<tie> the hash variable to this module. Normally, only the | ||||
| 26 | keys of the tied hash itself are preserved as references; to use | ||||
| 27 | references as keys in hashes-of-hashes, use Tie::RefHash::Nestable, | ||||
| 28 | included as part of Tie::RefHash. | ||||
| 29 | |||||
| 30 | It is implemented using the standard perl TIEHASH interface. Please | ||||
| 31 | see the C<tie> entry in perlfunc(1) and perltie(1) for more information. | ||||
| 32 | |||||
| 33 | The Nestable version works by looking for hash references being stored | ||||
| 34 | and converting them to tied hashes so that they too can have | ||||
| 35 | references as keys. This will happen without warning whenever you | ||||
| 36 | store a reference to one of your own hashes in the tied hash. | ||||
| 37 | |||||
| 38 | =head1 EXAMPLE | ||||
| 39 | |||||
| 40 | use Tie::RefHash; | ||||
| 41 | tie %h, 'Tie::RefHash'; | ||||
| 42 | $a = []; | ||||
| 43 | $b = {}; | ||||
| 44 | $c = \*main; | ||||
| 45 | $d = \"gunk"; | ||||
| 46 | $e = sub { 'foo' }; | ||||
| 47 | %h = ($a => 1, $b => 2, $c => 3, $d => 4, $e => 5); | ||||
| 48 | $a->[0] = 'foo'; | ||||
| 49 | $b->{foo} = 'bar'; | ||||
| 50 | for (keys %h) { | ||||
| 51 | print ref($_), "\n"; | ||||
| 52 | } | ||||
| 53 | |||||
| 54 | tie %h, 'Tie::RefHash::Nestable'; | ||||
| 55 | $h{$a}->{$b} = 1; | ||||
| 56 | for (keys %h, keys %{$h{$a}}) { | ||||
| 57 | print ref($_), "\n"; | ||||
| 58 | } | ||||
| 59 | |||||
| 60 | =head1 THREAD SUPPORT | ||||
| 61 | |||||
| 62 | L<Tie::RefHash> fully supports threading using the C<CLONE> method. | ||||
| 63 | |||||
| 64 | =head1 STORABLE SUPPORT | ||||
| 65 | |||||
| 66 | L<Storable> hooks are provided for semantically correct serialization and | ||||
| 67 | cloning of tied refhashes. | ||||
| 68 | |||||
| 69 | =head1 RELIC SUPPORT | ||||
| 70 | |||||
| 71 | This version of Tie::RefHash seems to no longer work with 5.004. This has not | ||||
| 72 | been throughly investigated. Patches welcome ;-) | ||||
| 73 | |||||
| 74 | =head1 MAINTAINER | ||||
| 75 | |||||
| 76 | Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt> | ||||
| 77 | |||||
| 78 | =head1 AUTHOR | ||||
| 79 | |||||
| 80 | Gurusamy Sarathy gsar@activestate.com | ||||
| 81 | |||||
| 82 | 'Nestable' by Ed Avis ed@membled.com | ||||
| 83 | |||||
| 84 | =head1 SEE ALSO | ||||
| 85 | |||||
| 86 | perl(1), perlfunc(1), perltie(1) | ||||
| 87 | |||||
| 88 | =cut | ||||
| 89 | |||||
| 90 | 2 | 226µs | 1 | 1.02ms | # spent 1.02ms (743µs+272µs) within Tie::RefHash::BEGIN@90 which was called:
# once (743µs+272µs) by Fatal::BEGIN@7 at line 90 # spent 1.02ms making 1 call to Tie::RefHash::BEGIN@90 |
| 91 | 2 | 31µs | 2 | 80µs | # spent 45µs (11+35) within Tie::RefHash::BEGIN@91 which was called:
# once (11µs+35µs) by Fatal::BEGIN@7 at line 91 # spent 45µs making 1 call to Tie::RefHash::BEGIN@91
# spent 35µs making 1 call to vars::import |
| 92 | 1 | 11µs | @ISA = qw(Tie::Hash); | ||
| 93 | 2 | 25µs | 2 | 21µs | # spent 16µs (11+5) within Tie::RefHash::BEGIN@93 which was called:
# once (11µs+5µs) by Fatal::BEGIN@7 at line 93 # spent 16µs making 1 call to Tie::RefHash::BEGIN@93
# spent 5µs making 1 call to strict::import |
| 94 | 2 | 31µs | 2 | 164µs | # spent 87µs (11+76) within Tie::RefHash::BEGIN@94 which was called:
# once (11µs+76µs) by Fatal::BEGIN@7 at line 94 # spent 87µs making 1 call to Tie::RefHash::BEGIN@94
# spent 76µs making 1 call to Exporter::import |
| 95 | |||||
| 96 | # spent 3.31ms (801µs+2.51) within Tie::RefHash::BEGIN@96 which was called:
# once (801µs+2.51ms) by Fatal::BEGIN@7 at line 104 | ||||
| 97 | 5 | 20µs | local $@; | ||
| 98 | # determine whether we need to take care of threads | ||||
| 99 | 2 | 248µs | 1 | 644µs | # spent 644µs (562+81) within Tie::RefHash::BEGIN@99 which was called:
# once (562µs+81µs) by Fatal::BEGIN@7 at line 99 # spent 644µs making 1 call to Tie::RefHash::BEGIN@99 |
| 100 | 1 | 3µs | 1 | 1.87ms | my $usethreads = $Config::Config{usethreads}; # && exists $INC{"threads.pm"} # spent 1.87ms making 1 call to Config::FETCH |
| 101 | *_HAS_THREADS = $usethreads ? sub () { 1 } : sub () { 0 }; | ||||
| 102 | 2 | 115µs | *_HAS_SCALAR_UTIL = eval { require Scalar::Util; 1 } ? sub () { 1 } : sub () { 0 }; | ||
| 103 | *_HAS_WEAKEN = defined(&Scalar::Util::weaken) ? sub () { 1 } : sub () { 0 }; | ||||
| 104 | 1 | 123µs | 1 | 3.31ms | } # spent 3.31ms making 1 call to Tie::RefHash::BEGIN@96 |
| 105 | |||||
| 106 | # spent 138µs (16+121) within Tie::RefHash::BEGIN@106 which was called:
# once (16µs+121µs) by Fatal::BEGIN@7 at line 124 | ||||
| 107 | # create a refaddr function | ||||
| 108 | |||||
| 109 | 2 | 13µs | local $@; | ||
| 110 | |||||
| 111 | 1 | 121µs | if ( _HAS_SCALAR_UTIL ) { # spent 121µs making 1 call to Exporter::import | ||
| 112 | Scalar::Util->import("refaddr"); | ||||
| 113 | } else { | ||||
| 114 | require overload; | ||||
| 115 | |||||
| 116 | *refaddr = sub { | ||||
| 117 | if ( overload::StrVal($_[0]) =~ /\( 0x ([a-zA-Z0-9]+) \)$/x) { | ||||
| 118 | return $1; | ||||
| 119 | } else { | ||||
| 120 | die "couldn't parse StrVal: " . overload::StrVal($_[0]); | ||||
| 121 | } | ||||
| 122 | }; | ||||
| 123 | } | ||||
| 124 | 1 | 653µs | 1 | 138µs | } # spent 138µs making 1 call to Tie::RefHash::BEGIN@106 |
| 125 | |||||
| 126 | 1 | 1µs | my (@thread_object_registry, $count); # used by the CLONE method to rehash the keys after their refaddr changed | ||
| 127 | |||||
| 128 | # spent 24µs within Tie::RefHash::TIEHASH which was called:
# once (24µs+0s) by autodie::BEGIN@6 at line 172 of Fatal.pm | ||||
| 129 | 5 | 26µs | my $c = shift; | ||
| 130 | my $s = []; | ||||
| 131 | bless $s, $c; | ||||
| 132 | while (@_) { | ||||
| 133 | $s->STORE(shift, shift); | ||||
| 134 | } | ||||
| 135 | |||||
| 136 | if (_HAS_THREADS ) { | ||||
| 137 | |||||
| 138 | if ( _HAS_WEAKEN ) { | ||||
| 139 | # remember the object so that we can rekey it on CLONE | ||||
| 140 | push @thread_object_registry, $s; | ||||
| 141 | # but make this a weak reference, so that there are no leaks | ||||
| 142 | Scalar::Util::weaken( $thread_object_registry[-1] ); | ||||
| 143 | |||||
| 144 | if ( ++$count > 1000 ) { | ||||
| 145 | # this ensures we don't fill up with a huge array dead weakrefs | ||||
| 146 | @thread_object_registry = grep { defined } @thread_object_registry; | ||||
| 147 | $count = 0; | ||||
| 148 | } | ||||
| 149 | } else { | ||||
| 150 | $count++; # used in the warning | ||||
| 151 | } | ||||
| 152 | } | ||||
| 153 | |||||
| 154 | return $s; | ||||
| 155 | } | ||||
| 156 | |||||
| 157 | 1 | 3µs | my $storable_format_version = join("/", __PACKAGE__, "0.01"); | ||
| 158 | |||||
| 159 | sub STORABLE_freeze { | ||||
| 160 | my ( $self, $is_cloning ) = @_; | ||||
| 161 | my ( $refs, $reg ) = @$self; | ||||
| 162 | return ( $storable_format_version, [ values %$refs ], $reg ); | ||||
| 163 | } | ||||
| 164 | |||||
| 165 | sub STORABLE_thaw { | ||||
| 166 | my ( $self, $is_cloning, $version, $refs, $reg ) = @_; | ||||
| 167 | croak "incompatible versions of Tie::RefHash between freeze and thaw" | ||||
| 168 | unless $version eq $storable_format_version; | ||||
| 169 | |||||
| 170 | @$self = ( {}, $reg ); | ||||
| 171 | $self->_reindex_keys( $refs ); | ||||
| 172 | } | ||||
| 173 | |||||
| 174 | sub CLONE { | ||||
| 175 | my $pkg = shift; | ||||
| 176 | |||||
| 177 | if ( $count and not _HAS_WEAKEN ) { | ||||
| 178 | warn "Tie::RefHash is not threadsafe without Scalar::Util::weaken"; | ||||
| 179 | } | ||||
| 180 | |||||
| 181 | # when the thread has been cloned all the objects need to be updated. | ||||
| 182 | # dead weakrefs are undefined, so we filter them out | ||||
| 183 | @thread_object_registry = grep { defined && do { $_->_reindex_keys; 1 } } @thread_object_registry; | ||||
| 184 | $count = 0; # we just cleaned up | ||||
| 185 | } | ||||
| 186 | |||||
| 187 | sub _reindex_keys { | ||||
| 188 | my ( $self, $extra_keys ) = @_; | ||||
| 189 | # rehash all the ref keys based on their new StrVal | ||||
| 190 | %{ $self->[0] } = map { refaddr($_->[0]) => $_ } (values(%{ $self->[0] }), @{ $extra_keys || [] }); | ||||
| 191 | } | ||||
| 192 | |||||
| 193 | sub FETCH { | ||||
| 194 | my($s, $k) = @_; | ||||
| 195 | if (ref $k) { | ||||
| 196 | my $kstr = refaddr($k); | ||||
| 197 | if (defined $s->[0]{$kstr}) { | ||||
| 198 | $s->[0]{$kstr}[1]; | ||||
| 199 | } | ||||
| 200 | else { | ||||
| 201 | undef; | ||||
| 202 | } | ||||
| 203 | } | ||||
| 204 | else { | ||||
| 205 | $s->[1]{$k}; | ||||
| 206 | } | ||||
| 207 | } | ||||
| 208 | |||||
| 209 | # spent 36µs (27+9) within Tie::RefHash::STORE which was called 2 times, avg 18µs/call:
# 2 times (27µs+9µs) by Fatal::_make_fatal at line 1205 of Fatal.pm, avg 18µs/call | ||||
| 210 | 6 | 38µs | my($s, $k, $v) = @_; | ||
| 211 | 2 | 10µs | if (ref $k) { # spent 10µs making 2 calls to Scalar::Util::refaddr, avg 5µs/call | ||
| 212 | $s->[0]{refaddr($k)} = [$k, $v]; | ||||
| 213 | } | ||||
| 214 | else { | ||||
| 215 | $s->[1]{$k} = $v; | ||||
| 216 | } | ||||
| 217 | $v; | ||||
| 218 | } | ||||
| 219 | |||||
| 220 | sub DELETE { | ||||
| 221 | my($s, $k) = @_; | ||||
| 222 | (ref $k) | ||||
| 223 | ? (delete($s->[0]{refaddr($k)}) || [])->[1] | ||||
| 224 | : delete($s->[1]{$k}); | ||||
| 225 | } | ||||
| 226 | |||||
| 227 | sub EXISTS { | ||||
| 228 | my($s, $k) = @_; | ||||
| 229 | (ref $k) ? exists($s->[0]{refaddr($k)}) : exists($s->[1]{$k}); | ||||
| 230 | } | ||||
| 231 | |||||
| 232 | sub FIRSTKEY { | ||||
| 233 | my $s = shift; | ||||
| 234 | keys %{$s->[0]}; # reset iterator | ||||
| 235 | keys %{$s->[1]}; # reset iterator | ||||
| 236 | $s->[2] = 0; # flag for iteration, see NEXTKEY | ||||
| 237 | $s->NEXTKEY; | ||||
| 238 | } | ||||
| 239 | |||||
| 240 | sub NEXTKEY { | ||||
| 241 | my $s = shift; | ||||
| 242 | my ($k, $v); | ||||
| 243 | if (!$s->[2]) { | ||||
| 244 | if (($k, $v) = each %{$s->[0]}) { | ||||
| 245 | return $v->[0]; | ||||
| 246 | } | ||||
| 247 | else { | ||||
| 248 | $s->[2] = 1; | ||||
| 249 | } | ||||
| 250 | } | ||||
| 251 | return each %{$s->[1]}; | ||||
| 252 | } | ||||
| 253 | |||||
| 254 | sub CLEAR { | ||||
| 255 | my $s = shift; | ||||
| 256 | $s->[2] = 0; | ||||
| 257 | %{$s->[0]} = (); | ||||
| 258 | %{$s->[1]} = (); | ||||
| 259 | } | ||||
| 260 | |||||
| 261 | package Tie::RefHash::Nestable; | ||||
| 262 | 2 | 104µs | 2 | 141µs | # spent 79µs (16+62) within Tie::RefHash::Nestable::BEGIN@262 which was called:
# once (16µs+62µs) by Fatal::BEGIN@7 at line 262 # spent 79µs making 1 call to Tie::RefHash::Nestable::BEGIN@262
# spent 62µs making 1 call to vars::import |
| 263 | 1 | 4µs | @ISA = 'Tie::RefHash'; | ||
| 264 | |||||
| 265 | sub STORE { | ||||
| 266 | my($s, $k, $v) = @_; | ||||
| 267 | if (ref($v) eq 'HASH' and not tied %$v) { | ||||
| 268 | my @elems = %$v; | ||||
| 269 | tie %$v, ref($s), @elems; | ||||
| 270 | } | ||||
| 271 | $s->SUPER::STORE($k, $v); | ||||
| 272 | } | ||||
| 273 | |||||
| 274 | 1 | 6µs | 1; |