Data-HashMap
view release on metacpan or search on metacpan
Revision history for Data-HashMap
0.11 2026-10-07
- Fix UTF-8 keys imported from tied hashes
- Keep remaining entries reachable during reentrant clear
- Trim and correct the documentation
0.10 2026-10-06
- Stricter input handling: NaN, out-of-range numbers and oversized
strings croak; tied arguments are read once
- Fix reentrancy and iteration bugs across each, clear, from_hash,
merge and new/thaw; bounded maps keep a small table
- POD: iteration, LRU, TTL and coercion rules clarified
0.09 2026-09-11
- Per-process hash seed; key order and freeze output vary between runs
- SV* variants: opt-in value copying (fourth new() argument)
- Maps are not cloned into new threads; maps made in a thread work
- Storable support for non-SV* variants; SV* variants croak
static void croak_i64(SV* sv) {
dTHX;
STRLEN len;
const char* p = SvPV_nomg(sv, len);
Perl_croak(aTHX_ "%.*s out of int64 range", (int)len, p);
}
static IV hm_sv_to_int(pTHX_ SV* sv, IV lo, IV hi, int width) {
/* SvIV first: a key arrives as a string, and the flags that show it overflowed appear only after conversion. */
IV v = SvIV(sv);
/* Before the IsUV test: SvIV may turn NaN into UV 0. */
if (SvNOKp(sv)) {
NV nv = SvNV_nomg(sv);
if (nv != nv) croak("cannot use NaN where an integer is expected");
if (nv < (NV)lo || nv > (NV)hi) goto bad;
}
if (SvIsUV(sv)) {
UV u = SvUV_nomg(sv);
if (u > (UV)hi) goto bad;
return (IV)u;
}
if (v < lo || v > hi) goto bad;
/* A string whose integer value overflows IV clamps silently to lo/hi; a
double-NV perl then cannot tell -2^63-1 from -2^63. Re-check precisely. */
if (SvROK(sv) || (SvOK(sv) && !looks_like_number(sv))) {
STRLEN len;
const char* p = SvPV_nomg(sv, len);
croak("%s must be a number, got '%.*s'", what, (int)len, p);
}
}
static uint32_t hm_ttl_arg(pTHX_ SV* sv) {
SvGETMAGIC(sv);
hm_num_arg(aTHX_ sv, "ttl");
NV nv = SvNV_nomg(sv);
if (nv != nv) croak("ttl must be a number, got NaN");
if (nv < 0) croak("ttl must be non-negative, got %" NVgf, nv);
if (nv > 0 && nv < 1) return 1; /* 0 means no TTL; a fraction must not truncate to it */
UV v = SvUV_nomg(sv);
return v > UINT32_MAX ? UINT32_MAX : (uint32_t)v;
}
static size_t hm_size_arg(pTHX_ SV* sv, const char* what) {
SvGETMAGIC(sv);
hm_num_arg(aTHX_ sv, what);
NV nv = SvNV_nomg(sv);
if (nv != nv) croak("%s must be a number, got NaN", what);
if (nv < 0) croak("%s must be non-negative, got %" NVgf, what, nv);
UV v = SvUV_nomg(sv);
#if UVSIZE > Size_t_size
if (v > (UV)SIZE_MAX) return SIZE_MAX;
#endif
return (size_t)v;
}
static UV hm_uv_arg(pTHX_ SV* sv) {
SvGETMAGIC(sv);
if (SvNOKp(sv) || SvPOKp(sv)) {
NV nv = SvNV_nomg(sv);
if (!(nv == nv)) croak("NaN is not a valid count");
}
return SvUV_nomg(sv);
}
/* ---- new() arguments ---- */
#define HM_NEW_ARGS(max_size_var, ttl_var, lru_skip_var, nargs, names) \
size_t max_size_var = 0; \
uint32_t ttl_var = 0; \
uint32_t lru_skip_var = 0; \
"$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.
lib/Data/HashMap.pm view on Meta::CPAN
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.
t/22-bugfixes.t view on Meta::CPAN
}
undef $ReinsClearMap;
package DropMap { sub DESTROY { undef $main::DropMap } }
our $DropMap = Data::HashMap::SA->new;
$DropMap->put("k$_", bless {}, 'DropMap') for 1 .. 8;
hm_sa_clear $DropMap;
ok(!defined $DropMap, 'clear survives a DESTROY that frees the map');
}
# ---- NaN croaks wherever a number is expected ----
{
my $nan = 0 + "nan";
my $m = Data::HashMap::II->new();
eval { hm_ii_put $m, $nan, 1 };
like($@, qr/cannot use NaN/, 'II put: NaN key croaks');
eval { hm_ii_put $m, 1, $nan };
like($@, qr/cannot use NaN/, 'II put: NaN value croaks');
eval { hm_ii_put $m, "nan", 1 };
like($@, qr/cannot use NaN/, 'II put: "nan" string key croaks');
eval { Data::HashMap::II->new(0, $nan) };
like($@, qr/ttl must be a number/, 'II new: NaN ttl croaks');
eval { Data::HashMap::II->new($nan) };
like($@, qr/max_size must be a number/, 'II new: NaN max_size croaks');
eval { Data::HashMap::II->new(2, 0, $nan) };
like($@, qr/lru_skip must be a number/, 'II new: NaN lru_skip croaks');
eval { hm_ii_put_ttl $m, 2, 2, $nan };
like($@, qr/ttl must be a number/, 'II put_ttl: NaN ttl croaks');
eval { hm_ii_reserve $m, $nan };
like($@, qr/NaN is not a valid count/, 'II reserve: NaN count croaks');
eval { hm_ii_drain $m, $nan };
like($@, qr/NaN is not a valid count/, 'II drain: NaN count croaks');
eval { hm_ii_incr_by $m, 1, $nan };
like($@, qr/cannot use NaN/, 'II incr_by: NaN delta croaks');
my $saturated = Data::HashMap::II->new(0, 9**9**9);
is($saturated->ttl, 4294967295, 'II new: Inf ttl saturates to UINT32_MAX');
}
# ---- int conversion reads a tied scalar once, on success and croak paths ----
{
package CountKey;
our ($n, $v);
sub TIESCALAR { bless {}, $_[0] }
sub FETCH { $n++; $v }
t/22-bugfixes.t view on Meta::CPAN
like($@, qr/out of int16 range/, 'I16 put: tied out-of-range key croaks');
is($CountKey::n, 1, 'I16 put: croak path reads tied key once');
$CountKey::v = 1e30; $CountKey::n = 0;
eval { hm_i16_put $m16, $t, 1 };
like($@, qr/out of int16 range/, 'I16 put: tied NV out-of-range key croaks');
is($CountKey::n, 1, 'I16 put: NV croak path reads tied key once');
$CountKey::v = 'nan';
eval { hm_ii_put $m, $t, 1 };
like($@, qr/cannot use NaN/, 'II put: tied NaN key croaks');
$CountKey::v = 5;
ok(eval { hm_ii_put $m, $t, 1; 1 }, 'II put: a tied key judged on its new value, not the last FETCH');
}
# ---- new/thaw on an instance use its real class, not a stringified ref ----
{
my $m = Data::HashMap::II->new;
$m->put(1, 10);
my $blob = $m->freeze;
( run in 1.209 second using v1.01-cache-2.11-cpan-60884a8df76 )