summaryrefslogtreecommitdiff
path: root/gc.c
diff options
context:
space:
mode:
authorJohn Hawthorn <john@hawthorn.email>2022-01-25 19:16:57 -0800
committerJohn Hawthorn <john@hawthorn.email>2022-02-23 19:57:42 -0800
commitb13a7c8e36e9b00b5c6668846f31be4e25523111 (patch)
tree9de58995b2e66027b83cf13aeacc3eb781ddd848 /gc.c
parent764e4fa850de749790e5ed11c8a4ab86a4499ac0 (diff)
Constant time class to class ancestor lookup
Previously when checking ancestors, we would walk all the way up the ancestry chain checking each parent for a matching class or module. I believe this was especially unfriendly to CPU cache since for each step we need to check two cache lines (the class and class ext). This check is used quite often in: * case statements * rescue statements * Calling protected methods * Class#is_a? * Module#=== * Module#<=> I believe it's most common to check a class against a parent class, to this commit aims to improve that (unfortunately does not help checking for an included Module). This is done by storing on each class the number and an array of all parent classes, in order (BasicObject is at index 0). Using this we can check whether a class is a subclass of another in constant time since we know the location to expect it in the hierarchy.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/5568
Diffstat (limited to 'gc.c')
-rw-r--r--gc.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/gc.c b/gc.c
index 35b9598c2a..1061b506bf 100644
--- a/gc.c
+++ b/gc.c
@@ -3187,6 +3187,7 @@ obj_free(rb_objspace_t *objspace, VALUE obj)
rb_class_remove_subclass_head(obj);
rb_class_remove_from_module_subclasses(obj);
rb_class_remove_from_super_subclasses(obj);
+ rb_class_remove_superclasses(obj);
#if SIZEOF_SERIAL_T != SIZEOF_VALUE && USE_RVARGC
xfree(RCLASS(obj)->class_serial_ptr);
#endif
@@ -4619,6 +4620,7 @@ obj_memsize_of(VALUE obj, int use_all_types)
if (RCLASS_CC_TBL(obj)) {
size += cc_table_memsize(RCLASS_CC_TBL(obj));
}
+ size += RCLASS_SUPERCLASS_DEPTH(obj) * sizeof(VALUE);
#if !USE_RVARGC
size += sizeof(rb_classext_t);
#endif
@@ -10033,6 +10035,14 @@ update_class_ext(rb_objspace_t *objspace, rb_classext_t *ext)
}
static void
+update_superclasses(rb_objspace_t *objspace, VALUE obj)
+{
+ for (size_t i = 0; i < RCLASS_SUPERCLASS_DEPTH(obj); i++) {
+ UPDATE_IF_MOVED(objspace, RCLASS_SUPERCLASSES(obj)[i]);
+ }
+}
+
+static void
gc_update_object_references(rb_objspace_t *objspace, VALUE obj)
{
RVALUE *any = RANY(obj);
@@ -10049,6 +10059,7 @@ gc_update_object_references(rb_objspace_t *objspace, VALUE obj)
update_m_tbl(objspace, RCLASS_M_TBL(obj));
update_cc_tbl(objspace, obj);
update_cvc_tbl(objspace, obj);
+ update_superclasses(objspace, obj);
gc_update_tbl_refs(objspace, RCLASS_IV_TBL(obj));