Skip to content

Commit 169ba06

Browse files
committed
[ruby/prism] Use a bloom filter to quickly reject local lookups
ruby/prism@fc0ec4c9f4
1 parent 9a76883 commit 169ba06

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

prism/parser.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,13 @@ typedef struct pm_locals {
556556
/** The capacity of the local variables set. */
557557
uint32_t capacity;
558558

559+
/**
560+
* A bloom filter over constant IDs stored in this set. Used to quickly
561+
* reject lookups for names that are definitely not present, avoiding the
562+
* cost of a linear scan or hash probe.
563+
*/
564+
uint32_t bloom;
565+
559566
/** The nullable allocated memory for the local variables in the set. */
560567
pm_local_t *locals;
561568
} pm_locals_t;

prism/prism.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,8 @@ pm_locals_write(pm_locals_t *locals, pm_constant_id_t name, uint32_t start, uint
855855
pm_locals_resize(locals);
856856
}
857857

858+
locals->bloom |= (1u << (name & 31));
859+
858860
if (locals->capacity < PM_LOCALS_HASH_THRESHOLD) {
859861
for (uint32_t index = 0; index < locals->capacity; index++) {
860862
pm_local_t *local = &locals->locals[index];
@@ -907,6 +909,8 @@ pm_locals_write(pm_locals_t *locals, pm_constant_id_t name, uint32_t start, uint
907909
*/
908910
static uint32_t
909911
pm_locals_find(pm_locals_t *locals, pm_constant_id_t name) {
912+
if (!(locals->bloom & (1u << (name & 31)))) return UINT32_MAX;
913+
910914
if (locals->capacity < PM_LOCALS_HASH_THRESHOLD) {
911915
for (uint32_t index = 0; index < locals->size; index++) {
912916
pm_local_t *local = &locals->locals[index];

0 commit comments

Comments
 (0)