From c208db0752461b055b4b672ae45ba0489263ef08 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 8 Jul 2009 11:39:07 -0700 Subject: Pre-compute the hash table mask to avoid doing it in the critical path. --- upb_table.c | 1 + upb_table.h | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/upb_table.c b/upb_table.c index 03fab2f..3bbc7f7 100644 --- a/upb_table.c +++ b/upb_table.c @@ -30,6 +30,7 @@ void upb_table_init(struct upb_table *t, uint32_t size, uint16_t entry_size) t->size_lg2 = 1; while(size >>= 1) t->size_lg2++; size_t bytes = upb_table_size(t) * t->entry_size; + t->mask = upb_table_size(t) - 1; t->entries = malloc(bytes); memset(t->entries, 0, bytes); /* Both tables consider 0's an empty entry. */ } diff --git a/upb_table.h b/upb_table.h index 8eec03c..094ed48 100644 --- a/upb_table.h +++ b/upb_table.h @@ -48,6 +48,7 @@ struct upb_table { uint32_t count; /* How many elements are currently in the table? */ uint16_t entry_size; /* How big is each entry? */ uint8_t size_lg2; /* The table is 2^size_lg2 in size. */ + uint32_t mask; }; struct upb_strtable { @@ -85,7 +86,7 @@ void upb_inttable_insert(struct upb_inttable *t, struct upb_inttable_entry *e); void upb_strtable_insert(struct upb_strtable *t, struct upb_strtable_entry *e); INLINE uint32_t upb_inttable_bucket(struct upb_inttable *t, upb_inttable_key_t k) { - return (k & (upb_inttable_size(t)-1)) + 1; /* Identity hash for ints. */ + return (k & t->t.mask) + 1; /* Identity hash for ints. */ } /* Looks up key in this table. Inlined because this is in the critical path -- cgit v1.2.3