Skip to content

Commit 224a02f

Browse files
paracyclematzbot
authored andcommitted
[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. ruby/prism@60d324a701
1 parent 70f8f7c commit 224a02f

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

prism/templates/lib/prism/compiler.rb.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ module Prism
3535
<%- nodes.each_with_index do |node, index| -%>
3636
<%= "\n" if index != 0 -%>
3737
# Compile a <%= node.name %> node
38-
alias visit_<%= node.human %> visit_child_nodes
38+
def visit_<%= node.human %>(node)
39+
node.compact_child_nodes.map { |node| node.accept(self) }
40+
end
3941
<%- end -%>
4042
end
4143
end

prism/templates/lib/prism/visitor.rb.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ module Prism
4747
<%- nodes.each_with_index do |node, index| -%>
4848
<%= "\n" if index != 0 -%>
4949
# Visit a <%= node.name %> node
50-
alias visit_<%= node.human %> visit_child_nodes
50+
def visit_<%= node.human %>(node)
51+
node.compact_child_nodes.each { |node| node.accept(self) }
52+
end
5153
<%- end -%>
5254
end
5355
end

0 commit comments

Comments
 (0)