From 98e418346aa48abe6d17e6ca47dc6d28cf3f0719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Rzepecki?= Date: Fri, 16 Sep 2022 20:43:11 +0200 Subject: [PATCH 1/2] Update rubocop config --- .rubocop.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.rubocop.yml b/.rubocop.yml index 85570542..0f5ecf44 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -18,7 +18,7 @@ Layout/LineLength: Max: 120 Metrics/BlockLength: - ExcludedMethods: + AllowedMethods: - describe - it - context From f6199a607fadd58500d9cdc9394b39413e6b2930 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Rzepecki?= Date: Fri, 16 Sep 2022 20:48:27 +0200 Subject: [PATCH 2/2] fix: Faster comment extraction 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> --- lib/appmap/trace.rb | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/appmap/trace.rb b/lib/appmap/trace.rb index d5046777..43c7f6ab 100644 --- a/lib/appmap/trace.rb +++ b/lib/appmap/trace.rb @@ -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 @@ -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| + 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