aboutsummaryrefslogtreecommitdiffstats
path: root/gc.c
diff options
context:
space:
mode:
authorJohn Hawthorn <john@hawthorn.email>2022-02-26 16:05:06 -0800
committerJohn Hawthorn <john@hawthorn.email>2022-03-03 11:23:27 -0800
commit19f331f58823dc0ff90ba7806c46380dc4064fa3 (patch)
tree230da9fe95b90dc396f20acf4c8c79850aeebf6a /gc.c
parent4d28009f09f0554467d914acb3c4c2dcf1cebfe7 (diff)
downloadruby-19f331f58823dc0ff90ba7806c46380dc4064fa3.tar.gz
Dedup superclass array in leaf sibling classes
Previously, we would build a new `superclasses` array for each class, even though for all immediate subclasses of a class, the array is identical. This avoids duplicating the arrays on leaf classes (those without subclasses) by calculating and storing a "superclasses including self" array on a class when it's first inherited and sharing that among all superclasses. An additional trick used is that the "superclass array including self" is valid as "self"'s superclass array. It just has it's own class at the end. We can use this to avoid an extra pointer of storage and can use one bit of a flag to track that we've "upgraded" the array.
Diffstat (limited to 'gc.c')
-rw-r--r--gc.c14
1 files changed, 10 insertions, 4 deletions
diff --git a/gc.c b/gc.c
index 1061b506bf..134100584c 100644
--- a/gc.c
+++ b/gc.c
@@ -3187,7 +3187,9 @@ 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 (FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
+ xfree(RCLASS_SUPERCLASSES(obj));
+ }
#if SIZEOF_SERIAL_T != SIZEOF_VALUE && USE_RVARGC
xfree(RCLASS(obj)->class_serial_ptr);
#endif
@@ -4620,7 +4622,9 @@ 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 (FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
+ size += (RCLASS_SUPERCLASS_DEPTH(obj) + 1) * sizeof(VALUE);
+ }
#if !USE_RVARGC
size += sizeof(rb_classext_t);
#endif
@@ -10037,8 +10041,10 @@ 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]);
+ if (FL_TEST_RAW(obj, RCLASS_SUPERCLASSES_INCLUDE_SELF)) {
+ for (size_t i = 0; i < RCLASS_SUPERCLASS_DEPTH(obj) + 1; i++) {
+ UPDATE_IF_MOVED(objspace, RCLASS_SUPERCLASSES(obj)[i]);
+ }
}
}