Skip to content

Commit

Permalink
Merge pull request #474 from Shopify/cache-load-types-in-to-ruby
Browse files Browse the repository at this point in the history
Avoid repeated access to Ractor.current
  • Loading branch information
marcandre authored Jan 30, 2021
2 parents 0abce07 + 285c461 commit a2721a0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/psych/visitors/to_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def initialize ss, class_loader, symbolize_names: false, freeze: false
super()
@st = {}
@ss = ss
@load_tags = Psych.load_tags
@domain_types = Psych.domain_types
@class_loader = class_loader
@symbolize_names = symbolize_names
Expand All @@ -48,7 +49,7 @@ def accept target
end

def deserialize o
if klass = resolve_class(Psych.load_tags[o.tag])
if klass = resolve_class(@load_tags[o.tag])
instance = klass.allocate

if instance.respond_to?(:init_with)
Expand Down Expand Up @@ -128,7 +129,7 @@ def visit_Psych_Nodes_Scalar o
end

def visit_Psych_Nodes_Sequence o
if klass = resolve_class(Psych.load_tags[o.tag])
if klass = resolve_class(@load_tags[o.tag])
instance = klass.allocate

if instance.respond_to?(:init_with)
Expand Down Expand Up @@ -160,8 +161,8 @@ def visit_Psych_Nodes_Sequence o
end

def visit_Psych_Nodes_Mapping o
if Psych.load_tags[o.tag]
return revive(resolve_class(Psych.load_tags[o.tag]), o)
if @load_tags[o.tag]
return revive(resolve_class(@load_tags[o.tag]), o)
end
return revive_hash(register(o, {}), o) unless o.tag

Expand Down Expand Up @@ -326,6 +327,7 @@ def visit_Psych_Nodes_Alias o
end

private

def register node, object
@st[node.anchor] = object if node.anchor
object
Expand Down
2 changes: 1 addition & 1 deletion lib/psych/visitors/visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.dispatch_cache

if defined?(Ractor)
def dispatch
Ractor.current[:Psych_Visitors_Visitor] ||= Visitor.dispatch_cache
@dispatch_cache ||= (Ractor.current[:Psych_Visitors_Visitor] ||= Visitor.dispatch_cache)
end
else
DISPATCH = dispatch_cache
Expand Down

0 comments on commit a2721a0

Please sign in to comment.