Data-HashMap

 view release on metacpan or  search on metacpan

lib/Data/HashMap.pm  view on Meta::CPAN

do not.

C<lru_skip> (0-99, larger values clamp to 99) skips promotion on exactly that
percentage of the accesses that would promote: 90 promotes one in ten. The
least recently used entry is always promoted when touched, so it cannot be
starved, and touching the most recently used one never counts. This trades
eviction precision for speed on read-heavy workloads with hot keys; 90 suits
most caches.

=head2 TTL expiry

With a default C<ttl>, or a per-key one from C<put_ttl> (which also works on a
map without a default), entries expire lazily, with one-second resolution: an
entry with a TTL of I<n> seconds is readable for between I<n> and I<n>+1
seconds. Iteration (C<keys>, C<values>, C<items>, C<each>, C<to_hash>) skips
expired entries, and C<get>, C<exists> and the counters remove one they touch;
C<size> counts expired entries until they are removed.

Entries nobody touches again are reaped when the table would otherwise grow, so
a map fed never-repeating keys stays bounded, at a few times its live set.
C<purge> reaps every expired entry on demand and never removes a live one.

On a map with a default TTL, C<put> and the counters renew an existing entry's
lifetime to that default -- even one given its own TTL by C<put_ttl> or made
permanent by C<persist> -- so a counter that keeps being hit never expires.
C<swap>, C<cas>, C<get_or_set> on an existing key, and reads leave the expiry
alone. On a map without a default the counters leave it alone too, while C<put>
clears any C<put_ttl> deadline. C<get_or_set> inserts with the default TTL; use
C<put_ttl> for a per-key one.

On a map with both C<max_size> and a TTL, eviction takes the least recently
used entry whether or not it has expired, and an expired entry keeps its slot
and its value until it is reaped: call C<purge> periodically so that expired
entries, not live ones, make room.

Expiry is measured against the wall clock (C<time>), so stepping the system
clock moves every deadline with it; a clock set backward keeps entries alive
longer.

=head1 CAVEATS

=over

=item Keyword syntax

A keyword taking two or more arguments takes a plain comma list with no
parentheses: C<hm_ii_get $m, $k>. C<hm_ii_get($m, $k)> fails to compile with
"Expected ','", and C<< => >> is not a separator; one-argument keywords such as
C<hm_xx_size($m)> do accept parentheses. A keyword parses like a list operator,
so everything after the last comma is the last argument:
C<< hm_xx_get $m, $k // $default >> looks up C<< $k // $default >>.
Parenthesise the call when an operator follows it:
C<< (hm_xx_get $m, $k) // $default >>, C<< (hm_sa_get $m, $id)->{field} >>.
C<hm_xx_keys>, C<hm_xx_values> and C<hm_xx_items> return the entry count in
scalar context, which on a TTL map includes expired entries the list form
skips. C<no Data::HashMap::XX> does not remove the keywords.

=item Integers

Keys and values are checked against the variant's range on every call,
C<from_hash> included, and croak outside it rather than wrap. NaN croaks
wherever a number is expected. INT_MIN and INT_MIN+1 are reserved as keys:
C<put>, C<put_ttl>, C<get_or_set> and lookups ignore them, as do C<swap> and
C<cas> (returning undef and false), while C<incr>, C<decr> and C<incr_by>
croak. An undef key or value coerces as in Perl, to C<""> or 0, usually with
a warning; SV* variants store an undef value as-is. The counters croak rather
than overflow, leaving the value unchanged: C<incr> with C<increment failed>,
C<decr> with C<decrement failed> and C<incr_by> with C<incr_by failed>. The
I16 and I32 croaks name the full type range, reserved values included. A
64-bit perl (C<use64bitint>) is required.

=item String keys

Identity is the key's bytes. A UTF-8-flagged key whose characters fit in one
byte is downgraded first, so C<"caf\xe9"> and its upgraded form are one key, as
in a hash. A key that needs UTF-8 is returned with the flag of its most recent
C<put>, and collides with its own encoded octets: C<"\x{263a}"> and
C<"\xe2\x98\xba"> are one key here and two in a hash, so copying a hash that
holds both into a map keeps only one.

=item Perl values

By default the SV* variants (I16A, I32A, IA, SA) store the SV you pass, not a
copy, as 0.08 did. That is the fastest option, but the stored value stays tied
to the caller's variable: storing C<$_> in a C<while (E<lt>$fhE<gt>)> loop
leaves every entry holding the loop's final C<undef>; a C<substr> result, C<$1>
or a C<foreach> variable changes after it is stored; C<from_hash>, C<clone>,
C<merge> and C<to_hash> share SVs with their source; and a stored literal is
read-only. Store a copy (C<"$_">), or pass a true fourth argument, C<copy>, to
C<new>: the map then copies each value it stores, as a hash does, at the cost
of one SV copy per put. In either mode C<get>, C<values>, C<items> and C<each>
return the map's own SV, as C<values %h> does, and referents are shared.

=item Iteration and order

Key order is unspecified and differs between processes, as with Perl's own
hashes; sort if you need a stable one. C<each> restarts after C<clear>, and
whenever another write resizes or compacts the table (C<swap>, C<cas> and
C<persist> only do so by reaping an expired entry), so do not write to the
map during C<each>; reads are safe. In scalar context it returns the key, so
a C<while> loop over it must guard with C<defined> -- C<while (defined(my $k =
hm_xx_each $map))> -- or it stops early on a false key such as C<0> or C<"">.
C<pop>, C<shift> and C<drain> keep their own cursor, so they do not move an
C<each> in progress unless they compact the table. On an LRU map C<pop> and
C<shift> take the least and most recently used entry; C<drain> always goes in
table order. In scalar context the three return the last value removed.
C<pop> and C<shift> skip expired entries; on an LRU map they also reap them,
and on a plain map C<size> keeps counting them until a lookup reaps them.

=item get_direct

Returns a read-only SV that borrows the map's buffer instead of copying it, for
immediate use such as comparing or printing. It is valid until that entry is
next changed, removed, reaped or evicted, so on a TTL or LRU map treat it as
valid only until the next call on the map. Arguments are evaluated before a
call: C<< f($m->get_direct($k), $m->remove($k)) >> hands C<f> freed memory.
C<< my $d = $m->get_direct($k) >> copies, so C<$d> is safe to keep.

=item freeze and thaw

The format is native-endian and not portable across byte orders. Output is not



( run in 1.056 second using v1.01-cache-2.11-cpan-60884a8df76 )