String: Use rapidhash for hashing

Rapidhash is a better hash function than FNV-1a (also used in some stl
implementations): it is faster and has a reduced number of hash
collisions.

Closes: #27937
This commit is contained in:
Tim Blechmann
2026-07-10 09:18:21 +07:00
committed by Brad King
parent bc23835e45
commit ed7a690e83
2 changed files with 6 additions and 12 deletions

View File

@@ -16,6 +16,8 @@
#include <cm/string_view>
#include <cmext/string_view>
#include <cm3p/rapidhash.h>
namespace cm {
class String;
@@ -961,8 +963,8 @@ struct hash<cm::String>
result_type operator()(argument_type const& s) const noexcept
{
result_type const h(std::hash<cm::string_view>{}(s.view()));
return h;
cm::string_view const v = s.view();
return static_cast<result_type>(rapidhash(v.data(), v.size()));
}
};
}

View File

@@ -9,7 +9,7 @@
# include <ostream>
# include <stdexcept>
# include <cm3p/kwiml/int.h>
# include <cm3p/rapidhash.h>
namespace cm {
@@ -282,15 +282,7 @@ std::string& operator+=(std::string& s, string_view v)
std::hash<cm::string_view>::result_type std::hash<cm::string_view>::operator()(
argument_type const& s) const noexcept
{
// FNV-1a hash.
static KWIML_INT_uint64_t const fnv_offset_basis = 0xcbf29ce484222325;
static KWIML_INT_uint64_t const fnv_prime = 0x100000001b3;
KWIML_INT_uint64_t h = fnv_offset_basis;
for (char const& c : s) {
h = h ^ KWIML_INT_uint64_t(KWIML_INT_uint8_t(c));
h = h * fnv_prime;
}
return result_type(h);
return result_type(rapidhash(s.data(), s.size()));
}
#else
// Avoid empty translation unit.