[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:
parent
46085ef970
commit
e6fa1d62fa
@ -15,30 +15,29 @@ module Psych
|
||||
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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user