Skip to content

Commit 606cea1

Browse files
authored
Merge pull request #283 from applandinc/fix/faster-comment-extraction
Faster comment extraction
2 parents fdc72aa + f6199a6 commit 606cea1

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Layout/LineLength:
1818
Max: 120
1919

2020
Metrics/BlockLength:
21-
ExcludedMethods:
21+
AllowedMethods:
2222
- describe
2323
- it
2424
- context

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_number)
48+
File.open(file) do |f|
49+
buffer = []
50+
f.each_line.lazy.take(line_number - 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)