aboutsummaryrefslogtreecommitdiff
path: root/src/stdlib/tables.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-06-24 12:56:23 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-06-24 12:56:23 -0400
commite122e5525ef044c25c2cd888bc267f7a2bd91b4f (patch)
treea1f6bf8772639d0c82d3ebb2feb1799de8023ac8 /src/stdlib/tables.c
parent7f8735d84baeff4d41182d0682f8bf4aeaa07c8c (diff)
Minor performance improvement for hash collisions in tables
Diffstat (limited to 'src/stdlib/tables.c')
-rw-r--r--src/stdlib/tables.c22
1 files changed, 9 insertions, 13 deletions
diff --git a/src/stdlib/tables.c b/src/stdlib/tables.c
index 762afb06..9301914c 100644
--- a/src/stdlib/tables.c
+++ b/src/stdlib/tables.c
@@ -177,20 +177,16 @@ static void Table$set_bucket(Table_t *t, const void *entry, int32_t index, const
// Move mid-chain entry to free space and update predecessor
buckets[predecessor].next_bucket = t->bucket_info->last_free;
buckets[t->bucket_info->last_free] = *bucket;
- } else { // Collided with the start of a chain
+
+ bucket->occupied = 1;
+ bucket->index = index;
+ bucket->next_bucket = END_OF_CHAIN;
+ } else { // Collided with the start of a chain, put the new entry in chain position #2
hdebug("Hit start of a chain\n");
- uint64_t end_of_chain = hash;
- while (buckets[end_of_chain].next_bucket != END_OF_CHAIN)
- end_of_chain = buckets[end_of_chain].next_bucket;
- hdebug("Appending to chain\n");
- // Chain now ends on the free space:
- buckets[end_of_chain].next_bucket = t->bucket_info->last_free;
- bucket = &buckets[t->bucket_info->last_free];
- }
-
- bucket->occupied = 1;
- bucket->index = index;
- bucket->next_bucket = END_OF_CHAIN;
+ buckets[t->bucket_info->last_free] = (bucket_t){
+ .occupied = 1, .index=index, .next_bucket=bucket->next_bucket};
+ bucket->next_bucket = t->bucket_info->last_free;
+ }
hshow(t);
}