Skip to content

Commit

Permalink
Refactor tests for moving strings with compaction
Browse files Browse the repository at this point in the history
  • Loading branch information
peterzhu2118 committed Jun 29, 2022
1 parent 95bfea6 commit 66eb58d
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions test/ruby/test_gc_compact.rb
Expand Up @@ -209,42 +209,36 @@ def obj.tracee
assert_equal([:call, :line], results)
end

def test_moving_strings_between_size_pools
def test_moving_strings_up_size_pools
assert_separately([], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 10, signal: :SEGV)
begin;
moveables = []
small_slots = []
large_slots = []
STR_COUNT = 500
# Ensure fragmentation in the large heap
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
}
GC.verify_compaction_references(double_heap: true, toward: :empty)
# Ensure fragmentation in the smaller heap
500.times {
small_slots << Object.new
Object.new
}
str = "a" * GC::INTERNAL_CONSTANTS[:BASE_SLOT_SIZE]
ary = STR_COUNT.times.map { "" << str }
500.times {
# 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
stats = GC.verify_compaction_references(double_heap: true, toward: :empty)
moveables << String.new("a").downcase
}
moveables.map { |s| s << ("bc" * base_slot_size) }
moveables.map { |s| s.squeeze! }
stats = GC.compact
assert_operator(stats[:moved_up][:T_STRING], :>=, STR_COUNT)
assert(ary) # warning: assigned but unused variable - ary
end;
end

def test_moving_strings_down_size_pools
assert_separately([], "#{<<~"begin;"}\n#{<<~"end;"}", timeout: 10, signal: :SEGV)
begin;
STR_COUNT = 500
GC.verify_compaction_references(double_heap: true, toward: :empty)
ary = STR_COUNT.times.map { ("a" * GC::INTERNAL_CONSTANTS[:BASE_SLOT_SIZE]).squeeze! }
moved_strings = (stats.dig(:moved_up, :T_STRING) || 0) +
(stats.dig(:moved_down, :T_STRING) || 0)
stats = GC.verify_compaction_references(double_heap: true, toward: :empty)
assert_operator(moved_strings, :>, 0)
assert_operator(stats[:moved_down][:T_STRING], :>=, STR_COUNT)
assert(ary) # warning: assigned but unused variable - ary
end;
end
end

0 comments on commit 66eb58d

Please sign in to comment.