From ff9ec241b670a7be6ee6af2016c25eaa9143e830 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Tue, 22 Oct 2024 12:24:12 +0200 Subject: [PATCH 1/3] Enable GC.auto_compact in test suite Reproduces: https://github.com/jhawthorn/vernier/issues/100 --- test/test_helper.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/test_helper.rb b/test/test_helper.rb index feff941..713982d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -23,3 +23,5 @@ def assert_valid_result(result) end end end + +GC.auto_compact = true From 5b0579fa3a568629b2ddd2ec44ffa4dcc3190303 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Wed, 23 Oct 2024 15:26:24 +0200 Subject: [PATCH 2/3] Handle collector->object_list being moved --- ext/vernier/vernier.cc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ext/vernier/vernier.cc b/ext/vernier/vernier.cc index ba4e880..28ba72c 100644 --- a/ext/vernier/vernier.cc +++ b/ext/vernier/vernier.cc @@ -1360,6 +1360,9 @@ class BaseCollector { rb_gc_mark(stack_table_value); }; + virtual void compact() { + }; + virtual VALUE get_markers() { return rb_ary_new(); }; @@ -1547,6 +1550,13 @@ class RetainedCollector : public BaseCollector { rb_gc_mark(tp_newobj); rb_gc_mark(tp_freeobj); } + + void compact() { + RetainedCollector *collector = this; + for (auto& obj: collector->object_list) { + obj = rb_gc_location(obj); + } + } }; class GlobalSignalHandler { @@ -1969,12 +1979,19 @@ collector_free(void *data) { delete collector; } +static void +collector_compact(void *data) { + BaseCollector *collector = static_cast(data); + collector->compact(); +} + static const rb_data_type_t rb_collector_type = { .wrap_struct_name = "vernier/collector", .function = { //.dmemsize = rb_collector_memsize, .dmark = collector_mark, .dfree = collector_free, + .dcompact = collector_compact, }, }; From a4babd8c54ecd3a54184356a605e3c360e918060 Mon Sep 17 00:00:00 2001 From: Joshua Young Date: Thu, 24 Oct 2024 19:30:41 +1000 Subject: [PATCH 3/3] Update collector->object_frames to reflect object_list move --- ext/vernier/vernier.cc | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/ext/vernier/vernier.cc b/ext/vernier/vernier.cc index 28ba72c..39d8bbc 100644 --- a/ext/vernier/vernier.cc +++ b/ext/vernier/vernier.cc @@ -1554,7 +1554,17 @@ class RetainedCollector : public BaseCollector { void compact() { RetainedCollector *collector = this; for (auto& obj: collector->object_list) { - obj = rb_gc_location(obj); + VALUE reloc_obj = rb_gc_location(obj); + + const auto search = collector->object_frames.find(obj); + if (search != collector->object_frames.end()) { + int stack_index = search->second; + + collector->object_frames.erase(search); + collector->object_frames.emplace(reloc_obj, stack_index); + } + + obj = reloc_obj; } } };