31#include <gtest/gtest.h>
48#include <unordered_set>
68 double min_uniqueness = 0.0;
69 double max_random_chi = std::numeric_limits<double>::infinity();
70 double max_prefix_chi = std::numeric_limits<double>::infinity();
71 double max_sequential_chi = std::numeric_limits<double>::infinity();
72 double min_avalanche = 0.0;
73 double max_bit_bias = std::numeric_limits<double>::infinity();
78 const char * name =
"";
79 HashProfile profile = HashProfile::Weak;
81 std::function<size_t(
const std::string &)> hash;
87 size_t (* fast_hash)(
const std::string &) =
nullptr;
92 double uniqueness = 0.0;
93 double random_chi = 0.0;
94 double prefix_chi = 0.0;
95 double sequential_chi = 0.0;
96 double avalanche = 0.0;
97 double max_bit_bias = 0.0;
98 double throughput_mib_s = 0.0;
99 size_t full_collisions = 0;
100 size_t max_random_bucket = 0;
101 uint64_t total_calls = 0;
102 double latency_ns = 0.0;
103 size_t output_bits = 0;
106Thresholds thresholds_for(HashProfile profile)
110 case HashProfile::Weak:
112 case HashProfile::General:
113 return {.min_uniqueness = 0.970,
114 .max_random_chi = 4.25,
115 .max_prefix_chi = 6.50,
116 .max_sequential_chi = 5.50,
117 .min_avalanche = 0.34,
118 .max_bit_bias = 0.15};
119 case HashProfile::Strong:
120 return {.min_uniqueness = 0.995,
121 .max_random_chi = 2.60,
122 .max_prefix_chi = 3.40,
123 .max_sequential_chi = 3.20,
124 .min_avalanche = 0.42,
125 .max_bit_bias = 0.09};
137 std::uniform_int_distribution<size_t> len_dist(min_len, max_len);
138 std::uniform_int_distribution<int> byte_dist(0, 255);
141 std::unordered_set<std::string> seen;
142 seen.reserve(
count * 2);
146 const size_t len = len_dist(
rng);
147 std::string sample(len,
'\0');
148 for (
size_t j = 0; j < len; ++j)
149 sample[j] =
static_cast<char>(byte_dist(
rng));
150 if (seen.insert(sample).second)
151 out.append(std::move(sample));
163 const std::string
prefix(prefix_len,
'A');
165 for (
size_t i = 0; i <
count; ++i)
167 std::string sample =
prefix;
170 const std::uint64_t v =
static_cast<std::uint64_t
>(i);
171 for (
size_t j = 0; j < suffix_len; ++j)
172 sample.push_back(
static_cast<char>((v >> (j * 8)) & 0xff));
173 out.append(std::move(sample));
184 for (
size_t i = 0; i <
count; ++i)
186 std::string sample(
sizeof(std::uint64_t),
'\0');
187 std::uint64_t
value =
static_cast<std::uint64_t
>(i);
188 for (
size_t j = 0; j <
sizeof(
value); ++j)
189 sample[j] =
static_cast<char>((
value >> (j * 8)) & 0xff);
190 out.append(std::move(sample));
199 return make_random_binary_samples(
count, len, len,
seed);
202std::pair<double, size_t> reduced_chi_square(
const Candidate & candidate,
207 for (
const auto & sample : samples)
208 ++counts[candidate.hash(sample) % buckets];
210 const double expected =
static_cast<double>(samples.
size()) / buckets;
212 size_t max_bucket = 0;
213 for (
const size_t value : counts)
217 max_bucket = std::max(max_bucket,
value);
220 return {chi /
static_cast<double>(buckets - 1), max_bucket};
223double uniqueness_ratio(
const Candidate & candidate,
227 std::unordered_set<size_t>
unique;
230 for (
const auto & sample : samples)
234 return static_cast<double>(
unique.size()) / samples.
size();
237size_t effective_output_bits(
const Candidate & candidate,
240 size_t observed_mask = 0;
242 for (
const auto & sample : samples)
243 observed_mask |= candidate.hash(sample);
245 const size_t bits = std::bit_width(observed_mask);
246 return std::max<size_t>(
bits, 1);
249double max_output_bit_bias(
const Candidate & candidate,
252 const size_t bits = effective_output_bits(candidate, samples);
253 std::array<size_t,
sizeof(size_t) * 8> ones = {};
255 for (
const auto & sample : samples)
257 const size_t hash = candidate.hash(sample);
258 for (
size_t bit = 0; bit <
bits; ++bit)
259 ones[bit] += (hash >> bit) &
size_t{1};
266 double max_bias = 0.0;
267 for (
size_t bit = 0; bit <
bits; ++bit)
269 const double ratio =
static_cast<double>(ones[bit]) / samples.
size();
270 max_bias = std::max(max_bias, std::abs(ratio - 0.5));
276double avalanche_ratio(
const Candidate & candidate,
283 const size_t output_bits = effective_output_bits(candidate, samples);
284 std::uint64_t changed_bits = 0;
285 std::uint64_t comparisons = 0;
287 for (
const auto & base : samples)
289 const size_t original = candidate.hash(base);
290 const size_t flip_bits = std::min<size_t>(base.size() * 8, 64);
292 for (
size_t bit = 0; bit < flip_bits; ++bit)
294 std::string mutated = base;
295 mutated[bit / 8] ^=
static_cast<char>(1u << (bit % 8));
296 const size_t hashed = candidate.hash(mutated);
297 changed_bits += std::popcount(original ^ hashed);
298 comparisons += output_bits;
302 return comparisons == 0 ? 0.0
303 :
static_cast<double>(changed_bits) / comparisons;
306struct ThroughputResult
310 double latency_ns = 0.0;
313ThroughputResult throughput_metrics(
const Candidate & candidate,
319 const bool use_fast = (candidate.fast_hash !=
nullptr);
321 constexpr std::uint64_t target_bytes = 128ULL * 1024 * 1024;
322 std::uint64_t processed = 0;
324 volatile size_t sink = 0;
326 const auto start = std::chrono::steady_clock::now();
327 while (processed < target_bytes)
329 for (
const auto & sample : samples)
331 sink ^= use_fast ? candidate.fast_hash(sample) : candidate.hash(sample);
332 processed += sample.size();
334 if (processed >= target_bytes)
338 const auto stop = std::chrono::steady_clock::now();
339 const std::chrono::duration<double> elapsed = stop - start;
342 ThroughputResult res;
343 res.mib_s =
static_cast<double>(processed) / (1024.0 * 1024.0) / elapsed.count();
345 res.latency_ns = (std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count()) /
static_cast<double>(calls);
349Metrics measure_metrics(
const Candidate & candidate,
356 metrics.uniqueness = uniqueness_ratio(candidate, random_samples,
357 metrics.full_collisions);
358 metrics.total_calls += random_samples.
size();
360 const auto [random_chi, max_random_bucket]
361 = reduced_chi_square(candidate, random_samples, 1024);
362 metrics.random_chi = random_chi;
363 metrics.max_random_bucket = max_random_bucket;
364 metrics.total_calls += random_samples.
size();
366 metrics.prefix_chi = reduced_chi_square(candidate, prefix_samples, 1024).first;
367 metrics.total_calls += prefix_samples.
size();
369 metrics.sequential_chi = reduced_chi_square(candidate, sequential_samples, 1024).first;
370 metrics.total_calls += sequential_samples.
size();
372 metrics.avalanche = avalanche_ratio(candidate, avalanche_samples);
373 const size_t flip_bits = std::min<size_t>(avalanche_samples[0].
size() * 8, 64);
374 metrics.total_calls += avalanche_samples.
size() * (1 + flip_bits);
376 metrics.max_bit_bias = max_output_bit_bias(candidate, random_samples);
377 metrics.output_bits = effective_output_bits(candidate, random_samples);
378 metrics.total_calls += random_samples.
size();
380 const auto tp = throughput_metrics(candidate, random_samples);
381 metrics.throughput_mib_s = tp.mib_s;
382 metrics.latency_ns = tp.latency_ns;
383 metrics.total_calls += tp.calls;
393 size_t add (
const std::string & s) {
return add_hash(s.data(), s.size()); }
394 size_t xor_ (
const std::string & s) {
return xor_hash(s.data(), s.size()); }
395 size_t rot (
const std::string & s) {
return rot_hash(s.data(), s.size()); }
396 size_t djb (
const std::string & s) {
return djb_hash(s.data(), s.size()); }
397 size_t sax (
const std::string & s) {
return sax_hash(s.data(), s.size()); }
398 size_t fnv (
const std::string & s) {
return fnv_hash(s.data(), s.size()); }
399 size_t oat (
const std::string & s) {
return oat_hash(s.data(), s.size()); }
400 size_t jsw (
const std::string & s) {
return jsw_hash(s.data(), s.size()); }
401 size_t elf (
const std::string & s) {
return elf_hash(s.data(), s.size()); }
402 size_t jen (
const std::string & s)
404 size_t sfh (
const std::string & s) {
return SuperFastHash(s); }
405 size_t murmur3 (
const std::string & s) {
return murmur3hash(s, 42ul); }
406 size_t xxh64 (
const std::string & s) {
return xxhash64_hash(s.data(), s.size(), 42); }
407 size_t wyh (
const std::string & s) {
return wyhash_hash(s.data(), s.size(), 42); }
408 size_t sip24 (
const std::string & s)
409 {
return siphash24_hash(
static_cast<const void *
>(s.data()), s.size()); }
416 {
"add_hash", HashProfile::Weak, add, add},
417 {
"xor_hash", HashProfile::Weak, xor_, xor_},
418 {
"rot_hash", HashProfile::Weak, rot, rot},
419 {
"djb_hash", HashProfile::General, djb, djb},
420 {
"sax_hash", HashProfile::Weak, sax, sax},
421 {
"fnv_hash", HashProfile::General, fnv, fnv},
422 {
"oat_hash", HashProfile::General, oat, oat},
423 {
"jsw_hash", HashProfile::Weak, jsw, jsw},
424 {
"elf_hash", HashProfile::Weak, elf, elf},
425 {
"jen_hash", HashProfile::Weak, jen, jen},
426 {
"SuperFastHash", HashProfile::General, sfh, sfh},
427 {
"murmur3hash", HashProfile::Strong, murmur3, murmur3},
428 {
"xxhash64_hash", HashProfile::Strong, xxh64, xxh64},
429 {
"wyhash_hash", HashProfile::Strong, wyh, wyh},
430 {
"siphash24_hash", HashProfile::Strong, sip24, sip24},
434std::string format_metrics_table(
const Array<std::pair<std::string, Metrics>> &
rows)
436 std::ostringstream
out;
437 out <<
"\nHash statistics summary\n";
438 out << std::left << std::setw(16) <<
"name"
439 << std::right << std::setw(5) <<
"bits"
440 << std::setw(8) <<
"uniq"
441 << std::setw(8) <<
"chi-r"
442 << std::setw(8) <<
"chi-p"
443 << std::setw(8) <<
"chi-s"
444 << std::setw(8) <<
"avalan"
445 << std::setw(8) <<
"bias"
446 << std::setw(10) <<
"MiB/s"
447 << std::setw(10) <<
"lat-ns"
448 << std::setw(8) <<
"coll"
451 for (
const auto & [name, metrics] :
rows)
453 out << std::left << std::setw(16) << name
455 << std::setw(5) << metrics.output_bits
456 << std::fixed << std::setprecision(3)
457 << std::setw(8) << metrics.uniqueness
458 << std::setw(8) << metrics.random_chi
459 << std::setw(8) << metrics.prefix_chi
460 << std::setw(8) << metrics.sequential_chi
461 << std::setw(8) << metrics.avalanche
462 << std::setw(8) << metrics.max_bit_bias
463 << std::setw(10) << std::setprecision(1) << metrics.throughput_mib_s
464 << std::setw(10) << std::setprecision(2) << metrics.latency_ns
465 << std::setw(8) << metrics.full_collisions
472class HashStatisticalTest :
public ::testing::Test
475 static void SetUpTestSuite()
482 static const auto samples = make_random_binary_samples(100000, 1, 64, 0x12345678ULL);
488 static const auto samples = make_prefix_samples(100000, 28, 8);
494 static const auto samples = make_sequential_samples(100000);
500 static const auto samples = make_avalanche_samples(1024, 32, 0xabcdefULL);
505TEST_F(HashStatisticalTest, DispersionAvalancheAndThroughputProfiles)
509 for (
const auto & candidate : all_candidates())
511 SCOPED_TRACE(candidate.name);
512 const Metrics metrics = measure_metrics(candidate,
515 sequential_samples(),
516 avalanche_samples());
517 rows.append(std::make_pair(std::string(candidate.name), metrics));
519 if (candidate.profile == HashProfile::Weak)
521 EXPECT_GT(metrics.throughput_mib_s, 0.0);
525 const Thresholds thresholds = thresholds_for(candidate.profile);
526 EXPECT_GE(metrics.uniqueness, thresholds.min_uniqueness)
527 << candidate.name <<
" uniqueness too low";
528 EXPECT_LE(metrics.random_chi, thresholds.max_random_chi)
529 << candidate.name <<
" random chi-square too high";
530 EXPECT_LE(metrics.prefix_chi, thresholds.max_prefix_chi)
531 << candidate.name <<
" shared-prefix chi-square too high";
532 EXPECT_LE(metrics.sequential_chi, thresholds.max_sequential_chi)
533 << candidate.name <<
" sequential chi-square too high";
534 EXPECT_GE(metrics.avalanche, thresholds.min_avalanche)
535 << candidate.name <<
" avalanche ratio too low";
536 EXPECT_LE(metrics.max_bit_bias, thresholds.max_bit_bias)
537 << candidate.name <<
" output-bit bias too high";
540 std::cout << format_metrics_table(
rows) << std::flush;
542 auto dispersion_score = [] (
const Metrics &
m)
544 return std::abs(1.0 -
m.uniqueness) +
545 std::abs(1.0 -
m.random_chi) +
546 std::abs(1.0 -
m.prefix_chi) +
547 std::abs(1.0 -
m.sequential_chi) +
548 std::abs(0.5 -
m.avalanche) +
552 auto dispersion_rows =
rows;
554 [&] (
const auto & a,
const auto & b)
556 return dispersion_score(a.second) < dispersion_score(b.second);
559 std::cout <<
"\nRanking by Dispersion (lower is better):\n";
560 for (
size_t i = 0; i < dispersion_rows.size(); ++i)
561 std::cout << std::setw(2) << i + 1 <<
". "
562 << std::left << std::setw(16) << dispersion_rows[i].first
563 <<
" Score: " << std::fixed << std::setprecision(4)
564 << dispersion_score(dispersion_rows[i].second) <<
"\n";
566 auto speed_rows =
rows;
568 [] (
const auto & a,
const auto & b)
570 return a.second.throughput_mib_s > b.second.throughput_mib_s;
573 std::cout <<
"\nRanking by Speed (higher is better):\n";
574 for (
size_t i = 0; i < speed_rows.size(); ++i)
575 std::cout << std::setw(2) << i + 1 <<
". "
576 << std::left << std::setw(16) << speed_rows[i].first
577 <<
" Speed: " << std::fixed << std::setprecision(1) << std::right << std::setw(8)
578 << speed_rows[i].second.throughput_mib_s <<
" MiB/s ("
579 << std::fixed << std::setprecision(2) << std::setw(6)
580 << speed_rows[i].second.latency_ns <<
" ns/op)\n";
583TEST_F(HashStatisticalTest, PairHashCombinersReportOrderingSensitivity)
585 size_t swapped_pair_dft_collisions = 0;
586 size_t swapped_pair_snd_collisions = 0;
588 for (std::uint64_t i = 1; i <= 4096; ++i)
590 const auto lhs = std::make_pair(i, i + 1);
591 const auto rhs = std::make_pair(i + 1, i);
593 ++swapped_pair_dft_collisions;
595 ++swapped_pair_snd_collisions;
598 std::cout <<
"\npair_dft swapped collisions: " << swapped_pair_dft_collisions
599 <<
"\npair_snd swapped collisions: " << swapped_pair_snd_collisions
604 EXPECT_LE(swapped_pair_dft_collisions, 41u);
605 EXPECT_LE(swapped_pair_snd_collisions, 41u);
608TEST_F(HashStatisticalTest, MapHashAdaptorTracksTheKeyHasherExactly)
610 const std::pair<std::string, int> item{
"aleph", 42};
611 const auto dft = map_hash_fct<std::string, int>(
612 [] (
const std::string & key) {
return dft_hash_fct(key); }, item);
613 const auto snd = map_hash_fct<std::string, int>(
614 [] (
const std::string & key) {
return snd_hash_fct(key); }, item);
615 const auto xxh = map_hash_fct<std::string, int>(
616 [] (
const std::string & key) {
return xxhash64_hash(key); }, item);
640TEST_F(HashStatisticalTest, CollisionScalingByBitWidth)
643 const std::array<size_t, 6> sizes = {1'000, 10'000, 65'536, 100'000,
647 auto expected_collisions = [] (
size_t n,
size_t bits) ->
double
650 const double log2_space =
static_cast<double>(
bits);
651 const double log2_N2 = std::log2(
static_cast<double>(n)) +
652 std::log2(
static_cast<double>(n - 1));
653 const double log2_expected = log2_N2 - 1.0 - log2_space;
654 return (log2_expected > 50.0) ? std::pow(2.0, std::min(log2_expected, 60.0))
655 :
std::
pow(2.0, log2_expected);
658 std::cout <<
"\nCollision scaling: 32-bit vs 64-bit output (wyhash_hash)\n"
659 << std::left << std::setw(10) <<
"N"
660 << std::right << std::setw(14) <<
"32b-collisions"
661 << std::setw(14) <<
"32b-expected"
662 << std::setw(14) <<
"64b-collisions"
663 << std::setw(14) <<
"64b-expected"
666 for (
const size_t n : sizes)
668 auto samples = make_random_binary_samples(n, 8, 32, 0xdeadbeefULL + n);
670 std::unordered_set<std::uint32_t> h32;
671 std::unordered_set<std::uint64_t> h64;
675 for (
const auto & s : samples)
677 const std::uint64_t v =
wyhash_hash(s.data(), s.size(), 42);
678 h32.insert(
static_cast<std::uint32_t
>(v));
682 const size_t coll32 = n - h32.size();
683 const size_t coll64 = n - h64.size();
684 const double exp32 = expected_collisions(n, 32);
685 const double exp64 = expected_collisions(n, 64);
687 std::cout << std::left << std::setw(10) << n
689 << std::setw(14) << coll32
690 << std::setw(14) << std::fixed << std::setprecision(2) << exp32
691 << std::setw(14) << coll64
692 << std::setw(14) << std::fixed << std::setprecision(6) << exp64
698 if constexpr (
sizeof(size_t) >= 8)
699 EXPECT_EQ(coll64, 0u) <<
"64-bit hash unexpectedly collided at N=" << n;
703 const double N4G = 4e9;
704 const double exp32_4G = N4G * (N4G - 1.0) / (2.0 * std::pow(2.0, 32));
705 const double exp64_4G = N4G * (N4G - 1.0) / (2.0 * std::pow(2.0, 64));
707 std::cout <<
"\nExtrapolation to N = 4 × 10^9:\n"
708 <<
" 32-bit expected collisions : " << std::scientific
709 << std::setprecision(3) << exp32_4G <<
" (~50 % of N)\n"
710 <<
" 64-bit expected collisions : " << exp64_4G
711 <<
" (negligible)\n";
714 EXPECT_GT(exp32_4G, N4G * 0.1)
715 <<
"32-bit hash should show >10 % collision rate at 4 G entries";
716 EXPECT_LT(exp64_4G, 1.0)
717 <<
"64-bit hash should show < 1 expected collision at 4 G entries";
TEST_F(AdaptiveHuffmanWithoutMemory, ALostByteLeavesTheDecoderFailed)
High-level sorting functions for Aleph containers.
size_t size_t int32_t value
size_t size_t int32_t * out
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
void reserve(size_t cap)
Reserves cap cells into the array.
Minimal std::expected-style result type for C++20.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_pow_function > > pow(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
size_t sax_hash(const void *key, const size_t len) noexcept
Shift-Add-XOR hash (SAX hash)
size_t add_hash(const void *key, const size_t len) noexcept
Additive hash.
size_t jsw_hash(const void *key, size_t len) noexcept
JSW hash (Julienne Walker)
size_t xxhash64_hash(const void *key, size_t len, std::uint64_t seed) noexcept
xxHash64 from the xxHash family.
size_t djb_hash(const void *key, const size_t len) noexcept
Bernstein's hash (DJB hash)
size_t jen_hash(const void *key, size_t length, unsigned initval) noexcept
Jenkins hash (lookup3)
size_t xor_hash(const void *key, const size_t len) noexcept
XOR hash.
size_t SuperFastHash(const void *key, const size_t len, std::uint32_t seed=0) noexcept
Paul Hsieh super fast hash function.
size_t fnv_hash(const void *key, const size_t len) noexcept
FNV-1a hash.
size_t siphash24_hash(const void *key, size_t len, std::uint64_t key0, std::uint64_t key1) noexcept
SipHash-2-4 keyed hash.
void init_jsw() noexcept
Initializes the randomized lookup table used by jsw_hash().
size_t rot_hash(const void *key, const size_t len) noexcept
Rotating hash.
size_t wyhash_hash(const void *key, size_t len, std::uint64_t seed) noexcept
wyhash non-cryptographic hash.
size_t elf_hash(const void *key, const size_t len) noexcept
ELF hash.
size_t oat_hash(const void *key, const size_t len) noexcept
One-at-a-Time hash (OAT hash)
Standard hash functions for Aleph types.
Main namespace for Aleph-w library functions.
size_t pair_snd_hash_fct(const std::pair< K1, K2 > &p) noexcept
Itor unique(Itor __first, Itor __last, BinaryPredicate __binary_pred=BinaryPredicate())
Remove consecutive duplicates in place.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
size_t snd_hash_fct(const Key &key) noexcept
Secondary default hash: different distribution from dft_hash_fct.
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
DynArray< T > & in_place_sort(DynArray< T > &c, Cmp cmp=Cmp())
Sorts a DynArray in place.
static void prefix(Node *root, DynList< Node * > &acc)
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
size_t dft_hash_fct(const Key &key) noexcept
Primary default hash: best speed/quality trade-off.
size_t pair_dft_hash_fct(const std::pair< K1, K2 > &p) noexcept
const unsigned Default_Hash_Seed
size_t murmur3hash(const Key &key, std::uint32_t seed)
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic array container with automatic resizing.