Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Layout/LineLength:
Max: 120

Metrics/BlockLength:
ExcludedMethods:
AllowedMethods:
- describe
- it
- context
Expand Down
22 changes: 20 additions & 2 deletions lib/appmap/trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ def source_location
end

def comment
@method.comment
rescue MethodSource::SourceNotFoundError, Errno::EINVAL
return nil if source_location.nil? || source_location.first.start_with?('<')

# Do not use method_source's comment method because it's slow
@comment ||= RubyMethod.last_comment *source_location
rescue Errno::EINVAL, Errno::ENOENT
nil
end

Expand All @@ -37,6 +40,21 @@ def name
def labels
@package.labels
end

private

# Read file and get last comment before line.
def self.last_comment(file, line_number)
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|

buffer = []
f.each_line.lazy.take(line_number - 1).reverse_each do |line|
break unless (line =~ /^\s*#/) || (line =~ /^\s*$/)

buffer << line.lstrip
end
buffer.reverse.join
end
end
end

class Tracing
Expand Down