1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

vm_insnhelper.c: never cache getinstancevariable twice

We have several options to ensure there's no race condition between main
thread and MJIT thead about IC reference:

1) Give up caching ivar for multiple classes (or multiple versions of the
   same class) in the same getinstancevariable (This commit's approach)
2) Allocate new inline cache every time

Other ideas we could think of couldn't eliminate possibilities of race
condition.
In 2, it's memory allocation would be slow and it may trigger JIT
cancellation frequently. So 1 would be fast for both VM and JIT
situations.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65213 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
k0kubun 2018-10-20 07:43:50 +00:00
parent b640b21d9c
commit 8449f4992b
4 changed files with 21 additions and 8 deletions

View file

@ -15,7 +15,7 @@
% end
%
% # compiler: Consider cfp->self as T_OBJECT if ic->ic_serial is set
if (ic->ic_serial) {
if (ic->ic_serial >= RUBY_VM_CLASS_SERIAL_MIN_VALID_VALUE) {
% # JIT: optimize away motion of sp and pc. This path does not call rb_warning() and so it's always leaf and not `handles_sp`.
% # <%= render 'mjit_compile_pc_and_sp', locals: { insn: insn } -%>
%

2
vm.c
View file

@ -340,7 +340,7 @@ rb_event_flag_t ruby_vm_event_flags;
rb_event_flag_t ruby_vm_event_enabled_flags;
rb_serial_t ruby_vm_global_method_state = 1;
rb_serial_t ruby_vm_global_constant_state = 1;
rb_serial_t ruby_vm_class_serial = 1;
rb_serial_t ruby_vm_class_serial = RUBY_VM_CLASS_SERIAL_MIN_VALID_VALUE;
static void thread_free(void *ptr);

View file

@ -1846,4 +1846,9 @@ void rb_postponed_job_flush(rb_vm_t *vm);
RUBY_SYMBOL_EXPORT_END
/* special values for ruby_vm_class_serial */
#define RUBY_VM_CLASS_SERIAL_UNSET 0 /* Not cached yet. Fields in `is_entries` and `cc_entries` start from 0 due to ZALLOC_N. */
#define RUBY_VM_CLASS_SERIAL_INVALID 1 /* Cache invalidated and never cached again. */
#define RUBY_VM_CLASS_SERIAL_MIN_VALID_VALUE 2 /* Actual class serials are larger than this value. */
#endif /* RUBY_VM_CORE_H */

View file

@ -976,12 +976,20 @@ vm_getivar(VALUE obj, ID id, IC ic, struct rb_call_cache *cc, int is_attr)
if (index < ROBJECT_NUMIV(obj)) {
val = ROBJECT_IVPTR(obj)[index];
}
if (!is_attr) {
ic->ic_value.index = index;
ic->ic_serial = RCLASS_SERIAL(RBASIC(obj)->klass);
}
else { /* call_info */
cc->aux.index = (int)index + 1;
if (is_attr) { /* call_info */
cc->aux.index = (int)index + 1;
}
else { /* getinstancevariable */
if (ic->ic_serial == RUBY_VM_CLASS_SERIAL_UNSET) {
/* set ic_serial only for the first time */
ic->ic_value.index = index;
ic->ic_serial = RCLASS_SERIAL(RBASIC(obj)->klass);
}
else if (ic->ic_serial != RUBY_VM_CLASS_SERIAL_INVALID) {
/* never use cache for another class, to avoid race condition with MJIT worker
and to reduce the number of JIT cancellations by code generated for IC hit. */
ic->ic_serial = RUBY_VM_CLASS_SERIAL_INVALID;
}
}
}
}