From df69e4a12efaf6b21c08f57d9cc7512a6aed8674 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Mon, 18 Dec 2023 04:00:48 -0500 Subject: [PATCH 1/2] Use `compare_by_identity` instead of `object_id` Object IDs became more expensive in Ruby 2.7. Using `Hash#compare_by_identity` let's us get the same effect, without needing to force all these objects to have object_ids assigned to them. --- lib/psych/visitors/yaml_tree.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb index 51491783..1dd4094c 100644 --- a/lib/psych/visitors/yaml_tree.rb +++ b/lib/psych/visitors/yaml_tree.rb @@ -15,30 +15,29 @@ module Visitors class YAMLTree < Psych::Visitors::Visitor class Registrar # :nodoc: def initialize - @obj_to_id = {} - @obj_to_node = {} + @obj_to_id = {}.compare_by_identity + @obj_to_node = {}.compare_by_identity @targets = [] @counter = 0 end def register target, node - return unless target.respond_to? :object_id @targets << target - @obj_to_node[target.object_id] = node + @obj_to_node[target] = node end def key? target - @obj_to_node.key? target.object_id + @obj_to_node.key? target rescue NoMethodError false end def id_for target - @obj_to_id[target.object_id] ||= (@counter += 1) + @obj_to_id[target] ||= (@counter += 1) end def node_for target - @obj_to_node[target.object_id] + @obj_to_node[target] end end From 0dc25a9d6ae28a24bb8a8168f4ac2c27e413dc7c Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Mon, 18 Dec 2023 04:01:01 -0500 Subject: [PATCH 2/2] Use `assert_same` in tests where applicable --- test/psych/test_object_references.rb | 8 ++++---- test/psych/visitors/test_to_ruby.rb | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/test/psych/test_object_references.rb b/test/psych/test_object_references.rb index 269d7224..86bb9034 100644 --- a/test/psych/test_object_references.rb +++ b/test/psych/test_object_references.rb @@ -39,7 +39,7 @@ def assert_reference_trip obj rescue Psych::DisallowedClass data = Psych.unsafe_load yml end - assert_equal data.first.object_id, data.last.object_id + assert_same data.first, data.last end def test_float_references @@ -49,7 +49,7 @@ def test_float_references - *name eoyml assert_equal data.first, data.last - assert_equal data.first.object_id, data.last.object_id + assert_same data.first, data.last end def test_binary_references @@ -60,7 +60,7 @@ def test_binary_references - *name eoyml assert_equal data.first, data.last - assert_equal data.first.object_id, data.last.object_id + assert_same data.first, data.last end def test_regexp_references @@ -70,7 +70,7 @@ def test_regexp_references - *name eoyml assert_equal data.first, data.last - assert_equal data.first.object_id, data.last.object_id + assert_same data.first, data.last end end end diff --git a/test/psych/visitors/test_to_ruby.rb b/test/psych/visitors/test_to_ruby.rb index 3d4608b9..89c36766 100644 --- a/test/psych/visitors/test_to_ruby.rb +++ b/test/psych/visitors/test_to_ruby.rb @@ -319,7 +319,7 @@ def test_alias list = seq.to_ruby assert_equal %w{ foo foo }, list - assert_equal list[0].object_id, list[1].object_id + assert_same list[0], list[1] end def test_mapping_with_str_tag