From 771576f02185b1044f23a451e3ac6b58494844dc Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Sun, 27 Aug 2023 19:29:35 -0400 Subject: [PATCH] Skip weak references to old objects in minor GC If we are in a minor GC and the object to mark is old, then the old object should already be marked and cannot be reclaimed in this GC cycle so we don't need to add it to the weak refences list. --- gc.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/gc.c b/gc.c index 4c6fdbb300..0d2a9a4daa 100644 --- a/gc.c +++ b/gc.c @@ -6903,6 +6903,16 @@ rb_gc_mark_weak(VALUE *ptr) GC_ASSERT(objspace->rgengc.parent_object == 0 || FL_TEST(objspace->rgengc.parent_object, FL_WB_PROTECTED)); + /* If we are in a minor GC and the other object is old, then obj should + * already be marked and cannot be reclaimed in this GC cycle so we don't + * need to add it to the weak refences list. */ + if (!is_full_marking(objspace) && RVALUE_OLD_P(obj)) { + GC_ASSERT(RVALUE_MARKED(obj)); + GC_ASSERT(!objspace->flags.during_compacting); + + return; + } + rgengc_check_relation(objspace, obj); rb_darray_append_without_gc(&objspace->weak_references, ptr);