From 63b082cf0e87942dcea28cbdeb1c8a9e616e903a Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Wed, 17 Dec 2025 12:12:17 -0800 Subject: [PATCH] Store ractor_id directly on EC This is easier to access as ec->ractor_id instead of pointer-chasing through ec->thread->ractor->ractor_id Co-authored-by: Luke Gruber --- thread.c | 1 + vm.c | 1 + vm_core.h | 8 ++++++++ vm_insnhelper.c | 4 ++-- vm_method.c | 2 +- 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/thread.c b/thread.c index 3e1bb1dbe7..788a0e9ad7 100644 --- a/thread.c +++ b/thread.c @@ -860,6 +860,7 @@ thread_create_core(VALUE thval, struct thread_create_params *params) #endif th->invoke_type = thread_invoke_type_ractor_proc; th->ractor = params->g; + th->ec->ractor_id = rb_ractor_id(th->ractor); th->ractor->threads.main = th; th->invoke_arg.proc.proc = rb_proc_isolate_bang(params->proc, Qnil); th->invoke_arg.proc.args = INT2FIX(RARRAY_LENINT(params->args)); diff --git a/vm.c b/vm.c index 15cf64c7b3..f78a779c3f 100644 --- a/vm.c +++ b/vm.c @@ -3955,6 +3955,7 @@ th_init(rb_thread_t *th, VALUE self, rb_vm_t *vm) th->ec->local_storage_recursive_hash_for_trace = Qnil; th->ec->storage = Qnil; + th->ec->ractor_id = rb_ractor_id(th->ractor); #if OPT_CALL_THREADED_CODE th->retval = Qundef; diff --git a/vm_core.h b/vm_core.h index 839c054ab3..999f06d403 100644 --- a/vm_core.h +++ b/vm_core.h @@ -1048,6 +1048,7 @@ struct rb_execution_context_struct { rb_fiber_t *fiber_ptr; struct rb_thread_struct *thread_ptr; rb_serial_t serial; + rb_serial_t ractor_id; /* storage (ec (fiber) local) */ struct rb_id_table *local_storage; @@ -2070,6 +2071,13 @@ rb_ec_ractor_ptr(const rb_execution_context_t *ec) } } +static inline rb_serial_t +rb_ec_ractor_id(const rb_execution_context_t *ec) +{ + VM_ASSERT(ec->ractor_id == rb_ractor_id(rb_ec_ractor_ptr(ec))); + return ec->ractor_id; +} + static inline rb_vm_t * rb_ec_vm_ptr(const rb_execution_context_t *ec) { diff --git a/vm_insnhelper.c b/vm_insnhelper.c index aa67e54d0a..2ad67461bb 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -4120,7 +4120,7 @@ vm_call_bmethod_body(rb_execution_context_t *ec, struct rb_calling_info *calling VALUE procv = cme->def->body.bmethod.proc; if (!RB_OBJ_SHAREABLE_P(procv) && - cme->def->body.bmethod.defined_ractor_id != rb_ractor_id(rb_ec_ractor_ptr(ec))) { + cme->def->body.bmethod.defined_ractor_id != rb_ec_ractor_id(ec)) { rb_raise(rb_eRuntimeError, "defined with an un-shareable Proc in a different Ractor"); } @@ -4143,7 +4143,7 @@ vm_call_iseq_bmethod(rb_execution_context_t *ec, rb_control_frame_t *cfp, struct VALUE procv = cme->def->body.bmethod.proc; if (!RB_OBJ_SHAREABLE_P(procv) && - cme->def->body.bmethod.defined_ractor_id != rb_ractor_id(rb_ec_ractor_ptr(ec))) { + cme->def->body.bmethod.defined_ractor_id != rb_ec_ractor_id(ec)) { rb_raise(rb_eRuntimeError, "defined with an un-shareable Proc in a different Ractor"); } diff --git a/vm_method.c b/vm_method.c index 9f569df7fa..2a6323e593 100644 --- a/vm_method.c +++ b/vm_method.c @@ -1030,7 +1030,7 @@ rb_method_definition_set(const rb_method_entry_t *me, rb_method_definition_t *de } case VM_METHOD_TYPE_BMETHOD: RB_OBJ_WRITE(me, &def->body.bmethod.proc, (VALUE)opts); - def->body.bmethod.defined_ractor_id = rb_ractor_id(rb_ec_ractor_ptr(GET_EC())); + def->body.bmethod.defined_ractor_id = rb_ec_ractor_id(GET_EC()); return; case VM_METHOD_TYPE_NOTIMPLEMENTED: setup_method_cfunc_struct(UNALIGNED_MEMBER_PTR(def, body.cfunc), (VALUE(*)(ANYARGS))rb_f_notimplement_internal, -1);