Data-HashMap-Shared

 view release on metacpan or  search on metacpan

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

cross-process B<cache>; lookups take a lock-free seqlock path.

B<Linux-only>. Requires 64-bit Perl on a little-endian architecture.

=head2 Features

=over

=item * File-backed mmap for cross-process sharing

=item * Futex-based read-write lock (fast userspace path)

=item * Atomic counters (incr/decr under the read lock on maps without LRU or TTL)

=item * Elastic capacity (starts small, grows/shrinks automatically)

=item * Arena allocator for string storage in shared memory

=item * Keyword API via XS::Parse::Keyword for maximum speed

=item * Opt-in B<LRU eviction> -- clock/second-chance algorithm; reads stay lock-free

=item * Opt-in B<per-key TTL> expiry -- lazy removal on access; monotonic clock

=item * Stale lock recovery for both writers and readers (dead PIDs detected and drained automatically)

=back

=head2 Variants

=over

=item L<Data::HashMap::Shared::I16> - int16 to int16

=item L<Data::HashMap::Shared::I32> - int32 to int32

=item L<Data::HashMap::Shared::II> - int64 to int64

=item L<Data::HashMap::Shared::I16S> - int16 to string

=item L<Data::HashMap::Shared::I32S> - int32 to string

=item L<Data::HashMap::Shared::IS> - int64 to string

=item L<Data::HashMap::Shared::SI16> - string to int16

=item L<Data::HashMap::Shared::SI32> - string to int32

=item L<Data::HashMap::Shared::SI> - string to int64

=item L<Data::HashMap::Shared::SS> - string to string

=back

=head2 Integer Range and Wrapping

Integer keys and values are fixed-width two's complement: 16-bit for
C<I16>/C<SI16>/C<I16S>, 32-bit for C<I32>/C<SI32>/C<I32S>, 64-bit for
C<II>/C<IS>/C<SI>. A number outside the variant's range is B<silently
truncated> to its low bits: on an C<I16> map C<70000> is key C<4464>. Numbers
beyond 64 bits saturate first (C<1e20> becomes C<-1>, NaN C<0>). C<incr> and
C<decr> wrap the same way. Pick a variant wide enough for your data.

=head2 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 C<$path>; C<undef> creates an anonymous
mapping shared only with C<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 C<new_from_fd> on a
C<< $map->memfd >> taken before the thread starts). C<Storable> croaks on a
handle; C<Clone> makes a second object that frees the map under the first.

C<new_memfd> creates an unlinked memfd-backed map whose descriptor can be
inherited across C<fork> or sent with C<SCM_RIGHTS>; C<$name> is only a label
and may be C<undef>. The descriptor is close-on-exec: pass
C<< POSIX::dup($map->memfd) >> across C<exec>. C<new_from_fd> reopens such a
descriptor from a duplicate, so the one you pass stays yours to close.
C<< $map->memfd >> returns the handle's own descriptor; do not close it.

C<$max_size> enables LRU eviction: an insert at C<$max_size> entries evicts the
least recently used (clock/second-chance; an eviction spares at most 64
recently read entries). 0 disables it. A C<$max_size> at or above the table's
slot count (2048 for C<$max_entries> 1000) can never be reached, and the
constructor warns (category C<misc>).

C<$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 C<size>, until
the next write to that key, a flush (C<flush_expired>,
C<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 C<n> expires between
C<n-1> and C<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); C<put_ttl>, C<update_ttl> and C<set_ttl> set a per-key one.
Expiry follows C<CLOCK_MONOTONIC_COARSE>, which restarts on reboot: TTLs do not
survive a reboot or a move to another host.

C<$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.

C<$arena_cap> sizes the string arena in bytes (default about 128 per entry,
clamped to 4096 .. 0xFFFFFFFF; per shard; ignored by integer-only variants).
Keys and values of 7 bytes or fewer are stored inline and need no arena; one



( run in 2.373 seconds using v1.01-cache-2.11-cpan-60884a8df76 )