Data-HashMap-Shared
view release on metacpan or search on metacpan
# TTL (entries expire after N seconds)
my $ttl_map = Data::HashMap::Shared::II->new('/tmp/ttl.shm', 100000, 0, 60);
shm_ii_put $ttl_map, 1, 10; # expires in 60s
shm_ii_put_ttl $ttl_map, 2, 20, 5; # per-key: expires in 5s
# Multiprocess
if (fork() == 0) {
my $child = Data::HashMap::Shared::II->new('/tmp/mymap.shm', 100000);
shm_ii_incr $child, 1; # atomic increment visible to parent
exit;
}
wait;
DESCRIPTION
Data::HashMap::Shared provides type-specialized hash maps stored in
file-backed shared memory (mmap(MAP_SHARED)) for multiprocess data sharing
on Linux. With opt-in LRU eviction and per-key TTL it doubles as a fast
cross-process cache; lookups take a lock-free seqlock path.
Linux-only. Requires 64-bit Perl on a little-endian architecture.
Features
* File-backed mmap for cross-process sharing
* Futex-based read-write lock (fast userspace path)
* Atomic counters (incr/decr under the read lock on maps without LRU or
TTL)
* Elastic capacity (starts small, grows/shrinks automatically)
* Arena allocator for string storage in shared memory
* Keyword API via XS::Parse::Keyword for maximum speed
* Opt-in LRU eviction -- clock/second-chance algorithm; reads stay
lock-free
* Opt-in per-key TTL expiry -- lazy removal on access; monotonic clock
* Stale lock recovery for both writers and readers (dead PIDs detected
and drained automatically)
Variants
Data::HashMap::Shared::I16 - int16 to int16
Data::HashMap::Shared::I32 - int32 to int32
Data::HashMap::Shared::II - int64 to int64
Data::HashMap::Shared::I16S - int16 to string
Data::HashMap::Shared::I32S - int32 to string
Data::HashMap::Shared::IS - int64 to string
Data::HashMap::Shared::SI16 - string to int16
Data::HashMap::Shared::SI32 - string to int32
Data::HashMap::Shared::SI - string to int64
Data::HashMap::Shared::SS - string to string
Integer Range and Wrapping
Integer keys and values are fixed-width two's complement: 16-bit for
"I16"/"SI16"/"I16S", 32-bit for "I32"/"SI32"/"I32S", 64-bit for
"II"/"IS"/"SI". A number outside the variant's range is silently truncated
to its low bits: on an "I16" map 70000 is key 4464. Numbers beyond 64 bits
saturate first (1e20 becomes -1, NaN 0). "incr" and "decr" wrap the same
way. Pick a variant wide enough for your data.
Constructor
my $map = Data::HashMap::Shared::II->new($path, $max_entries);
my $map = Data::HashMap::Shared::II->new(undef, $max_entries); # anonymous
my $map = Data::HashMap::Shared::II->new($path, $max_entries, $max_size);
my $map = Data::HashMap::Shared::II->new($path, $max_entries, $max_size, $ttl);
my $map = Data::HashMap::Shared::II->new($path, $max_entries, $max_size, $ttl, $lru_skip);
my $map = Data::HashMap::Shared::SS->new($path, $max_entries, 0, 0, 0, $arena_cap); # explicit arena bytes
my $map = Data::HashMap::Shared::II->new($path, $max_entries, $max_size, $ttl, $lru_skip, $arena_cap, $file_mode);
my $map = Data::HashMap::Shared::II->new_sharded($prefix, $shards, $max_entries, $max_size, $ttl, $lru_skip, $arena_cap, $file_mode);
my $map = Data::HashMap::Shared::II->new_memfd($name, $max_entries, ...); # memfd-backed
my $map = Data::HashMap::Shared::II->new_from_fd($fd); # reopen memfd
my $fd = $map->memfd; # -1 if not memfd
Creates or opens a map backed by file $path; "undef" creates an anonymous
mapping shared only with "fork"ed children. Any number of processes may
open the same file. The sizing arguments apply only when the file is
created: an existing file's header wins, though the arguments are still
range-checked. Opening a file of another variant, or a corrupt one,
croaks.
A handle cannot cross into a new ithread or be copied: open the map again
instead (in a thread, for a memfd map, with "new_from_fd" on a
"$map->memfd" taken before the thread starts). "Storable" croaks on a
handle; "Clone" makes a second object that frees the map under the first.
"new_memfd" creates an unlinked memfd-backed map whose descriptor can be
inherited across "fork" or sent with "SCM_RIGHTS"; $name is only a label
and may be "undef". The descriptor is close-on-exec: pass
POSIX::dup($map->memfd) across "exec". "new_from_fd" reopens such a
descriptor from a duplicate, so the one you pass stays yours to close.
"$map->memfd" returns the handle's own descriptor; do not close it.
$max_size enables LRU eviction: an insert at $max_size entries evicts the
least recently used (clock/second-chance; an eviction spares at most 64
recently read entries). 0 disables it. A $max_size at or above the table's
slot count (2048 for $max_entries 1000) can never be reached, and the
constructor warns (category "misc").
$ttl sets the default time-to-live in seconds (0 disables it). An expired
entry is invisible to reads but keeps its slot, and counts in "size",
until the next write to that key, a flush ("flush_expired",
"flush_expired_partial"), or an insert that needs room: an insert flushes
every expired entry at once when the table or arena is full or the table
passes its design load. TTLs are whole seconds, truncated, so a TTL of "n"
expires between "n-1" and "n" seconds later: refresh a heartbeat at least
two seconds before its TTL. Every store resets an entry's TTL to the map
default (a permanent entry stays permanent); "put_ttl", "update_ttl" and
"set_ttl" set a per-key one. Expiry follows "CLOCK_MONOTONIC_COARSE",
which restarts on reboot: TTLs do not survive a reboot or a move to
another host.
$lru_skip (0-99) reduces LRU promotion on updates to a power-of-two rate:
below 50 every update promotes, 50 one in two, 90 one in sixteen, 99 one
in 128. Reads never promote; they set the clock bit eviction consults. It
pays off only on Zipfian write workloads; leave it at 0 otherwise.
$arena_cap sizes the string arena in bytes (default about 128 per entry,
clamped to 4096 .. 0xFFFFFFFF; per shard; ignored by integer-only
( run in 1.583 second using v1.01-cache-2.11-cpan-60884a8df76 )