Skip to content

Commit 4cd3e41

Browse files
sribee8Sriya Pratipati
andauthored
[libc] Removed public function calls in table.h (#144168)
Removed strcmp, strlen, and memset calls from table.h and replaced them with internal functions. --------- Co-authored-by: Sriya Pratipati <[email protected]>
1 parent 267b859 commit 4cd3e41

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

libc/src/__support/HashTable/CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ add_header_library(
3232
libc.src.__support.macros.attributes
3333
libc.src.__support.macros.optimization
3434
libc.src.__support.memory_size
35-
libc.src.string.memset
36-
libc.src.string.strcmp
37-
libc.src.string.strlen
35+
libc.src.string.memory_utils.inline_strcmp
36+
libc.src.string.string_utils
3837
)
3938

4039
add_header_library(

libc/src/__support/HashTable/table.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
#include "src/__support/macros/config.h"
1919
#include "src/__support/macros/optimization.h"
2020
#include "src/__support/memory_size.h"
21-
#include "src/string/memset.h"
22-
#include "src/string/strcmp.h"
23-
#include "src/string/strlen.h"
21+
#include "src/string/memory_utils/inline_strcmp.h"
22+
#include "src/string/string_utils.h"
2423
#include <stddef.h>
2524
#include <stdint.h>
2625

@@ -158,7 +157,9 @@ struct HashTable {
158157
for (size_t i : masks) {
159158
size_t index = (pos + i) & entries_mask;
160159
ENTRY &entry = this->entry(index);
161-
if (LIBC_LIKELY(entry.key != nullptr && strcmp(entry.key, key) == 0))
160+
auto comp = [](char l, char r) -> int { return l - r; };
161+
if (LIBC_LIKELY(entry.key != nullptr &&
162+
inline_strcmp(entry.key, key, comp) == 0))
162163
return index;
163164
}
164165
BitMask available = ctrls.mask_available();
@@ -176,7 +177,7 @@ struct HashTable {
176177

177178
LIBC_INLINE uint64_t oneshot_hash(const char *key) const {
178179
LIBC_NAMESPACE::internal::HashState hasher = state;
179-
hasher.update(key, strlen(key));
180+
hasher.update(key, internal::string_length(key));
180181
return hasher.finish();
181182
}
182183

@@ -282,8 +283,8 @@ struct HashTable {
282283
table->entries_mask = entries - 1u;
283284
table->available_slots = entries / 8 * 7;
284285
table->state = HashState{randomness};
285-
memset(&table->control(0), 0x80, ctrl_sizes);
286-
memset(mem, 0, table->offset_from_entries());
286+
__builtin_memset(&table->control(0), 0x80, ctrl_sizes);
287+
__builtin_memset(mem, 0, table->offset_from_entries());
287288
}
288289
return table;
289290
}

libc/test/src/__support/HashTable/table_test.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ TEST(LlvmLibcTableTest, Insertion) {
108108
static_cast<void *>(keys[CAP].bytes));
109109

110110
for (size_t i = 0; i <= CAP; ++i) {
111-
ASSERT_EQ(strcmp(table->find(keys[i].bytes)->key, keys[i].bytes), 0);
111+
auto comp = [](char l, char r) -> int { return l - r; };
112+
ASSERT_EQ(
113+
inline_strcmp(table->find(keys[i].bytes)->key, keys[i].bytes, comp), 0);
112114
}
113115
for (size_t i = CAP + 1; i < 256; ++i) {
114116
ASSERT_EQ(table->find(keys[i].bytes), static_cast<ENTRY *>(nullptr));

0 commit comments

Comments
 (0)