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 authored and hsbt committed Dec 23, 2020
1 parent d486185 commit a2dd8d8
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion 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 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
3 changes: 3 additions & 0 deletions test/psych/test_ractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def test_not_shareable
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|
Expand Down

0 comments on commit a2dd8d8

Please sign in to comment.