diff options
author | Ufuk Kayserilioglu <[email protected]> | 2025-05-22 16:32:04 +0300 |
---|---|---|
committer | git <[email protected]> | 2025-05-23 14:22:47 +0000 |
commit | 224a02f924967066fc5c784c2f4a75eea52b11b4 (patch) | |
tree | dff7ae613748c333e89350053551f3a91ccd400a | |
parent | 70f8f7c4b11da8e237dc312a76ac49ebe0b66333 (diff) |
[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
-rw-r--r-- | prism/templates/lib/prism/compiler.rb.erb | 4 | ||||
-rw-r--r-- | 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 |