Skip to content

Implement gen_fields_tbl cache #14201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1245,9 +1245,19 @@ rb_obj_fields(VALUE obj, ID field_name)
goto generic_fields;
default:
generic_fields:
RB_VM_LOCKING() {
if (!st_lookup(generic_fields_tbl_, (st_data_t)obj, (st_data_t *)&fields_obj)) {
rb_bug("Object is missing entry in generic_fields_tbl");
{
rb_execution_context_t *ec = GET_EC();
if (ec->gen_fields_cache.obj == obj) {
fields_obj = ec->gen_fields_cache.fields_obj;
}
else {
RB_VM_LOCKING() {
if (!st_lookup(generic_fields_tbl_, (st_data_t)obj, (st_data_t *)&fields_obj)) {
rb_bug("Object is missing entry in generic_fields_tbl");
}
}
ec->gen_fields_cache.fields_obj = fields_obj;
ec->gen_fields_cache.obj = obj;
Comment on lines +1259 to +1260
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm mistaken, ec isn't write barrier protected.

}
}
}
Expand Down Expand Up @@ -1275,8 +1285,15 @@ rb_free_generic_ivar(VALUE obj)
goto generic_fields;
default:
generic_fields:
RB_VM_LOCKING() {
st_delete(generic_fields_tbl_no_ractor_check(), &key, &value);
{
rb_execution_context_t *ec = GET_EC();
if (ec->gen_fields_cache.obj == obj) {
ec->gen_fields_cache.obj = Qundef;
ec->gen_fields_cache.fields_obj = Qundef;
}
RB_VM_LOCKING() {
st_delete(generic_fields_tbl_no_ractor_check(), &key, &value);
}
}
}
RBASIC_SET_SHAPE_ID(obj, ROOT_SHAPE_ID);
Expand Down Expand Up @@ -1307,10 +1324,18 @@ rb_obj_set_fields(VALUE obj, VALUE fields_obj, ID field_name, VALUE original_fie
goto generic_fields;
default:
generic_fields:
RB_VM_LOCKING() {
st_insert(generic_fields_tbl_, (st_data_t)obj, (st_data_t)fields_obj);
{
RB_VM_LOCKING() {
st_insert(generic_fields_tbl_, (st_data_t)obj, (st_data_t)fields_obj);
}
RB_OBJ_WRITTEN(obj, original_fields_obj, fields_obj);

rb_execution_context_t *ec = GET_EC();
if (ec->gen_fields_cache.fields_obj != fields_obj) {
ec->gen_fields_cache.obj = obj;
ec->gen_fields_cache.fields_obj = fields_obj;
}
}
RB_OBJ_WRITTEN(obj, original_fields_obj, fields_obj);
}

if (original_fields_obj) {
Expand Down
6 changes: 6 additions & 0 deletions vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -3441,6 +3441,9 @@ rb_execution_context_update(rb_execution_context_t *ec)
}

ec->storage = rb_gc_location(ec->storage);

ec->gen_fields_cache.obj = rb_gc_location(ec->gen_fields_cache.obj);
ec->gen_fields_cache.fields_obj = rb_gc_location(ec->gen_fields_cache.fields_obj);
}

static enum rb_id_table_iterator_result
Expand Down Expand Up @@ -3505,6 +3508,9 @@ rb_execution_context_mark(const rb_execution_context_t *ec)
rb_gc_mark(ec->private_const_reference);

rb_gc_mark_movable(ec->storage);

rb_gc_mark_weak((VALUE *)&ec->gen_fields_cache.obj);
rb_gc_mark_weak((VALUE *)&ec->gen_fields_cache.fields_obj);
}

void rb_fiber_mark_self(rb_fiber_t *fib);
Expand Down
5 changes: 5 additions & 0 deletions vm_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,11 @@ struct rb_execution_context_struct {

VALUE private_const_reference;

struct {
VALUE obj;
VALUE fields_obj;
} gen_fields_cache;

/* for GC */
struct {
VALUE *stack_start;
Expand Down
Loading