Skip to content

Commit

Permalink
Hash table optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
dils2k committed Jun 29, 2023
1 parent 254bdc6 commit 1278e4f
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions table.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void freeTable(Table* table) {
}

static Entry* findEntry(Entry* entries, int capacity, ObjString* key) {
uint32_t index = key->hash % capacity;
uint32_t index = key->hash & (capacity-1);
Entry* tombstone = NULL;

for (;;) {
Expand All @@ -35,7 +35,7 @@ static Entry* findEntry(Entry* entries, int capacity, ObjString* key) {
return entry;
}

index = (index+1) % capacity;
index = (index+1) & (capacity-1);
}
}

Expand Down Expand Up @@ -90,7 +90,7 @@ ObjString* tableFindString(Table* table, const char* chars,
int length, uint32_t hash) {
if (table->count == 0) return NULL;

uint32_t index = hash % table->capacity;
uint32_t index = hash & (table->capacity-1);
for (;;) {
Entry* entry = &table->entries[index];
if (entry->key == NULL) {
Expand All @@ -102,7 +102,7 @@ ObjString* tableFindString(Table* table, const char* chars,
// We found it.
return entry->key;
}
index = (index + 1) % table->capacity;
index = (index + 1) & (table->capacity-1);
}
}

Expand Down

0 comments on commit 1278e4f

Please sign in to comment.