Data-HashMap
view release on metacpan or search on metacpan
"get_or_set" and the counters do; "exists", "persist" and a failed "cas"
do not.
"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.
TTL expiry
With a default "ttl", or a per-key one from "put_ttl" (which also works
on a map without a default), entries expire lazily, with one-second
resolution: an entry with a TTL of *n* seconds is readable for between
*n* and *n*+1 seconds. Iteration ("keys", "values", "items", "each",
"to_hash") skips expired entries, and "get", "exists" and the counters
remove one they touch; "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. "purge" reaps every expired entry on demand and never
removes a live one.
On a map with a default TTL, "put" and the counters renew an existing
entry's lifetime to that default -- even one given its own TTL by
"put_ttl" or made permanent by "persist" -- so a counter that keeps
being hit never expires. "swap", "cas", "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 "put" clears any "put_ttl" deadline.
"get_or_set" inserts with the default TTL; use "put_ttl" for a per-key
one.
On a map with both "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 "purge"
periodically so that expired entries, not live ones, make room.
Expiry is measured against the wall clock ("time"), so stepping the
system clock moves every deadline with it; a clock set backward keeps
entries alive longer.
CAVEATS
Keyword syntax
A keyword taking two or more arguments takes a plain comma list with
no parentheses: "hm_ii_get $m, $k". "hm_ii_get($m, $k)" fails to
compile with "Expected ','", and "=>" is not a separator;
one-argument keywords such as hm_xx_size($m) do accept parentheses.
A keyword parses like a list operator, so everything after the last
comma is the last argument: "hm_xx_get $m, $k // $default" looks up
"$k // $default". Parenthesise the call when an operator follows it:
"(hm_xx_get $m, $k) // $default", "(hm_sa_get $m, $id)->{field}".
"hm_xx_keys", "hm_xx_values" and "hm_xx_items" return the entry
count in scalar context, which on a TTL map includes expired entries
the list form skips. "no Data::HashMap::XX" does not remove the
keywords.
Integers
Keys and values are checked against the variant's range on every
call, "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: "put", "put_ttl", "get_or_set" and lookups ignore
them, as do "swap" and "cas" (returning undef and false), while
"incr", "decr" and "incr_by" croak. An undef key or value coerces as
in Perl, to "" or 0, usually with a warning; SV* variants store an
undef value as-is. The counters croak rather than overflow, leaving
the value unchanged: "incr" with "increment failed", "decr" with
"decrement failed" and "incr_by" with "incr_by failed". The I16 and
I32 croaks name the full type range, reserved values included. A
64-bit perl ("use64bitint") is required.
String keys
Identity is the key's bytes. A UTF-8-flagged key whose characters
fit in one byte is downgraded first, so "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 "put", and collides with its own
encoded octets: "\x{263a}" and "\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.
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 $_ in a
"while (<$fh>)" loop leaves every entry holding the loop's final
"undef"; a "substr" result, $1 or a "foreach" variable changes after
it is stored; "from_hash", "clone", "merge" and "to_hash" share SVs
with their source; and a stored literal is read-only. Store a copy
("$_"), or pass a true fourth argument, "copy", to "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 "get", "values", "items" and "each"
return the map's own SV, as "values %h" does, and referents are
shared.
Iteration and order
Key order is unspecified and differs between processes, as with
Perl's own hashes; sort if you need a stable one. "each" restarts
after "clear", and whenever another write resizes or compacts the
table ("swap", "cas" and "persist" only do so by reaping an expired
entry), so do not write to the map during "each"; reads are safe. In
scalar context it returns the key, so a "while" loop over it must
guard with "defined" -- "while (defined(my $k = hm_xx_each $map))"
-- or it stops early on a false key such as 0 or "". "pop", "shift"
and "drain" keep their own cursor, so they do not move an "each" in
progress unless they compact the table. On an LRU map "pop" and
"shift" take the least and most recently used entry; "drain" always
goes in table order. In scalar context the three return the last
value removed. "pop" and "shift" skip expired entries; on an LRU map
they also reap them, and on a plain map "size" keeps counting them
until a lookup reaps them.
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:
"f($m->get_direct($k), $m->remove($k))" hands "f" freed memory. "my
$d = $m->get_direct($k)" copies, so $d is safe to keep.
freeze and thaw
( run in 1.590 second using v1.01-cache-2.11-cpan-60884a8df76 )