From 847eb1a69b54da3a5d686922f0a2fcd8ab37f1e6 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Wed, 20 Aug 2025 23:27:04 +0100 Subject: Refactor vectors to SBO, removing inlined entirely. Avoid 2 levels of indirection, and having to allocate twice for small payloads, by having an inlined array on the vector directly! Beautiful and simple. Required a bit of refactoring around the board, but overall the result makes me feel happier. --- symtable.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'symtable.c') diff --git a/symtable.c b/symtable.c index 1358b5e..c804344 100644 --- a/symtable.c +++ b/symtable.c @@ -29,39 +29,43 @@ void sym_table_init(sym_table_t *table) { table->capacity = MAX(table->capacity, SYM_TABLE_INIT_SIZE); table->count = 0; - ivec_make((void **)&table->entries, - table->capacity * sizeof(*table->entries)); + vec_init(&table->entries, table->capacity * sizeof(sv_t)); } -sv_t *sym_table_find(sym_table_t *table, sv_t sv) +char *sym_table_find(sym_table_t *table, sv_t sv) { // WIP: Deal with resizing this when table->count > table->size / 2 u64 index = djb2(sv) & (table->capacity - 1); - for (sv_t comp = table->entries[index]; comp.data; index += 1, - index = index & (table->capacity - 1), comp = table->entries[index]) + for (sv_t comp = VEC_GET(&table->entries, sv_t)[index]; comp.data; index += 1, + index = index & (table->capacity - 1), + comp = VEC_GET(&table->entries, sv_t)[index]) // Is it present in the table? if (sv.size == comp.size && strncmp(sv.data, comp.data, sv.size) == 0) break; // we couldn't find it in our linear search (worst case scenario) - if (!table->entries[index].data) + if (!VEC_GET(&table->entries, sv_t)[index].data) { - sv_t newsv = sv_copy(sv); - table->entries[index] = newsv; + sv_t newsv = sv_copy(sv); + VEC_GET(&table->entries, sv_t)[index] = newsv; ++table->count; } - return table->entries + index; + return VEC_GET(&table->entries, sv_t)[index].data; } void sym_table_cleanup(sym_table_t *table) { // kill the data + sv_t current = {0}; for (u64 i = 0; i < table->capacity; ++i) - if (table->entries[i].data) - free(table->entries[i].data); + { + current = VEC_GET(&table->entries, sv_t)[i]; + if (current.data) + free(current.data); + } // kill the container - ivec_free((void **)&table->entries); + vec_free(&table->entries); memset(table, 0, sizeof(*table)); } -- cgit v1.2.3-13-gbd6f