Skip to content

Commit

Permalink
Use compare_by_identity instead of object_id
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
amomchilov committed Dec 18, 2023
1 parent 02b7ef3 commit df69e4a
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/psych/visitors/yaml_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit df69e4a

Please sign in to comment.