From ed9af6eb7c256783676bb33d8c2648d585828cfd Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Tue, 22 Dec 2020 21:29:00 -0500 Subject: [PATCH] [ruby/psych] Optimize cache with `compare_by_identity` Using `compare_by_identity` gives a 4x performance boost on cache hits. Benchmark in https://github.com/JuanitoFatas/fast-ruby/issues/189 --- ext/psych/lib/psych/visitors/visitor.rb | 2 +- ext/psych/lib/psych/visitors/yaml_tree.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/psych/lib/psych/visitors/visitor.rb b/ext/psych/lib/psych/visitors/visitor.rb index 35f8f81f18446f..e2585c0c774624 100644 --- a/ext/psych/lib/psych/visitors/visitor.rb +++ b/ext/psych/lib/psych/visitors/visitor.rb @@ -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) diff --git a/ext/psych/lib/psych/visitors/yaml_tree.rb b/ext/psych/lib/psych/visitors/yaml_tree.rb index 986c57be70309c..ac6777aeb5d838 100644 --- a/ext/psych/lib/psych/visitors/yaml_tree.rb +++ b/ext/psych/lib/psych/visitors/yaml_tree.rb @@ -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