Skip to content

Commit

Permalink
Support GC.auto_compact = :empty on debug builds
Browse files Browse the repository at this point in the history
This commit adds `GC.auto_compact = :empty` which will run
auto-compaction sorting pages by empty slots so the most amount of
objects will be moved. This will make it easier to write tests for
auto-compaction.
  • Loading branch information
peterzhu2118 committed Dec 19, 2023
1 parent 3d98436 commit 32ecda3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions gc.c
Expand Up @@ -1342,6 +1342,9 @@ int ruby_gc_debug_indent = 0;
VALUE rb_mGC;
int ruby_disable_gc = 0;
int ruby_enable_autocompact = 0;
#if RGENGC_CHECK_MODE
gc_compact_compare_func ruby_autocompact_compare_func;
#endif

void rb_iseq_mark_and_move(rb_iseq_t *iseq, bool referece_updating);
void rb_iseq_free(const rb_iseq_t *iseq);
Expand Down Expand Up @@ -9549,6 +9552,9 @@ gc_start(rb_objspace_t *objspace, unsigned int reason)
/* Explicitly enable compaction (GC.compact) */
if (do_full_mark && ruby_enable_autocompact) {
objspace->flags.during_compacting = TRUE;
#if RGENGC_CHECK_MODE
objspace->rcompactor.compare_func = ruby_autocompact_compare_func;
#endif
}
else {
objspace->flags.during_compacting = !!(reason & GPR_FLAG_COMPACT);
Expand Down Expand Up @@ -11883,6 +11889,18 @@ gc_set_auto_compact(VALUE _, VALUE v)
GC_ASSERT(GC_COMPACTION_SUPPORTED);

ruby_enable_autocompact = RTEST(v);

#if RGENGC_CHECK_MODE
ruby_autocompact_compare_func = NULL;

if (SYMBOL_P(v)) {
ID id = RB_SYM2ID(v);
if (id == rb_intern("empty")) {
ruby_autocompact_compare_func = compare_free_slots;
}
}
#endif

return v;
}
#else
Expand Down
4 changes: 2 additions & 2 deletions tool/lib/envutil.rb
Expand Up @@ -245,9 +245,9 @@ def under_gc_stress(stress = true)
end
module_function :under_gc_stress

def under_gc_compact_stress(&block)
def under_gc_compact_stress(val = :empty, &block)
auto_compact = GC.auto_compact
GC.auto_compact = true
GC.auto_compact = val
under_gc_stress(&block)
ensure
GC.auto_compact = auto_compact
Expand Down

0 comments on commit 32ecda3

Please sign in to comment.