[ruby/psych] 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.

https://github.com/ruby/psych/commit/df69e4a12e
This commit is contained in:
Alexander Momchilov 2023-12-18 04:00:48 -05:00 committed by Hiroshi SHIBATA
parent 46085ef970
commit e6fa1d62fa

View File

@ -15,30 +15,29 @@ module Psych
class YAMLTree < Psych::Visitors::Visitor class YAMLTree < Psych::Visitors::Visitor
class Registrar # :nodoc: class Registrar # :nodoc:
def initialize def initialize
@obj_to_id = {} @obj_to_id = {}.compare_by_identity
@obj_to_node = {} @obj_to_node = {}.compare_by_identity
@targets = [] @targets = []
@counter = 0 @counter = 0
end end
def register target, node def register target, node
return unless target.respond_to? :object_id
@targets << target @targets << target
@obj_to_node[target.object_id] = node @obj_to_node[target] = node
end end
def key? target def key? target
@obj_to_node.key? target.object_id @obj_to_node.key? target
rescue NoMethodError rescue NoMethodError
false false
end end
def id_for target def id_for target
@obj_to_id[target.object_id] ||= (@counter += 1) @obj_to_id[target] ||= (@counter += 1)
end end
def node_for target def node_for target
@obj_to_node[target.object_id] @obj_to_node[target]
end end
end end