[ruby/prism] Monomorphise visitor methods

The current implementation of the visitor pattern in Prism uses
a single method (`visit_child_nodes`) to handle all node types. This can lead to performance issues since the `node` argument will end up being polymorphic, and will prevent effective use of inline caches, which in CRuby are monomorphic.

This commit generates an inlined version of the previous code for each node type, thus making the calls inside visitor methods monomorphic. This should improve performance, especially in cases where the visitor is called frequently.

https://github.com/ruby/prism/commit/60d324a701
This commit is contained in:
Ufuk Kayserilioglu 2025-05-22 16:32:04 +03:00 committed by git
parent 70f8f7c4b1
commit 224a02f924
2 changed files with 6 additions and 2 deletions

View File

@ -35,7 +35,9 @@ module Prism
<%- nodes.each_with_index do |node, index| -%>
<%= "\n" if index != 0 -%>
# Compile a <%= node.name %> node
alias visit_<%= node.human %> visit_child_nodes
def visit_<%= node.human %>(node)
node.compact_child_nodes.map { |node| node.accept(self) }
end
<%- end -%>
end
end

View File

@ -47,7 +47,9 @@ module Prism
<%- nodes.each_with_index do |node, index| -%>
<%= "\n" if index != 0 -%>
# Visit a <%= node.name %> node
alias visit_<%= node.human %> visit_child_nodes
def visit_<%= node.human %>(node)
node.compact_child_nodes.each { |node| node.accept(self) }
end
<%- end -%>
end
end