Skip to content

Commit

Permalink
[ruby/psych] Optimize cache with compare_by_identity
Browse files Browse the repository at this point in the history
Using `compare_by_identity` gives a 4x performance boost on cache hits.
Benchmark in fastruby/fast-ruby#189
  • Loading branch information
marcandre committed Dec 23, 2020
1 parent 3a85cdb commit 1976640
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ext/psych/lib/psych/visitors/visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def accept target
def self.dispatch_cache
Hash.new do |hash, klass|
hash[klass] = :"visit_#{klass.name.gsub('::', '_')}"
end
end.compare_by_identity
end

if defined?(Ractor)
Expand Down
2 changes: 1 addition & 1 deletion ext/psych/lib/psych/visitors/yaml_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def initialize emitter, ss, options
raise(TypeError, "Can't dump #{target.class}") unless method

h[klass] = method
end
end.compare_by_identity
end

def start encoding = Nodes::Stream::UTF8
Expand Down

0 comments on commit 1976640

Please sign in to comment.