Files
carbon-lang/common/raw_hashtable_test_helpers.h
T
Chandler Carruthandjosh11b 21a81bc59e Introduce custom hash table data structures. (#3940)
The hash table design is heavily based on Abseil's ["Swiss
Tables"][swiss-tables] design. It uses an array of bytes storing
metadata about each entry and an array of entries where each is a pair
of key and value. The metadata byte consists of 7-bits of hash of the
key (distinct from the bits used to index the table), and one bit
indicating the presence of a special entry -- either empty or deleted.

[swiss-tables]: https://abseil.io/about/design/swisstables

There are a large range of optimizations and other nuanced aspects of
this hash table design and implementation, a good point to understand
that context is `raw_hashtable.h` which has an overview of the design
and references to various other files for relevant details.

---------

Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
2024-06-08 01:50:02 +00:00

116 lines
3.6 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_COMMON_RAW_HASHTABLE_TEST_HELPERS_H_
#define CARBON_COMMON_RAW_HASHTABLE_TEST_HELPERS_H_
#include <compare>
#include "common/check.h"
#include "common/hashing.h"
#include "common/hashtable_key_context.h"
#include "common/ostream.h"
namespace Carbon::RawHashtable {
// Non-trivial type for testing.
struct TestData : Printable<TestData> {
int value;
// NOLINTNEXTLINE: google-explicit-constructor
TestData(int v) : value(v) { CARBON_CHECK(value >= 0); }
~TestData() {
CARBON_CHECK(value >= 0);
value = -1;
}
TestData(const TestData& other) : TestData(other.value) {}
TestData(TestData&& other) noexcept : TestData(other.value) {
other.value = 0;
}
auto Print(llvm::raw_ostream& out) const -> void { out << value; }
friend auto operator==(TestData lhs, TestData rhs) -> bool {
return lhs.value == rhs.value;
}
friend auto operator<=>(TestData lhs, TestData rhs) -> std::strong_ordering {
return lhs.value <=> rhs.value;
}
friend auto CarbonHashValue(TestData data, uint64_t seed) -> HashCode {
return Carbon::HashValue(data.value, seed);
}
};
// Test stateless key context that produces different hashes from normal.
// Changing the hash values should result in test failures if the context ever
// fails to be used.
struct TestKeyContext : DefaultKeyContext {
template <typename KeyT>
auto HashKey(const KeyT& key, uint64_t seed) const -> HashCode {
Hasher hash(seed);
// Inject some other data to the hash.
hash.Hash(42);
hash.Hash(HashValue(key));
return static_cast<HashCode>(hash);
}
};
// Hostile fixed hashing key context used for stress testing. Allows control
// over which parts of the hash will be forced to collide, and the values they
// are coerced to. Note that this relies on implementation details and internals
// of `HashCode`.
template <int TagBits, bool FixIndexBits, bool FixTagBits, uint64_t FixedVal>
struct FixedHashKeyContext : DefaultKeyContext {
template <typename KeyT>
auto HashKey(const KeyT& key, uint64_t seed) const -> HashCode {
HashCode original_hash = HashValue(key, seed);
auto raw_hash = static_cast<uint64_t>(original_hash);
constexpr uint64_t TagMask = (1U << TagBits) - 1;
if (FixIndexBits) {
raw_hash &= TagMask;
raw_hash |= FixedVal << TagBits;
CARBON_DCHECK(HashCode(raw_hash).ExtractIndexAndTag<TagBits>().first ==
(FixedVal & (~static_cast<uint64_t>(0) >> TagBits)));
}
if (FixTagBits) {
raw_hash &= ~TagMask;
raw_hash |= FixedVal & TagMask;
CARBON_DCHECK(HashCode(raw_hash).ExtractIndexAndTag<TagBits>().second ==
(FixedVal & TagMask));
}
return HashCode(raw_hash);
}
};
template <typename T>
class IndexKeyContext {
public:
explicit IndexKeyContext(llvm::ArrayRef<T> array) : array_(array) {}
auto HashKey(const T& value, uint64_t seed) const -> HashCode {
return HashValue(value, seed);
}
auto HashKey(ssize_t index, uint64_t seed) const -> HashCode {
return HashKey(array_[index], seed);
}
auto KeyEq(const T& lhs, ssize_t rhs_index) const -> bool {
return lhs == array_[rhs_index];
}
auto KeyEq(ssize_t lhs_index, ssize_t rhs_index) const -> bool {
// No need to compare the elements, if the indices are equal, the values
// must be.
return lhs_index == rhs_index;
}
private:
llvm::ArrayRef<T> array_;
};
} // namespace Carbon::RawHashtable
#endif // CARBON_COMMON_RAW_HASHTABLE_TEST_HELPERS_H_