Skip to content

Conversation

@dividedmind
Copy link
Contributor

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).


# Read file and get last comment before line.
def self.last_comment(file, line)
File.open(file) do |f|
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

@symwell symwell Sep 16, 2022

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?

Copy link
Contributor

@symwell symwell Sep 16, 2022

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>
@dividedmind dividedmind force-pushed the fix/faster-comment-extraction branch from 46fe4ad to f6199a6 Compare September 18, 2022 09:51
@dividedmind dividedmind requested a review from symwell September 18, 2022 09:52
@dividedmind
Copy link
Contributor Author

Thanks @symwell, good catch! I've merged your changes into my commit, please take a look.

Copy link
Contributor

@symwell symwell left a 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.

@kgilpin kgilpin changed the title Faster comment extraction fix: Faster comment extraction Sep 19, 2022
@kgilpin kgilpin merged commit 606cea1 into master Sep 19, 2022
@kgilpin kgilpin deleted the fix/faster-comment-extraction branch September 19, 2022 14:06
@appland-release
Copy link

🎉 This PR is included in version 0.90.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Take the method_source code we need and copy it in AppMap, to not depend on method_source or conflict with it in the wild

5 participants