ruby-changes:64508
From: Marc-Andre <ko1@a...>
Date: Wed, 23 Dec 2020 15:08:59 +0900 (JST)
Subject: [ruby-changes:64508] daec109f42 (master): [ruby/psych] Optimize cache with `compare_by_identity`
https://git.ruby-lang.org/ruby.git/commit/?id=daec109f42 From daec109f423e54094800e083fc8a8ca5cbceb866 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune <github@m...> Date: Tue, 22 Dec 2020 21:29:00 -0500 Subject: [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 diff --git a/ext/psych/lib/psych/visitors/visitor.rb b/ext/psych/lib/psych/visitors/visitor.rb index 35f8f81..e2585c0 100644 --- a/ext/psych/lib/psych/visitors/visitor.rb +++ b/ext/psych/lib/psych/visitors/visitor.rb @@ -12,7 +12,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/visitor.rb#L12 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 986c57b..ac6777a 100644 --- a/ext/psych/lib/psych/visitors/yaml_tree.rb +++ b/ext/psych/lib/psych/visitors/yaml_tree.rb @@ -80,7 +80,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/yaml_tree.rb#L80 raise(TypeError, "Can't dump #{target.class}") unless method h[klass] = method - end + end.compare_by_identity end def start encoding = Nodes::Stream::UTF8 diff --git a/test/psych/test_ractor.rb b/test/psych/test_ractor.rb index 90e4036..2f8d5ca 100644 --- a/test/psych/test_ractor.rb +++ b/test/psych/test_ractor.rb @@ -24,6 +24,9 @@ class TestPsychRactor < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/psych/test_ractor.rb#L24 end def test_ractor_config + # Config is ractor-local + # Test is to make sure it works, even though usage is probably very low. + # The methods are not documented and might be deprecated one day assert_ractor(<<~RUBY, require_relative: 'helper') r = Ractor.new do Psych.add_builtin_type 'omap' do |type, val| -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/