From 671645c370498955eb101695bd9099bf4caf5aea Mon Sep 17 00:00:00 2001 From: ubq323 Date: Fri, 21 Jun 2024 02:46:15 +0100 Subject: minor optimization --- ht.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'ht.c') diff --git a/ht.c b/ht.c index 09eec3e..a2845f0 100644 --- a/ht.c +++ b/ht.c @@ -24,15 +24,17 @@ Ht ht_new() { } static HtEntry *find(HtEntry *b, size_t cap, ObjString *k) { - size_t ix = k->hash % cap; + // cap is guaranteed to be power of 2 + size_t mask = cap - 1; + size_t ix = k->hash & mask; if (cap == 0) return NULL; for (;;) { HtEntry *ent = &b[ix]; // XXX tombstones - if (ent->k == k || ent->k == NULL) { + if (ent->k == NULL || ent->k == k) { return ent; } - ix = (ix+1)%cap; + ix = (ix+1) & mask; } } -- cgit v1.2.3