Skip to content

Commit f894886

Browse files
committed
fix: Faster comment extraction
AppMap needs to extract method comments to apply labels. This change makes this process faster.
1 parent 98e4183 commit f894886

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

lib/appmap/trace.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ def source_location
2121
end
2222

2323
def comment
24-
@method.comment
25-
rescue MethodSource::SourceNotFoundError, Errno::EINVAL
24+
return nil if source_location.nil? || source_location.first.start_with?('<')
25+
26+
# Do not use method_source's comment method because it's slow
27+
@comment ||= RubyMethod.last_comment *source_location
28+
rescue Errno::EINVAL, Errno::ENOENT
2629
nil
2730
end
2831

@@ -37,6 +40,21 @@ def name
3740
def labels
3841
@package.labels
3942
end
43+
44+
private
45+
46+
# Read file and get last comment before line.
47+
def self.last_comment(file, line)
48+
File.open(file) do |f|
49+
buffer = []
50+
f.lines.lazy.take(line - 1).reverse_each do |line|
51+
break unless (line =~ /^\s*#/) || (line =~ /^\s*$/)
52+
53+
buffer << line.lstrip
54+
end
55+
buffer.reverse.join
56+
end
57+
end
4058
end
4159

4260
class Tracing

0 commit comments

Comments
 (0)