[ruby/prism] 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:
  https://github.com/oracle/truffleruby/issues/3293#issuecomment-1759730996
* For the benchmark at https://github.com/oracle/truffleruby/issues/3293#issuecomment-1759790280
  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

a592ec346a
This commit is contained in:
Benoit Daloze 2023-10-12 16:54:44 +02:00 committed by git
parent 51ea82a770
commit 631ddb34e4

View file

@ -47,6 +47,7 @@ module Prism
@constant_pool = nil @constant_pool = nil
@source = source @source = source
define_load_node_lambdas unless RUBY_ENGINE == 'ruby'
end end
def load_encoding def load_encoding
@ -194,6 +195,7 @@ module Prism
load_constant(index - 1) if index != 0 load_constant(index - 1) if index != 0
end end
if RUBY_ENGINE == 'ruby'
def load_node def load_node
type = io.getbyte type = io.getbyte
location = load_location location = load_location
@ -222,6 +224,41 @@ module Prism
<%- end -%> <%- end -%>
end end
end end
else
def load_node
type = io.getbyte
@load_node_lambdas[type].call
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 end
TOKEN_TYPES = [ TOKEN_TYPES = [