Skip to content

Commit 89e9040

Browse files
committed
Compare bucket hashes before comparing length in the constant pool
1 parent f277c70 commit 89e9040

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

src/constant_pool.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,9 @@ pm_constant_pool_find(const pm_constant_pool_t *pool, const uint8_t *start, size
245245
pm_constant_pool_bucket_t *bucket;
246246

247247
while (bucket = &pool->buckets[index], bucket->id != PM_CONSTANT_ID_UNSET) {
248-
if ((bucket->length == length) && memcmp(bucket->start, start, length) == 0) {
248+
// Compare the stored hash before touching the contents so that probe
249+
// collisions are rejected without a memcmp call.
250+
if ((bucket->hash == hash) && (bucket->length == length) && memcmp(bucket->start, start, length) == 0) {
249251
return bucket->id;
250252
}
251253

@@ -274,8 +276,9 @@ pm_constant_pool_insert(pm_arena_t *arena, pm_constant_pool_t *pool, const uint8
274276
while (bucket = &pool->buckets[index], bucket->id != PM_CONSTANT_ID_UNSET) {
275277
// If there is a collision, then we need to check if the content is the
276278
// same as the content we are trying to insert. If it is, then we can
277-
// return the id of the existing constant.
278-
if ((bucket->length == length) && memcmp(bucket->start, start, length) == 0) {
279+
// return the id of the existing constant. Compare the stored hash
280+
// first so that probe collisions are rejected without a memcmp call.
281+
if ((bucket->hash == hash) && (bucket->length == length) && memcmp(bucket->start, start, length) == 0) {
279282
// Since we have found a match, we need to check if this is
280283
// attempting to insert a shared or an owned constant. We want to
281284
// prefer shared constants since they don't require allocations.

0 commit comments

Comments
 (0)