-
Notifications
You must be signed in to change notification settings - Fork 13
fix: Faster comment extraction #283
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
Conversation
c0a5518 to
f894886
Compare
|
|
||
| # Read file and get last comment before line. | ||
| def self.last_comment(file, line) | ||
| File.open(file) do |f| |
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.
The other implementation cached file contents. Adding caching here might be only a few lines.
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.
line is used twice, as an input parameter and do parameter.
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.
The other implementation cached the whole file per method, which is a bit of a waste of memory. This one caches just the comment.
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 tried to test this with method_source testcases and got
$ ruby --version
ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [x86_64-linux]
$ bundle exec rspec spec
1) MethodSource Methods should return a comment for method
Failure/Error:
f.lines.lazy.take(line_number - 1).reverse_each do |line|
break unless (line =~ /^\s*#/) || (line =~ /^\s*$/)
buffer << line.lstrip
end
NoMethodError:
undefined method `lines' for #<File:/home/test/src/method_source/spec/spec_helper.rb (closed)>
Did you mean? lineno
# ./lib/method_source.rb:52:in `block in comment_describing_custom'
# ./lib/method_source.rb:49:in `open'
# ./lib/method_source.rb:49:in `comment_describing_custom'
# ./lib/method_source.rb:44:in `comment_helper'
# ./lib/method_source.rb:143:in `comment'
# ./spec/method_source_spec.rb:69:in `block (3 levels) in <top (required)>'
Maybe f.lines doesn't exist in Ruby 3.0?
Which version of Ruby did you use?
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.
In ruby 2.6.10, 2.7.2 and 3.0.1 it works with
f.each_line.lazy.take(line_number - 1).reverse_each do |line|
AppMap needs to extract method comments to apply labels. This change makes this process faster. Co-authored-by: symwell <111290954+symwell@users.noreply.github.com>
46fe4ad to
f6199a6
Compare
|
Thanks @symwell, good catch! I've merged your changes into my commit, please take a look. |
symwell
left a comment
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.
Verified locally that it works.
|
🎉 This PR is included in version 0.90.1 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
AppMap needs to extract method comments to apply labels. This change makes this process faster.
Note this is roughly the same change as in #279 but (IMO) a bit cleaner, plus it avoids reading the whole file by using IO#lines iterator and only taking the required number of lines. As such it fixes #276 (in a way).