imemo_fields_set: save copying when reassigning a variable

If we still fit in the existing imemo/fields object we can
update it atomically, saving a reallocation.
This commit is contained in:
Jean Boussier 2025-07-02 13:10:24 +02:00
parent a020e3490a
commit 517c106709
3 changed files with 41 additions and 8 deletions

View file

@ -566,9 +566,7 @@ RCLASSEXT_SET_FIELDS_OBJ(VALUE obj, rb_classext_t *ext, VALUE fields_obj)
{
RUBY_ASSERT(RB_TYPE_P(obj, RUBY_T_CLASS) || RB_TYPE_P(obj, RUBY_T_MODULE));
VALUE old_fields_obj = ext->fields_obj;
RUBY_ATOMIC_VALUE_SET(ext->fields_obj, fields_obj);
RB_OBJ_WRITTEN(obj, old_fields_obj, fields_obj);
RB_OBJ_ATOMIC_WRITE(obj, &ext->fields_obj, fields_obj);
}
static inline void

View file

@ -264,6 +264,26 @@ int rb_gc_modular_gc_loaded_p(void);
RUBY_SYMBOL_EXPORT_END
static inline VALUE
rb_obj_atomic_write(
VALUE a, VALUE *slot, VALUE b,
RBIMPL_ATTR_MAYBE_UNUSED()
const char *filename,
RBIMPL_ATTR_MAYBE_UNUSED()
int line)
{
#ifdef RGENGC_LOGGING_WRITE
RGENGC_LOGGING_WRITE(a, slot, b, filename, line);
#endif
RUBY_ATOMIC_VALUE_SET(*slot, b);
rb_obj_written(a, RUBY_Qundef /* ignore `oldv' now */, b, filename, line);
return a;
}
#define RB_OBJ_ATOMIC_WRITE(old, slot, young) \
RBIMPL_CAST(rb_obj_atomic_write((VALUE)(old), (VALUE *)(slot), (VALUE)(young), __FILE__, __LINE__))
int rb_ec_stack_check(struct rb_execution_context_struct *ec);
void rb_gc_writebarrier_remember(VALUE obj);
const char *rb_obj_info(VALUE obj);