diff options
author | Koichi Sasada <ko1@atdot.net> | 2021-01-04 18:08:25 +0900 |
---|---|---|
committer | NARUSE, Yui <naruse@airemix.jp> | 2021-01-13 17:06:16 +0900 |
commit | b93e16dc0f45069d4a5fcce20d5c4437e151f0a8 (patch) | |
tree | 58e71dd4acd25b3deca641a6d5d2e25432dbc646 /vm_insnhelper.c | |
parent | 95aff214687a5e12c3eb57d056665741e734c188 (diff) |
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.
Diffstat (limited to 'vm_insnhelper.c')
-rw-r--r-- | vm_insnhelper.c | 32 |
1 files changed, 20 insertions, 12 deletions
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 |