From 75b96c3a056d9e50bdabd87fa4676e6aaffbcff0 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 3 Feb 2021 16:17:28 -0800 Subject: [PATCH] Don't register non-heap allocated objects `rb_define_const` can add objects as "mark objects". This is to make code like this work: https://github.com/ruby/ruby/blob/33d6e92e0c6eaf1308ce7108e653c53bb5fb106c/ext/etc/etc.c#L1201 ``` rb_define_const(rb_cStruct, "Passwd", sPasswd); /* deprecated name */ ``` sPasswd is a heap allocated object that is also a C global, so we can't move it (it needs to be pinned). However, we have many calls to `rb_define_const` that just pass in an integer like this: ``` rb_define_const(rb_cDBM, "WRITER", INT2FIX(O_RDWR|RUBY_DBM_RW_BIT)); ``` Non heap allocated objects like integers will never move, so there is no reason to waste time in the GC marking / pinning them. --- gc.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gc.c b/gc.c index 2d76b545098d1c..30badf975f70d4 100644 --- a/gc.c +++ b/gc.c @@ -8051,6 +8051,9 @@ rb_gc_force_recycle(VALUE obj) void rb_gc_register_mark_object(VALUE obj) { + if (!is_pointer_to_heap(&rb_objspace, (void *)obj)) + return; + RB_VM_LOCK_ENTER(); { VALUE ary_ary = GET_VM()->mark_object_ary;