Skip to content

Commit df69e4a

Browse files
committed
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.
1 parent 02b7ef3 commit df69e4a

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

lib/psych/visitors/yaml_tree.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,29 @@ module Visitors
1515
class YAMLTree < Psych::Visitors::Visitor
1616
class Registrar # :nodoc:
1717
def initialize
18-
@obj_to_id = {}
19-
@obj_to_node = {}
18+
@obj_to_id = {}.compare_by_identity
19+
@obj_to_node = {}.compare_by_identity
2020
@targets = []
2121
@counter = 0
2222
end
2323

2424
def register target, node
25-
return unless target.respond_to? :object_id
2625
@targets << target
27-
@obj_to_node[target.object_id] = node
26+
@obj_to_node[target] = node
2827
end
2928

3029
def key? target
31-
@obj_to_node.key? target.object_id
30+
@obj_to_node.key? target
3231
rescue NoMethodError
3332
false
3433
end
3534

3635
def id_for target
37-
@obj_to_id[target.object_id] ||= (@counter += 1)
36+
@obj_to_id[target] ||= (@counter += 1)
3837
end
3938

4039
def node_for target
41-
@obj_to_node[target.object_id]
40+
@obj_to_node[target]
4241
end
4342
end
4443

0 commit comments

Comments
 (0)