Files
ladybird/AK/HashFunctions.h
Jelle Raaijmakers 1745926fc6 AK+Everywhere: Use MurmurHash3 for int/u64 hashing
Rework our hash functions a bit for significant better performance:

* Rename int_hash to u32_hash to mirror u64_hash.
* Make pair_int_hash call u64_hash instead of multiple u32_hash()es.
* Implement MurmurHash3's fmix32 and fmix64 for u32_hash and u64_hash.

On my machine, this speeds up u32_hash by 20%, u64_hash by ~290%, and
pair_int_hash by ~260%.

We lose the property that an input of 0 results in something that is not
0. I've experimented with an offset to both hash functions, but it
resulted in a measurable performance degradation for u64_hash. If
there's a good use case for 0 not to result in 0, we can always add in
that offset as a countermeasure in the future.
2026-02-20 22:47:24 +01:00

50 lines
975 B
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
// MurmurHash3 32-bit finalizer (fmix32).
constexpr unsigned u32_hash(u32 key)
{
key ^= key >> 16;
key *= 0x85ebca6bU;
key ^= key >> 13;
key *= 0xc2b2ae35U;
key ^= key >> 16;
return key;
}
// MurmurHash3 64-bit finalizer (fmix64).
constexpr unsigned u64_hash(u64 key)
{
key ^= key >> 33;
key *= 0xff51afd7ed558ccdULL;
key ^= key >> 33;
key *= 0xc4ceb9fe1a85ec53ULL;
key ^= key >> 33;
return static_cast<unsigned>(key);
}
constexpr unsigned pair_int_hash(u32 key1, u32 key2)
{
return u64_hash((static_cast<u64>(key1) << 32) | key2);
}
constexpr unsigned ptr_hash(FlatPtr ptr)
{
if constexpr (sizeof(ptr) == 8)
return u64_hash(ptr);
else
return u32_hash(ptr);
}
inline unsigned ptr_hash(void const* ptr)
{
return ptr_hash(FlatPtr(ptr));
}