-
Notifications
You must be signed in to change notification settings - Fork 177
Optimize ruby visitor #3826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize ruby visitor #3826
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -187,7 +187,7 @@ module Prism | |
| while (node = queue.shift) | ||
| result << node | ||
|
|
||
| node.compact_child_nodes.each do |child_node| | ||
| node.each_child_node do |child_node| | ||
| child_location = child_node.location | ||
|
|
||
| start_line = child_location.start_line | ||
|
|
@@ -259,6 +259,13 @@ module Prism | |
|
|
||
| alias deconstruct child_nodes | ||
|
|
||
| # With a block given, yields each child node. Without a block, returns | ||
| # an enumerator that contains each child node. Excludes any `nil`s in | ||
| # the place of optional nodes that were not present. | ||
| def each_child_node | ||
| raise NoMethodError, "undefined method `each_child_node' for #{inspect}" | ||
| end | ||
|
|
||
| # Returns an array of child nodes, excluding any `nil`s in the place of | ||
| # optional nodes that were not present. | ||
| def compact_child_nodes | ||
|
|
@@ -335,6 +342,22 @@ module Prism | |
| }.compact.join(", ") %>] | ||
| end | ||
|
|
||
| # def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator[Prism::node] | ||
| def each_child_node | ||
| return to_enum(:each_child_node) unless block_given? | ||
|
|
||
| <%- node.fields.each do |field| -%> | ||
| <%- case field -%> | ||
| <%- when Prism::Template::NodeField -%> | ||
| yield <%= field.name %> | ||
| <%- when Prism::Template::OptionalNodeField -%> | ||
| yield <%= field.name %> if <%= field.name %> | ||
| <%- when Prism::Template::NodeListField -%> | ||
| <%= field.name %>.each {|node| yield node } | ||
| <%- end -%> | ||
| <%- end -%> | ||
| end | ||
|
|
||
| # def compact_child_nodes: () -> Array[Node] | ||
| def compact_child_nodes | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can just be implemented as
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, you're right to keep this the same. |
||
| <%- if node.fields.any? { |field| field.is_a?(Prism::Template::OptionalNodeField) } -%> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ module Prism | |
| def child_nodes: () -> Array[Prism::node?] | ||
| def comment_targets: () -> Array[Prism::node | Location] | ||
| def compact_child_nodes: () -> Array[Prism::node] | ||
| def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator[Prism::node] | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unsure if this is correct. I'm also missing sorbet signatures which I don't know how to write for this
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks right to me, and it would have failed to typecheck otherwise.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So are sorbet signatures not needed? I don't use these systems so no idea when/where it gets used. Other functions are present so I assume there is a reason for them. |
||
| def self.fields: () -> Array[Prism::Reflection::Field] | ||
| def type: () -> Symbol | ||
| def self.type: () -> Symbol | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be faster because it avoids an extra block (that literal block) and consumes less stack (both good for performance and to avoid stack overflow errors).
I'll give it a try.