From b93e16dc0f45069d4a5fcce20d5c4437e151f0a8 Mon Sep 17 00:00:00 2001 From: Koichi Sasada Date: Mon, 4 Jan 2021 18:08:25 +0900 Subject: enable constant cache on ractors constant cache `IC` is accessed by non-atomic manner and there are thread-safety issues, so Ruby 3.0 disables to use const cache on non-main ractors. This patch enables it by introducing `imemo_constcache` and allocates it by every re-fill of const cache like `imemo_callcache`. [Bug #17510] Now `IC` only has one entry `IC::entry` and it points to `iseq_inline_constant_cache_entry`, managed by T_IMEMO object. `IC` is atomic data structure so `rb_mjit_before_vm_ic_update()` and `rb_mjit_after_vm_ic_update()` is not needed. --- vm_insnhelper.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'vm_insnhelper.c') diff --git a/vm_insnhelper.c b/vm_insnhelper.c index 07058cd65c..73ba44a12e 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -4610,26 +4610,34 @@ vm_opt_newarray_min(rb_num_t num, const VALUE *ptr) #undef id_cmp +#define IMEMO_CONST_CACHE_SHAREABLE IMEMO_FL_USER0 + static int -vm_ic_hit_p(const rb_serial_t ic_serial, const rb_cref_t *ic_cref, const VALUE *reg_ep) +vm_ic_hit_p(const struct iseq_inline_constant_cache_entry *ice, const VALUE *reg_ep) { - if (ic_serial == GET_GLOBAL_CONSTANT_STATE() && rb_ractor_main_p()) { - return (ic_cref == NULL || // no need to check CREF - ic_cref == vm_get_cref(reg_ep)); + VM_ASSERT(IMEMO_TYPE_P(ice, imemo_constcache)); + if (ice->ic_serial == GET_GLOBAL_CONSTANT_STATE() && + (FL_TEST_RAW((VALUE)ice, IMEMO_CONST_CACHE_SHAREABLE) || rb_ractor_main_p())) { + + VM_ASSERT(FL_TEST_RAW((VALUE)ice, IMEMO_CONST_CACHE_SHAREABLE) ? rb_ractor_shareable_p(ice->value) : true); + + return (ice->ic_cref == NULL || // no need to check CREF + ice->ic_cref == vm_get_cref(reg_ep)); } return FALSE; } static void -vm_ic_update(IC ic, VALUE val, const VALUE *reg_ep) -{ - VM_ASSERT(ic->value != Qundef); - rb_mjit_before_vm_ic_update(); - ic->value = val; - ic->ic_serial = GET_GLOBAL_CONSTANT_STATE() - ruby_vm_const_missing_count; - ic->ic_cref = vm_get_const_key_cref(reg_ep); - rb_mjit_after_vm_ic_update(); +vm_ic_update(const rb_iseq_t *iseq, IC ic, VALUE val, const VALUE *reg_ep) +{ + + struct iseq_inline_constant_cache_entry *ice = (struct iseq_inline_constant_cache_entry *)rb_imemo_new(imemo_constcache, 0, 0, 0, 0); + RB_OBJ_WRITE(ice, &ice->value, val); + ice->ic_cref = vm_get_const_key_cref(reg_ep); + ice->ic_serial = GET_GLOBAL_CONSTANT_STATE() - ruby_vm_const_missing_count; + if (rb_ractor_shareable_p(val)) ice->flags |= IMEMO_CONST_CACHE_SHAREABLE; ruby_vm_const_missing_count = 0; + RB_OBJ_WRITE(iseq, &ic->entry, ice); } static VALUE -- cgit v1.2.3