diff options
author | Joshua Haberman <joshua@reverberate.org> | 2009-07-08 11:39:07 -0700 |
---|---|---|
committer | Joshua Haberman <joshua@reverberate.org> | 2009-07-08 11:39:07 -0700 |
commit | c208db0752461b055b4b672ae45ba0489263ef08 (patch) | |
tree | 6261f678f28eb1e347745b0b69a0c6f81f8933db | |
parent | 797bd11247fb0d5774b3fd6d9d562eaa5f8b14ae (diff) |
Pre-compute the hash table mask to avoid doing it in the critical path.
-rw-r--r-- | upb_table.c | 1 | ||||
-rw-r--r-- | upb_table.h | 3 |
2 files changed, 3 insertions, 1 deletions
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 |