234 changes: 176 additions & 58 deletions libc/src/__support/HashTable/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "include/llvm-libc-types/ENTRY.h"
#include "src/__support/CPP/bit.h" // bit_ceil
#include "src/__support/CPP/new.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/HashTable/bitmask.h"
#include "src/__support/hash.h"
#include "src/__support/macros/attributes.h"
Expand Down Expand Up @@ -79,7 +78,7 @@ LIBC_INLINE size_t capacity_to_entries(size_t cap) {
// | N * Entry |
// ======================= <- align boundary
// | Header |
// =======================
// ======================= <- align boundary (for fast resize)
// | (N + 1) * Byte |
// =======================
//
Expand All @@ -94,6 +93,20 @@ struct HashTable {
// How many entries are there in the table.
LIBC_INLINE size_t num_of_entries() const { return entries_mask + 1; }

// How many entries can we store in the table before resizing.
LIBC_INLINE size_t full_capacity() const { return num_of_entries() / 8 * 7; }

// The alignment of the whole memory area is the maximum of the alignment
// among the following types:
// - HashTable
// - ENTRY
// - Group
LIBC_INLINE constexpr static size_t table_alignment() {
size_t left_align = alignof(HashTable) > alignof(ENTRY) ? alignof(HashTable)
: alignof(ENTRY);
return left_align > alignof(Group) ? left_align : alignof(Group);
}

LIBC_INLINE bool is_full() const { return available_slots == 0; }

LIBC_INLINE size_t offset_from_entries() const {
Expand All @@ -102,24 +115,30 @@ struct HashTable {
SafeMemSize::offset_to(entries_size, table_alignment());
}

LIBC_INLINE constexpr static size_t table_alignment() {
return alignof(HashTable) > alignof(ENTRY) ? alignof(HashTable)
: alignof(ENTRY);
}

LIBC_INLINE constexpr static size_t offset_to_groups() {
return sizeof(HashTable);
size_t header_size = sizeof(HashTable);
return header_size + SafeMemSize::offset_to(header_size, table_alignment());
}

LIBC_INLINE ENTRY &entry(size_t i) {
return reinterpret_cast<ENTRY *>(this)[-i - 1];
}

LIBC_INLINE const ENTRY &entry(size_t i) const {
return reinterpret_cast<const ENTRY *>(this)[-i - 1];
}

LIBC_INLINE uint8_t &control(size_t i) {
uint8_t *ptr = reinterpret_cast<uint8_t *>(this) + offset_to_groups();
return ptr[i];
}

LIBC_INLINE const uint8_t &control(size_t i) const {
const uint8_t *ptr =
reinterpret_cast<const uint8_t *>(this) + offset_to_groups();
return ptr[i];
}

// We duplicate a group of control bytes to the end. Thus, it is possible that
// we need to set two control bytes at the same time.
LIBC_INLINE void set_ctrl(size_t index, uint8_t value) {
Expand All @@ -128,6 +147,107 @@ struct HashTable {
control(index2) = value;
}

LIBC_INLINE size_t find(const char *key, uint64_t primary) {
uint8_t secondary = secondary_hash(primary);
ProbeSequence sequence{static_cast<size_t>(primary), 0, entries_mask};
while (true) {
size_t pos = sequence.next();
Group ctrls = Group::load(&control(pos));
IteratableBitMask masks = ctrls.match_byte(secondary);
for (size_t i : masks) {
size_t index = (pos + i) & entries_mask;
ENTRY &entry = this->entry(index);
if (LIBC_LIKELY(entry.key != nullptr && strcmp(entry.key, key) == 0))
return index;
}
BitMask available = ctrls.mask_available();
// Since there is no deletion, the first time we find an available slot
// it is also ready to be used as an insertion point. Therefore, we also
// return the first available slot we find. If such entry is empty, the
// key will be nullptr.
if (LIBC_LIKELY(available.any_bit_set())) {
size_t index =
(pos + available.lowest_set_bit_nonzero()) & entries_mask;
return index;
}
}
}

LIBC_INLINE uint64_t oneshot_hash(const char *key) const {
LIBC_NAMESPACE::internal::HashState hasher = state;
hasher.update(key, strlen(key));
return hasher.finish();
}

// A fast insertion routine without checking if a key already exists.
// Nor does the routine check if the table is full.
// This is only to be used in grow() where we insert all existing entries
// into a new table. Hence, the requirements are naturally satisfied.
LIBC_INLINE ENTRY *unsafe_insert(ENTRY item) {
uint64_t primary = oneshot_hash(item.key);
uint8_t secondary = secondary_hash(primary);
ProbeSequence sequence{static_cast<size_t>(primary), 0, entries_mask};
while (true) {
size_t pos = sequence.next();
Group ctrls = Group::load(&control(pos));
BitMask available = ctrls.mask_available();
if (available.any_bit_set()) {
size_t index =
(pos + available.lowest_set_bit_nonzero()) & entries_mask;
set_ctrl(index, secondary);
entry(index).key = item.key;
entry(index).data = item.data;
available_slots--;
return &entry(index);
}
}
}

LIBC_INLINE HashTable *grow() const {
size_t hint = full_capacity() + 1;
HashState state = this->state;
// migrate to a new random state
state.update(&hint, sizeof(hint));
HashTable *new_table = allocate(hint, state.finish());
// It is safe to call unsafe_insert() because we know that:
// - the new table has enough capacity to hold all the entries
// - there is no duplicate key in the old table
if (new_table != nullptr)
for (ENTRY e : *this)
new_table->unsafe_insert(e);
return new_table;
}

LIBC_INLINE static ENTRY *insert(HashTable *&table, ENTRY item,
uint64_t primary) {
auto index = table->find(item.key, primary);
auto slot = &table->entry(index);
// SVr4 and POSIX.1-2001 specify that action is significant only for
// unsuccessful searches, so that an ENTER should not do anything
// for a successful search.
if (slot->key != nullptr)
return slot;

// if table of full, we try to grow the table
if (table->is_full()) {
HashTable *new_table = table->grow();
// allocation failed, return nullptr to indicate failure
if (new_table == nullptr)
return nullptr;
// resized sccuessfully: clean up the old table and use the new one
deallocate(table);
table = new_table;
// it is still valid to use the fastpath insertion.
return table->unsafe_insert(item);
}

table->set_ctrl(index, secondary_hash(primary));
slot->key = item.key;
slot->data = item.data;
table->available_slots--;
return slot;
}

public:
LIBC_INLINE static void deallocate(HashTable *table) {
if (table) {
Expand All @@ -136,6 +256,7 @@ struct HashTable {
operator delete(ptr, std::align_val_t{table_alignment()});
}
}

LIBC_INLINE static HashTable *allocate(size_t capacity, uint64_t randomness) {
// check if capacity_to_entries overflows MAX_MEM_SIZE
if (capacity > size_t{1} << (8 * sizeof(size_t) - 1 - 3))
Expand Down Expand Up @@ -166,68 +287,65 @@ struct HashTable {
return table;
}

private:
LIBC_INLINE size_t find(const char *key, uint64_t primary) {
uint8_t secondary = secondary_hash(primary);
ProbeSequence sequence{static_cast<size_t>(primary), 0, entries_mask};
while (true) {
size_t pos = sequence.next();
Group ctrls = Group::load(&control(pos));
IteratableBitMask masks = ctrls.match_byte(secondary);
for (size_t i : masks) {
size_t index = (pos + i) & entries_mask;
ENTRY &entry = this->entry(index);
if (LIBC_LIKELY(entry.key != nullptr && strcmp(entry.key, key) == 0))
return index;
}
BitMask available = ctrls.mask_available();
// Since there is no deletion, the first time we find an available slot
// it is also ready to be used as an insertion point. Therefore, we also
// return the first available slot we find. If such entry is empty, the
// key will be nullptr.
if (LIBC_LIKELY(available.any_bit_set())) {
size_t index =
(pos + available.lowest_set_bit_nonzero()) & entries_mask;
return index;
}
struct FullTableIterator {
size_t current_offset;
size_t remaining;
IteratableBitMask current_mask;
const HashTable &table;

// It is fine to use remaining to represent the iterator:
// - this comparison only happens with the same table
// - hashtable will not be mutated during the iteration
LIBC_INLINE bool operator==(const FullTableIterator &other) const {
return remaining == other.remaining;
}
LIBC_INLINE bool operator!=(const FullTableIterator &other) const {
return remaining != other.remaining;
}
}

private:
LIBC_INLINE ENTRY *insert(ENTRY item, uint64_t primary) {
auto index = find(item.key, primary);
auto slot = &this->entry(index);
// SVr4 and POSIX.1-2001 specify that action is significant only for
// unsuccessful searches, so that an ENTER should not do anything
// for a successful search.
if (slot->key != nullptr)
return slot;
LIBC_INLINE FullTableIterator &operator++() {
this->ensure_valid_group();
current_mask.remove_lowest_bit();
remaining--;
return *this;
}
LIBC_INLINE const ENTRY &operator*() {
this->ensure_valid_group();
return table.entry(
(current_offset + current_mask.lowest_set_bit_nonzero()) &
table.entries_mask);
}

if (!is_full()) {
set_ctrl(index, secondary_hash(primary));
slot->key = item.key;
slot->data = item.data;
available_slots--;
return slot;
private:
LIBC_INLINE void ensure_valid_group() {
while (!current_mask.any_bit_set()) {
current_offset += sizeof(Group);
// It is ensured that the load will only happen at aligned boundaries.
current_mask =
Group::load_aligned(&table.control(current_offset)).occupied();
}
}
return nullptr;
};

using value_type = ENTRY;
using iterator = FullTableIterator;
iterator begin() const {
return {0, full_capacity() - available_slots,
Group::load_aligned(&control(0)).occupied(), *this};
}
iterator end() const { return {0, 0, {0}, *this}; }

public:
LIBC_INLINE ENTRY *find(const char *key) {
LIBC_NAMESPACE::internal::HashState hasher = state;
hasher.update(key, strlen(key));
uint64_t primary = hasher.finish();
uint64_t primary = oneshot_hash(key);
ENTRY &entry = this->entry(find(key, primary));
if (entry.key == nullptr)
return nullptr;
return &entry;
}
LIBC_INLINE ENTRY *insert(ENTRY item) {
LIBC_NAMESPACE::internal::HashState hasher = state;
hasher.update(item.key, strlen(item.key));
uint64_t primary = hasher.finish();
return insert(item, primary);

LIBC_INLINE static ENTRY *insert(HashTable *&table, ENTRY item) {
uint64_t primary = table->oneshot_hash(item.key);
return insert(table, item, primary);
}
};
} // namespace internal
Expand Down
3 changes: 1 addition & 2 deletions libc/src/search/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ add_entrypoint_object(
DEPENDS
libc.src.search.hsearch.global
libc.src.__support.HashTable.table
libc.src.__support.libc_assert
libc.src.__support.HashTable.randomness
libc.src.errno.errno
libc.include.search
)
Expand All @@ -62,7 +62,6 @@ add_entrypoint_object(
DEPENDS
libc.src.search.hsearch.global
libc.src.__support.HashTable.table
libc.src.__support.libc_assert
libc.include.search
)

Expand Down
6 changes: 6 additions & 0 deletions libc/src/search/hcreate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

namespace LIBC_NAMESPACE {
LLVM_LIBC_FUNCTION(int, hcreate, (size_t capacity)) {
// We follow FreeBSD's implementation here. If the global_hash_table is
// already initialized, this function will do nothing and return 1.
// https://cgit.freebsd.org/src/tree/lib/libc/stdlib/hcreate.c
if (internal::global_hash_table != nullptr)
return 1;

uint64_t randomness = internal::randomness::next_random_seed();
internal::HashTable *table =
internal::HashTable::allocate(capacity, randomness);
Expand Down
4 changes: 2 additions & 2 deletions libc/src/search/hdestroy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

#include "src/search/hdestroy.h"
#include "src/__support/HashTable/table.h"
#include "src/__support/libc_assert.h"
#include "src/search/hsearch/global.h"

namespace LIBC_NAMESPACE {
LLVM_LIBC_FUNCTION(void, hdestroy, (void)) {
LIBC_ASSERT(internal::global_hash_table != nullptr);
// HashTable::deallocate will check for nullptr. It will be a no-op if
// global_hash_table is null.
internal::HashTable::deallocate(internal::global_hash_table);
internal::global_hash_table = nullptr;
}
Expand Down
21 changes: 17 additions & 4 deletions libc/src/search/hsearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,37 @@
//===----------------------------------------------------------------------===//

#include "src/search/hsearch.h"
#include "src/__support/HashTable/randomness.h"
#include "src/__support/HashTable/table.h"
#include "src/__support/libc_assert.h"
#include "src/errno/libc_errno.h"
#include "src/search/hsearch/global.h"

namespace LIBC_NAMESPACE {
LLVM_LIBC_FUNCTION(ENTRY *, hsearch, (ENTRY item, ACTION action)) {
ENTRY *result;
LIBC_ASSERT(internal::global_hash_table != nullptr);
if (internal::global_hash_table == nullptr) {
// If global_hash_table is null, we create a new hash table with a minimal
// capacity. Such hashtable will be expanded as needed.
uint64_t randomness = internal::randomness::next_random_seed();
internal::global_hash_table = internal::HashTable::allocate(0, randomness);
}

// In rare cases, the global hashtable may still fail to allocate. We treat it
// as ESRCH or ENOMEM depending on the action.
switch (action) {
case FIND:
result = internal::global_hash_table->find(item.key);
result = internal::global_hash_table
? internal::global_hash_table->find(item.key)
: nullptr;
if (result == nullptr) {
libc_errno = ESRCH;
}
break;
case ENTER:
result = internal::global_hash_table->insert(item);
result =
internal::global_hash_table
? internal::HashTable::insert(internal::global_hash_table, item)
: nullptr;
if (result == nullptr) {
libc_errno = ENOMEM;
}
Expand Down
3 changes: 2 additions & 1 deletion libc/src/search/hsearch_r.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ LLVM_LIBC_FUNCTION(int, hsearch_r,
}
break;
case ENTER:
*retval = table->insert(item);
*retval = internal::HashTable::insert(table, item);
htab->__opaque = table;
if (*retval == nullptr) {
libc_errno = ENOMEM;
return 0;
Expand Down
6 changes: 3 additions & 3 deletions libc/test/src/__support/HashTable/bitmask_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
namespace LIBC_NAMESPACE {
namespace internal {

using ShortBitMask = BitMaskAdaptor<uint16_t, 0xffff, 1>;
using LargeBitMask = BitMaskAdaptor<uint64_t, 0x80'80'80'80'80'80'80'80, 8>;
using ShortBitMask = BitMaskAdaptor<uint16_t, 1>;
using LargeBitMask = BitMaskAdaptor<uint64_t, 8>;

TEST(LlvmLibcHashTableBitMaskTest, SingleBitStrideLowestSetBit) {
uint16_t data = 0xffff;
Expand Down Expand Up @@ -53,7 +53,7 @@ TEST(LlvmLibcHashTableBitMaskTest, SingleBitStrideIteration) {

TEST(LlvmLibcHashTableBitMaskTest, MultiBitStrideIteration) {
using Iter = IteratableBitMaskAdaptor<LargeBitMask>;
uint64_t data = Iter::MASK;
uint64_t data = 0x8080808080808080ul;
for (size_t i = 0; i < 8; ++i) {
Iter iter = {data};
size_t j = i;
Expand Down
79 changes: 66 additions & 13 deletions libc/test/src/__support/HashTable/table_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,63 @@ TEST(LlvmLibcTableTest, AllocationAndDeallocation) {
HashTable::deallocate(nullptr);
}

TEST(LlvmLibcTableTest, Iteration) {
constexpr size_t TEST_SIZE = 512;
size_t counter[TEST_SIZE];
struct key {
uint8_t bytes[3];
} keys[TEST_SIZE];
HashTable *table = HashTable::allocate(0, 0x7f7f7f7f7f7f7f7f);
ASSERT_NE(table, static_cast<HashTable *>(nullptr));
for (size_t i = 0; i < TEST_SIZE; ++i) {
counter[i] = 0;
if (i >= 256) {
keys[i].bytes[0] = 2;
keys[i].bytes[1] = i % 256;
keys[i].bytes[2] = 0;
} else {
keys[i].bytes[0] = 1;
keys[i].bytes[1] = i;
keys[i].bytes[2] = 0;
}
HashTable::insert(table, {reinterpret_cast<char *>(keys[i].bytes),
reinterpret_cast<void *>((size_t)i)});
}

size_t count = 0;
for (const ENTRY &e : *table) {
size_t data = reinterpret_cast<size_t>(e.data);
++counter[data];
++count;
}
ASSERT_EQ(count, TEST_SIZE);
for (size_t i = 0; i < TEST_SIZE; ++i) {
ASSERT_EQ(counter[i], static_cast<size_t>(1));
}
HashTable::deallocate(table);
}

// Check if resize works correctly. This test actually covers two things:
// - The sizes are indeed growing.
// - The sizes are growing rapidly enough to reach the upper bound.
TEST(LlvmLibcTableTest, GrowthSequence) {
size_t cap = capacity_to_entries(0);
// right shift 4 to avoid overflow ssize_t.
while (cap < static_cast<size_t>(-1) >> 4u) {
size_t hint = cap / 8 * 7 + 1;
size_t new_cap = capacity_to_entries(hint);
ASSERT_GT(new_cap, cap);
cap = new_cap;
}
}

TEST(LlvmLibcTableTest, Insertion) {
union key {
uint64_t value;
char bytes[8];
char bytes[2];
} keys[256];
for (size_t k = 0; k < 256; ++k) {
keys[k].value = LIBC_NAMESPACE::Endian::to_little_endian(k);
keys[k].bytes[0] = static_cast<char>(k);
keys[k].bytes[1] = 0;
}
constexpr size_t CAP = cpp::bit_ceil((sizeof(Group) + 1) * 8 / 7) / 8 * 7;
static_assert(CAP + 1 < 256, "CAP is too large for this test.");
Expand All @@ -46,27 +96,30 @@ TEST(LlvmLibcTableTest, Insertion) {

// insert to full capacity.
for (size_t i = 0; i < CAP; ++i) {
ASSERT_NE(table->insert({keys[i].bytes, keys[i].bytes}),
ASSERT_NE(HashTable::insert(table, {keys[i].bytes, keys[i].bytes}),
static_cast<ENTRY *>(nullptr));
}

// one more insert should fail.
ASSERT_EQ(table->insert({keys[CAP + 1].bytes, keys[CAP + 1].bytes}),
static_cast<ENTRY *>(nullptr));
// One more insert should grow the table successfully. We test the value
// here because the grow finishes with a fastpath insertion that is different
// from the normal insertion.
ASSERT_EQ(HashTable::insert(table, {keys[CAP].bytes, keys[CAP].bytes})->data,
static_cast<void *>(keys[CAP].bytes));

for (size_t i = 0; i < CAP; ++i) {
for (size_t i = 0; i <= CAP; ++i) {
ASSERT_EQ(strcmp(table->find(keys[i].bytes)->key, keys[i].bytes), 0);
}
for (size_t i = CAP; i < 256; ++i) {
for (size_t i = CAP + 1; i < 256; ++i) {
ASSERT_EQ(table->find(keys[i].bytes), static_cast<ENTRY *>(nullptr));
}

// do not replace old value
for (size_t i = 0; i < CAP; ++i) {
ASSERT_NE(table->insert({keys[i].bytes, reinterpret_cast<void *>(i)}),
static_cast<ENTRY *>(nullptr));
for (size_t i = 0; i <= CAP; ++i) {
ASSERT_NE(
HashTable::insert(table, {keys[i].bytes, reinterpret_cast<void *>(i)}),
static_cast<ENTRY *>(nullptr));
}
for (size_t i = 0; i < CAP; ++i) {
for (size_t i = 0; i <= CAP; ++i) {
ASSERT_EQ(table->find(keys[i].bytes)->data,
reinterpret_cast<void *>(keys[i].bytes));
}
Expand Down
30 changes: 17 additions & 13 deletions libc/test/src/search/hsearch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,21 @@ constexpr size_t CAP =
LIBC_NAMESPACE::cpp::bit_ceil((GROUP_SIZE + 1) * 8 / 7) / 8 * 7;
static_assert(CAP < sizeof(search_data), "CAP too large");

TEST(LlvmLibcHSearchTest, InsertTooMany) {
TEST(LlvmLibcHSearchTest, GrowFromZero) {
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
ASSERT_GT(LIBC_NAMESPACE::hcreate(GROUP_SIZE + 1), 0);

for (size_t i = 0; i < CAP; ++i) {
ASSERT_EQ(LIBC_NAMESPACE::hsearch({&search_data[i], nullptr}, ENTER)->key,
&search_data[i]);
ASSERT_GT(LIBC_NAMESPACE::hcreate(0), 0);
for (size_t i = 0; i < sizeof(search_data) - 1; ++i) {
ENTRY *inserted = LIBC_NAMESPACE::hsearch(
{&search_data[i], reinterpret_cast<void *>(i)}, ENTER);
ASSERT_NE(inserted, static_cast<ENTRY *>(nullptr));
ASSERT_EQ(inserted->key, &search_data[i]);
}
ASSERT_THAT(static_cast<void *>(
LIBC_NAMESPACE::hsearch({search_data2, nullptr}, ENTER)),
Fails(ENOMEM, static_cast<void *>(nullptr)));
for (size_t i = sizeof(search_data) - 1; i != 0; --i) {
ASSERT_EQ(
LIBC_NAMESPACE::hsearch({&search_data[i - 1], nullptr}, FIND)->data,
reinterpret_cast<void *>(i - 1));
}

LIBC_NAMESPACE::hdestroy();
}

Expand All @@ -85,10 +89,10 @@ TEST(LlvmLibcHSearchTest, Found) {
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
ASSERT_GT(LIBC_NAMESPACE::hcreate(GROUP_SIZE + 1), 0);
for (size_t i = 0; i < CAP; ++i) {
ASSERT_EQ(LIBC_NAMESPACE::hsearch(
{&search_data[i], reinterpret_cast<void *>(i)}, ENTER)
->key,
&search_data[i]);
ENTRY *inserted = LIBC_NAMESPACE::hsearch(
{&search_data[i], reinterpret_cast<void *>(i)}, ENTER);
ASSERT_NE(inserted, static_cast<ENTRY *>(nullptr));
ASSERT_EQ(inserted->key, &search_data[i]);
}
for (size_t i = 0; i < CAP; ++i) {
ASSERT_EQ(LIBC_NAMESPACE::hsearch({&search_data[i], nullptr}, FIND)->data,
Expand Down