From e6aeacb099701ddcf8fe66772da280702a388bb4 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Fri, 5 Apr 2024 12:46:48 -0400 Subject: [PATCH] [ruby/prism] Lazily hash locals https://github.com/ruby/prism/commit/ef8ea4624a --- prism/prism.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/prism/prism.c b/prism/prism.c index 95e8f14857cea1..6e2a28cb91e1bc 100644 --- a/prism/prism.c +++ b/prism/prism.c @@ -719,12 +719,16 @@ pm_locals_rehash(pm_locals_t *locals) { next_locals = xcalloc(next_capacity, sizeof(pm_local_t)); if (next_locals == NULL) abort(); + // If we just switched from a list to a hash, then we need to fill in + // the hash values of all of the locals. + bool hash_needed = locals->locals[0].hash == 0; uint32_t mask = next_capacity - 1; + for (uint32_t index = 0; index < locals->capacity; index++) { pm_local_t *local = &locals->locals[index]; if (local->name != PM_CONSTANT_ID_UNSET) { - uint32_t hash = local->hash; + uint32_t hash = hash_needed ? pm_locals_hash(local->name) : local->hash; while (next_locals[hash & mask].name != PM_CONSTANT_ID_UNSET) hash++; next_locals[hash & mask] = *local; @@ -758,7 +762,7 @@ pm_locals_write(pm_locals_t *locals, pm_constant_id_t name) { .name = name, .index = locals->size++, .reads = 0, - .hash = pm_locals_hash(name) + .hash = 0 }; return true; } else if (local->name == name) {