From df69e4a12efaf6b21c08f57d9cc7512a6aed8674 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Mon, 18 Dec 2023 04:00:48 -0500 Subject: [PATCH] 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