Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split Prism::Loader#load_node in one lambda per node type #1710

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 62 additions & 25 deletions templates/lib/prism/serialize.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module Prism
@constant_pool = nil

@source = source
define_load_node_lambdas unless RUBY_ENGINE == 'ruby'
end

def load_encoding
Expand Down Expand Up @@ -194,32 +195,68 @@ module Prism
load_constant(index - 1) if index != 0
end

def load_node
type = io.getbyte
location = load_location
if RUBY_ENGINE == 'ruby'
def load_node
type = io.getbyte
location = load_location

case type
<%- nodes.each_with_index do |node, index| -%>
when <%= index + 1 %> then
<%- if node.needs_serialized_length? -%>
load_serialized_length
<%- end -%>
<%= node.name %>.new(<%= (node.fields.map { |field|
case field
when Prism::NodeField then "load_node"
when Prism::OptionalNodeField then "load_optional_node"
when Prism::StringField then "load_string"
when Prism::NodeListField then "Array.new(load_varint) { load_node }"
when Prism::ConstantField then "load_required_constant"
when Prism::OptionalConstantField then "load_optional_constant"
when Prism::ConstantListField then "Array.new(load_varint) { load_required_constant }"
when Prism::LocationField then "load_location"
when Prism::OptionalLocationField then "load_optional_location"
when Prism::UInt32Field, Prism::FlagsField then "load_varint"
else raise
end
} + ["location"]).join(", ") -%>)
<%- end -%>
end
end
else
def load_node
type = io.getbyte
@load_node_lambdas[type].call
end

case type
<%- nodes.each_with_index do |node, index| -%>
when <%= index + 1 %> then
<%- if node.needs_serialized_length? -%>
load_serialized_length
<%- end -%>
<%= node.name %>.new(<%= (node.fields.map { |field|
case field
when Prism::NodeField then "load_node"
when Prism::OptionalNodeField then "load_optional_node"
when Prism::StringField then "load_string"
when Prism::NodeListField then "Array.new(load_varint) { load_node }"
when Prism::ConstantField then "load_required_constant"
when Prism::OptionalConstantField then "load_optional_constant"
when Prism::ConstantListField then "Array.new(load_varint) { load_required_constant }"
when Prism::LocationField then "load_location"
when Prism::OptionalLocationField then "load_optional_location"
when Prism::UInt32Field, Prism::FlagsField then "load_varint"
else raise
end
} + ["location"]).join(", ") -%>)
<%- end -%>
def define_load_node_lambdas
@load_node_lambdas = [
nil,
<%- nodes.each do |node| -%>
-> {
location = load_location
<%- if node.needs_serialized_length? -%>
load_serialized_length
<%- end -%>
<%= node.name %>.new(<%= (node.fields.map { |field|
case field
when Prism::NodeField then "load_node"
when Prism::OptionalNodeField then "load_optional_node"
when Prism::StringField then "load_string"
when Prism::NodeListField then "Array.new(load_varint) { load_node }"
when Prism::ConstantField then "load_required_constant"
when Prism::OptionalConstantField then "load_optional_constant"
when Prism::ConstantListField then "Array.new(load_varint) { load_required_constant }"
when Prism::LocationField then "load_location"
when Prism::OptionalLocationField then "load_optional_location"
when Prism::UInt32Field, Prism::FlagsField then "load_varint"
else raise
end
} + ["location"]).join(", ") -%>)
},
<%- end -%>
]
end
end
end
Expand Down