summaryrefslogtreecommitdiff
path: root/vm.c
diff options
context:
space:
mode:
authoralpaca-tc <[email protected]>2025-04-06 01:50:08 +0900
committerKoichi Sasada <[email protected]>2025-06-09 12:33:35 +0900
commitc8ddc0a843074811b200673a2019fbe4b50bb890 (patch)
treef3881cd1f408f10abfdd7a8258d88cf18a557717 /vm.c
parentd0b5f3155406e8243b78e4cedd3a38710c7c323c (diff)
Optimize callcache invalidation for refinements
Fixes [Bug #21201] This change addresses a performance regression where defining methods inside `refine` blocks caused severe slowdowns. The issue was due to `rb_clear_all_refinement_method_cache()` triggering a full object space scan via `rb_objspace_each_objects` to find and invalidate affected callcaches, which is very inefficient. To fix this, I introduce `vm->cc_refinement_table` to track callcaches related to refinements. This allows us to invalidate only the necessary callcaches without scanning the entire heap, resulting in significant performance improvement.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/13077
Diffstat (limited to 'vm.c')
-rw-r--r--vm.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/vm.c b/vm.c
index f3e4f1e2ce..4b254eaea1 100644
--- a/vm.c
+++ b/vm.c
@@ -3194,6 +3194,10 @@ ruby_vm_destruct(rb_vm_t *vm)
st_free_table(vm->ci_table);
vm->ci_table = NULL;
}
+ if (vm->cc_refinement_table) {
+ rb_set_free_table(vm->cc_refinement_table);
+ vm->cc_refinement_table = NULL;
+ }
RB_ALTSTACK_FREE(vm->main_altstack);
struct global_object_list *next;
@@ -3294,6 +3298,7 @@ vm_memsize(const void *ptr)
vm_memsize_builtin_function_table(vm->builtin_function_table) +
rb_id_table_memsize(vm->negative_cme_table) +
rb_st_memsize(vm->overloaded_cme_table) +
+ rb_set_memsize(vm->cc_refinement_table) +
vm_memsize_constant_cache()
);
@@ -4503,6 +4508,7 @@ Init_vm_objects(void)
vm->mark_object_ary = pin_array_list_new(Qnil);
vm->loading_table = st_init_strtable();
vm->ci_table = st_init_table(&vm_ci_hashtype);
+ vm->cc_refinement_table = rb_set_init_numtable();
}
// Stub for builtin function when not building YJIT units