Skip to content

Commit a592ec3

Browse files
committed
Split Prism::Loader#load_node in one lambda per node type
* Otherwise load_node is too big to compile and is forced to run in interpreter: oracle/truffleruby#3293 (comment) * For the benchmark at oracle/truffleruby#3293 (comment) TruffleRuby Native 23.1.0: Before: 10.574041 After: 5.592436 JRuby 9.4.3.0: Before: 7.037780 After: 3.995317 JRuby 9.4.3.0 -Xcompile.invokedynamic=true: Before: 7.047832 After: 2.269294
1 parent d088c0c commit a592ec3

File tree

1 file changed

+62
-25
lines changed

1 file changed

+62
-25
lines changed

templates/lib/prism/serialize.rb.erb

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module Prism
4747
@constant_pool = nil
4848

4949
@source = source
50+
define_load_node_lambdas unless RUBY_ENGINE == 'ruby'
5051
end
5152

5253
def load_encoding
@@ -194,32 +195,68 @@ module Prism
194195
load_constant(index - 1) if index != 0
195196
end
196197

197-
def load_node
198-
type = io.getbyte
199-
location = load_location
198+
if RUBY_ENGINE == 'ruby'
199+
def load_node
200+
type = io.getbyte
201+
location = load_location
202+
203+
case type
204+
<%- nodes.each_with_index do |node, index| -%>
205+
when <%= index + 1 %> then
206+
<%- if node.needs_serialized_length? -%>
207+
load_serialized_length
208+
<%- end -%>
209+
<%= node.name %>.new(<%= (node.fields.map { |field|
210+
case field
211+
when Prism::NodeField then "load_node"
212+
when Prism::OptionalNodeField then "load_optional_node"
213+
when Prism::StringField then "load_string"
214+
when Prism::NodeListField then "Array.new(load_varint) { load_node }"
215+
when Prism::ConstantField then "load_required_constant"
216+
when Prism::OptionalConstantField then "load_optional_constant"
217+
when Prism::ConstantListField then "Array.new(load_varint) { load_required_constant }"
218+
when Prism::LocationField then "load_location"
219+
when Prism::OptionalLocationField then "load_optional_location"
220+
when Prism::UInt32Field, Prism::FlagsField then "load_varint"
221+
else raise
222+
end
223+
} + ["location"]).join(", ") -%>)
224+
<%- end -%>
225+
end
226+
end
227+
else
228+
def load_node
229+
type = io.getbyte
230+
@load_node_lambdas[type].call
231+
end
200232

201-
case type
202-
<%- nodes.each_with_index do |node, index| -%>
203-
when <%= index + 1 %> then
204-
<%- if node.needs_serialized_length? -%>
205-
load_serialized_length
206-
<%- end -%>
207-
<%= node.name %>.new(<%= (node.fields.map { |field|
208-
case field
209-
when Prism::NodeField then "load_node"
210-
when Prism::OptionalNodeField then "load_optional_node"
211-
when Prism::StringField then "load_string"
212-
when Prism::NodeListField then "Array.new(load_varint) { load_node }"
213-
when Prism::ConstantField then "load_required_constant"
214-
when Prism::OptionalConstantField then "load_optional_constant"
215-
when Prism::ConstantListField then "Array.new(load_varint) { load_required_constant }"
216-
when Prism::LocationField then "load_location"
217-
when Prism::OptionalLocationField then "load_optional_location"
218-
when Prism::UInt32Field, Prism::FlagsField then "load_varint"
219-
else raise
220-
end
221-
} + ["location"]).join(", ") -%>)
222-
<%- end -%>
233+
def define_load_node_lambdas
234+
@load_node_lambdas = [
235+
nil,
236+
<%- nodes.each do |node| -%>
237+
-> {
238+
location = load_location
239+
<%- if node.needs_serialized_length? -%>
240+
load_serialized_length
241+
<%- end -%>
242+
<%= node.name %>.new(<%= (node.fields.map { |field|
243+
case field
244+
when Prism::NodeField then "load_node"
245+
when Prism::OptionalNodeField then "load_optional_node"
246+
when Prism::StringField then "load_string"
247+
when Prism::NodeListField then "Array.new(load_varint) { load_node }"
248+
when Prism::ConstantField then "load_required_constant"
249+
when Prism::OptionalConstantField then "load_optional_constant"
250+
when Prism::ConstantListField then "Array.new(load_varint) { load_required_constant }"
251+
when Prism::LocationField then "load_location"
252+
when Prism::OptionalLocationField then "load_optional_location"
253+
when Prism::UInt32Field, Prism::FlagsField then "load_varint"
254+
else raise
255+
end
256+
} + ["location"]).join(", ") -%>)
257+
},
258+
<%- end -%>
259+
]
223260
end
224261
end
225262
end

0 commit comments

Comments
 (0)