1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Refactor tests for moving strings with compaction

This commit is contained in:
Peter Zhu 2022-06-29 14:04:04 -04:00
parent 95bfea6648
commit 66eb58d6bd
Notes: git 2022-06-30 04:12:12 +09:00

View file

@ -209,42 +209,36 @@ class TestGCCompact < Test::Unit::TestCase
assert_equal([:call, :line], results) assert_equal([:call, :line], results)
end end
def test_moving_strings_between_size_pools def test_moving_strings_up_size_pools
assert_separately([], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 10, signal: :SEGV) assert_separately([], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 10, signal: :SEGV)
begin; begin;
moveables = [] STR_COUNT = 500
small_slots = []
large_slots = []
# Ensure fragmentation in the large heap GC.verify_compaction_references(double_heap: true, toward: :empty)
base_slot_size = GC.stat_heap[0].fetch(:slot_size)
500.times {
String.new(+"a" * base_slot_size).downcase
large_slots << String.new(+"a" * base_slot_size).downcase
}
# Ensure fragmentation in the smaller heap str = "a" * GC::INTERNAL_CONSTANTS[:BASE_SLOT_SIZE]
500.times { ary = STR_COUNT.times.map { "" << str }
small_slots << Object.new
Object.new
}
500.times { stats = GC.verify_compaction_references(double_heap: true, toward: :empty)
# strings are created as shared strings when initialized from literals
# use downcase to force the creation of an embedded string (it calls
# rb_str_new internally)
moveables << String.new(+"a" * base_slot_size).downcase
moveables << String.new("a").downcase assert_operator(stats[:moved_up][:T_STRING], :>=, STR_COUNT)
} assert(ary) # warning: assigned but unused variable - ary
moveables.map { |s| s << ("bc" * base_slot_size) } end;
moveables.map { |s| s.squeeze! } end
stats = GC.compact
moved_strings = (stats.dig(:moved_up, :T_STRING) || 0) + def test_moving_strings_down_size_pools
(stats.dig(:moved_down, :T_STRING) || 0) assert_separately([], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 10, signal: :SEGV)
begin;
STR_COUNT = 500
assert_operator(moved_strings, :>, 0) GC.verify_compaction_references(double_heap: true, toward: :empty)
ary = STR_COUNT.times.map { ("a" * GC::INTERNAL_CONSTANTS[:BASE_SLOT_SIZE]).squeeze! }
stats = GC.verify_compaction_references(double_heap: true, toward: :empty)
assert_operator(stats[:moved_down][:T_STRING], :>=, STR_COUNT)
assert(ary) # warning: assigned but unused variable - ary
end; end;
end end
end end