aboutsummaryrefslogtreecommitdiffstats
path: root/class.c
diff options
context:
space:
mode:
authorJean Boussier <byroot@ruby-lang.org>2024-03-03 10:46:46 +0100
committerPeter Zhu <peter@peterzhu.ca>2024-03-06 15:33:43 -0500
commitd4f3dcf4dff80ded472ba3061e62c6c676ffab8c (patch)
tree4dd0954969e05925932b38ebd275d125f2304339 /class.c
parent16ec54ec4177f959b6912745460fd6a96d906d82 (diff)
downloadruby-d4f3dcf4dff80ded472ba3061e62c6c676ffab8c.tar.gz
Refactor VM root modules
This `st_table` is used to both mark and pin classes defined from the C API. But `vm->mark_object_ary` already does both much more efficiently. Currently a Ruby process starts with 252 rooted classes, which uses `7224B` in an `st_table` or `2016B` in an `RArray`. So a baseline of 5kB saved, but since `mark_object_ary` is preallocated with `1024` slots but only use `405` of them, it's a net `7kB` save. `vm->mark_object_ary` is also being refactored. Prior to this changes, `mark_object_ary` was a regular `RArray`, but since this allows for references to be moved, it was marked a second time from `rb_vm_mark()` to pin these objects. This has the detrimental effect of marking these references on every minors even though it's a mostly append only list. But using a custom TypedData we can save from having to mark all the references on minor GC runs. Addtionally, immediate values are now ignored and not appended to `vm->mark_object_ary` as it's just wasted space.
Diffstat (limited to 'class.c')
-rw-r--r--class.c24
1 files changed, 15 insertions, 9 deletions
diff --git a/class.c b/class.c
index 32ca477ce9..98e7dfcc08 100644
--- a/class.c
+++ b/class.c
@@ -32,6 +32,9 @@
/* Flags of T_CLASS
*
+ * 0: RCLASS_IS_ROOT
+ * The class has been added to the VM roots. Will always be marked and pinned.
+ * This is done for classes defined from C to allow storing them in global variables.
* 1: RUBY_FL_SINGLETON
* This class is a singleton class.
* 2: RCLASS_SUPERCLASSES_INCLUDE_SELF
@@ -56,6 +59,9 @@
/* Flags of T_MODULE
*
+ * 0: RCLASS_IS_ROOT
+ * The class has been added to the VM roots. Will always be marked and pinned.
+ * This is done for classes defined from C to allow storing them in global variables.
* 1: RMODULE_ALLOCATED_BUT_NOT_INITIALIZED
* Module has not been initialized.
* 2: RCLASS_SUPERCLASSES_INCLUDE_SELF
@@ -812,7 +818,7 @@ boot_defclass(const char *name, VALUE super)
ID id = rb_intern(name);
rb_const_set((rb_cObject ? rb_cObject : obj), id, obj);
- rb_vm_add_root_module(obj);
+ rb_vm_register_global_object(obj);
return obj;
}
@@ -894,7 +900,7 @@ Init_class_hierarchy(void)
{
rb_cBasicObject = boot_defclass("BasicObject", 0);
rb_cObject = boot_defclass("Object", rb_cBasicObject);
- rb_gc_register_mark_object(rb_cObject);
+ rb_vm_register_global_object(rb_cObject);
/* resolve class name ASAP for order-independence */
rb_set_class_path_string(rb_cObject, rb_cObject, rb_fstring_lit("Object"));
@@ -988,14 +994,14 @@ rb_define_class(const char *name, VALUE super)
}
/* Class may have been defined in Ruby and not pin-rooted */
- rb_vm_add_root_module(klass);
+ rb_vm_register_global_object(klass);
return klass;
}
if (!super) {
rb_raise(rb_eArgError, "no super class for '%s'", name);
}
klass = rb_define_class_id(id, super);
- rb_vm_add_root_module(klass);
+ rb_vm_register_global_object(klass);
rb_const_set(rb_cObject, id, klass);
rb_class_inherited(super, klass);
@@ -1045,7 +1051,7 @@ VALUE
rb_define_class_id_under(VALUE outer, ID id, VALUE super)
{
VALUE klass = rb_define_class_id_under_no_pin(outer, id, super);
- rb_vm_add_root_module(klass);
+ rb_vm_register_global_object(klass);
return klass;
}
@@ -1099,11 +1105,11 @@ rb_define_module(const char *name)
name, rb_obj_class(module));
}
/* Module may have been defined in Ruby and not pin-rooted */
- rb_vm_add_root_module(module);
+ rb_vm_register_global_object(module);
return module;
}
module = rb_module_new();
- rb_vm_add_root_module(module);
+ rb_vm_register_global_object(module);
rb_const_set(rb_cObject, id, module);
return module;
@@ -1128,13 +1134,13 @@ rb_define_module_id_under(VALUE outer, ID id)
outer, rb_id2str(id), rb_obj_class(module));
}
/* Module may have been defined in Ruby and not pin-rooted */
- rb_gc_register_mark_object(module);
+ rb_vm_register_global_object(module);
return module;
}
module = rb_module_new();
rb_const_set(outer, id, module);
rb_set_class_path_string(module, outer, rb_id2str(id));
- rb_gc_register_mark_object(module);
+ rb_vm_register_global_object(module);
return module;
}