-
-
Notifications
You must be signed in to change notification settings - Fork 398
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
Avoid repeated allocations of node-type array #1452
Conversation
lib/yard/parser/ruby/ruby_parser.rb
Outdated
@@ -612,8 +612,9 @@ def comment_starts_line?(charno) | |||
end | |||
|
|||
def insert_comments | |||
node_types = [:comment, :void_stmt, :list] |
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.
Does extracting this to a constant reduce allocations further?
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.
I would imagine this to be true, especially if that list was then frozen.
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.
Thanks for the suggestion. I implemented the change and reduced the number of allocations by ~80 or so.
I also switched the container to a Set
to avoid repeated O(n) look-ups.
@@ -613,7 +620,7 @@ def comment_starts_line?(charno) | |||
|
|||
def insert_comments | |||
root.traverse do |node| | |||
next if [:comment, :void_stmt, :list].include?(node.type) || node.parent.type != :list | |||
next if COMMENT_SKIP_NODE_TYPES.include?(node.type) || node.parent.type != :list |
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.
Because of how Set is implemented in MRI, the array implementation is likely to be faster at this scale. (set
is also not a prior dependency of yard
, although that is moot as of Ruby 3.2)
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.
Thanks for the review. I am now using an array again.
Getting around to these. Thanks for the contribution! |
Description
Hi, I am trying to optimize YARD a bit to speed up documentation runs on a large codebase.
The codebase is private, so I am using https://github.com/watir/watir/ as a smaller stand-in to report benchmarking numbers from https://github.com/SamSaffron/memory_profiler.
Test set-up
rvm
on Linux)ruby-memory-profiler --no-color --retained-strings=500 --allocated-strings=500 --max=300 -o prof.log ./run-yard.rb stats
run-yard.rb
is a smaller version of/bin/yard
(MemoryProfiler was not able to run/bin/yard
)stats
command to avoid memory exhaustionPerformance data
Completed Tasks
bundle exec rake
locally (if code is attached to PR).