Skip to content

Add the first 31 bytes to the hash of long strings #1581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/core/json/include/sourcemeta/core/json_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ template <typename T> struct PropertyHashJSON {
-> hash_type {
hash_type result;
assert(!value.empty());
assert(value.size() <= 31);
// Copy starting a byte 2
std::memcpy(reinterpret_cast<char *>(&result) + 1, value.data(), size);
return result;
Expand Down Expand Up @@ -126,17 +125,19 @@ template <typename T> struct PropertyHashJSON {
// This case is specifically designed to be constant with regards to
// string length, and to exploit the fact that most JSON objects don't
// have a lot of entries, so hash collision is not as common
return {(size + static_cast<typename hash_type::type>(value.front()) +
static_cast<typename hash_type::type>(value.back())) %
// Make sure the property hash can never exceed 8 bits
256};
auto hash = this->perfect(value, 31);
hash.a |= (size + static_cast<typename hash_type::type>(value.front()) +
static_cast<typename hash_type::type>(value.back())) %
// Make sure the property hash can never exceed 8 bits
256;
return hash;
}
}

inline auto is_perfect(const hash_type &hash) const noexcept -> bool {
// If there is anything written past the first byte,
// then it is a perfect hash
return hash.a > 255;
return (hash.a & 255) == 0;
}
};

Expand Down
14 changes: 7 additions & 7 deletions test/json/json_hash_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ TEST(JSON_key_hash, hash_empty) {
hasher;
const sourcemeta::core::JSON::String value{""};
const auto hash{hasher(value)};
EXPECT_FALSE(hasher.is_perfect(hash));
EXPECT_TRUE(hasher.is_perfect(hash));
#if defined(__SIZEOF_INT128__)
EXPECT_EQ(hash.a,
(__uint128_t{0x0000000000000000} << 64) | 0x0000000000000000);
Expand Down Expand Up @@ -619,14 +619,14 @@ TEST(JSON_key_hash, hash_fooooooooooooooooooooooooooooooo) {
EXPECT_FALSE(hasher.is_perfect(hash));
#if defined(__SIZEOF_INT128__)
EXPECT_EQ(hash.a,
(__uint128_t{0x0000000000000000} << 64) | 0x00000000000000f5);
(__uint128_t{0x6f6f6f6f6f6f6f6f} << 64) | 0x6f6f6f6f6f6f66f5);
EXPECT_EQ(hash.b,
(__uint128_t{0x0000000000000000} << 64) | 0x0000000000000000);
(__uint128_t{0x6f6f6f6f6f6f6f6f} << 64) | 0x6f6f6f6f6f6f6f6f);
#else
// 0x20 (length) + 0x66 (f) + 0x6f (o)
EXPECT_EQ(hash.a, 0x00000000000000f5);
EXPECT_EQ(hash.b, 0x0000000000000000);
EXPECT_EQ(hash.c, 0x0000000000000000);
EXPECT_EQ(hash.d, 0x0000000000000000);
EXPECT_EQ(hash.a, 0x6f6f6f6f6f6f66f5);
EXPECT_EQ(hash.b, 0x6f6f6f6f6f6f6f6f);
EXPECT_EQ(hash.c, 0x6f6f6f6f6f6f6f6f);
EXPECT_EQ(hash.d, 0x6f6f6f6f6f6f6f6f);
#endif
}