From 224a02f924967066fc5c784c2f4a75eea52b11b4 Mon Sep 17 00:00:00 2001 From: Ufuk Kayserilioglu Date: Thu, 22 May 2025 16:32:04 +0300 Subject: [PATCH] [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 --- prism/templates/lib/prism/compiler.rb.erb | 4 +++- prism/templates/lib/prism/visitor.rb.erb | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/prism/templates/lib/prism/compiler.rb.erb b/prism/templates/lib/prism/compiler.rb.erb index 45ed88d8de..9102025c20 100644 --- a/prism/templates/lib/prism/compiler.rb.erb +++ b/prism/templates/lib/prism/compiler.rb.erb @@ -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 diff --git a/prism/templates/lib/prism/visitor.rb.erb b/prism/templates/lib/prism/visitor.rb.erb index 4b30a1815b..a1eac38dc4 100644 --- a/prism/templates/lib/prism/visitor.rb.erb +++ b/prism/templates/lib/prism/visitor.rb.erb @@ -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