2019-04-19 21:19:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'test/unit'
|
|
|
|
require 'fiddle'
|
|
|
|
|
|
|
|
class TestGCCompact < Test::Unit::TestCase
|
|
|
|
def memory_location(obj)
|
|
|
|
(Fiddle.dlwrap(obj) >> 1)
|
|
|
|
end
|
|
|
|
|
2019-04-19 21:59:34 -04:00
|
|
|
def big_list(level = 10)
|
|
|
|
if level > 0
|
|
|
|
big_list(level - 1)
|
|
|
|
else
|
|
|
|
1000.times.map {
|
|
|
|
# try to make some empty slots by allocating an object and discarding
|
|
|
|
Object.new
|
|
|
|
Object.new
|
|
|
|
} # likely next to each other
|
|
|
|
end
|
2019-04-19 21:19:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Find an object that's allocated in a slot that had a previous
|
|
|
|
# tenant, and that tenant moved and is still alive
|
|
|
|
def find_object_in_recycled_slot(addresses)
|
|
|
|
new_object = nil
|
|
|
|
|
|
|
|
100_000.times do
|
|
|
|
new_object = Object.new
|
2019-04-19 23:00:49 -04:00
|
|
|
if addresses.index memory_location(new_object)
|
2019-04-19 21:19:47 -04:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
new_object
|
|
|
|
end
|
|
|
|
|
2019-04-23 15:10:52 -04:00
|
|
|
def test_complex_hash_keys
|
|
|
|
list_of_objects = big_list
|
|
|
|
hash = list_of_objects.hash
|
2019-05-09 16:13:56 -04:00
|
|
|
GC.verify_compaction_references(toward: :empty)
|
2019-04-23 15:10:52 -04:00
|
|
|
assert_equal hash, list_of_objects.hash
|
2020-01-26 20:46:57 -05:00
|
|
|
GC.verify_compaction_references(double_heap: false)
|
|
|
|
assert_equal hash, list_of_objects.hash
|
2019-04-23 15:10:52 -04:00
|
|
|
end
|
2019-09-09 18:46:07 -04:00
|
|
|
|
|
|
|
def walk_ast ast
|
|
|
|
children = ast.children.grep(RubyVM::AbstractSyntaxTree::Node)
|
|
|
|
children.each do |child|
|
|
|
|
assert child.type
|
|
|
|
walk_ast child
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ast_compacts
|
|
|
|
ast = RubyVM::AbstractSyntaxTree.parse_file __FILE__
|
|
|
|
assert GC.compact
|
|
|
|
walk_ast ast
|
|
|
|
end
|
2019-11-07 15:46:14 -05:00
|
|
|
|
|
|
|
def test_compact_count
|
|
|
|
count = GC.stat(:compact_count)
|
|
|
|
GC.compact
|
|
|
|
assert_equal count + 1, GC.stat(:compact_count)
|
|
|
|
end
|
2019-04-19 21:19:47 -04:00
|
|
|
end
|