summaryrefslogtreecommitdiff
path: root/vm_insnhelper.c
diff options
context:
space:
mode:
authorKoichi Sasada <ko1@atdot.net>2021-01-04 18:08:25 +0900
committerKoichi Sasada <ko1@atdot.net>2021-01-05 02:27:58 +0900
commite7fc353f044f9280222ca41b029b1368d2bf2fe3 (patch)
tree3e3a8e464dbc3767d5b71f17675d3f0dba6ac43f /vm_insnhelper.c
parentbf21faec1521540f2be05df400c37600b4316a0f (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.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/4022
Diffstat (limited to 'vm_insnhelper.c')
-rw-r--r--vm_insnhelper.c32
1 files changed, 20 insertions, 12 deletions
diff --git a/vm_insnhelper.c b/vm_insnhelper.c
index da23b1660a..7ac5567e9f 100644
--- a/vm_insnhelper.c
+++ b/vm_insnhelper.c
@@ -4611,26 +4611,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