Skip to content

Commit f6e0ad1

Browse files
authored
perf: Use FilenameCache in profilers (#2919)
1 parent e2a073a commit f6e0ad1

File tree

7 files changed

+28
-23
lines changed

7 files changed

+28
-23
lines changed

sentry-ruby/lib/sentry/interfaces/stacktrace_builder.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class StacktraceBuilder
2222
# @return [Boolean]
2323
attr_reader :strip_backtrace_load_path
2424

25+
# @return [FilenameCache]
26+
attr_reader :filename_cache
27+
2528
# @param project_root [String]
2629
# @param app_dirs_pattern [Regexp, nil]
2730
# @param linecache [LineCache]

sentry-ruby/lib/sentry/profiler.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def initialize(configuration)
2626
@project_root = configuration.project_root
2727
@app_dirs_pattern = configuration.app_dirs_pattern
2828
@in_app_pattern = Regexp.new("^(#{@project_root}/)?#{@app_dirs_pattern}")
29+
@filename_cache = configuration.stacktrace_builder.filename_cache
2930
end
3031

3132
def start

sentry-ruby/lib/sentry/profiler/helpers.rb

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,8 @@ def in_app?(abs_path)
99
abs_path.match?(@in_app_pattern)
1010
end
1111

12-
# copied from stacktrace.rb since I don't want to touch existing code
13-
# TODO-neel-profiler try to fetch this from stackprof once we patch
14-
# the native extension
1512
def compute_filename(abs_path, in_app)
16-
return nil if abs_path.nil?
17-
18-
under_project_root = @project_root && abs_path.start_with?(@project_root)
19-
20-
prefix =
21-
if under_project_root && in_app
22-
@project_root
23-
else
24-
longest_load_path = $LOAD_PATH.select { |path| abs_path.start_with?(path.to_s) }.max_by(&:size)
25-
26-
if under_project_root
27-
longest_load_path || @project_root
28-
else
29-
longest_load_path
30-
end
31-
end
32-
33-
prefix ? abs_path[prefix.to_s.chomp(File::SEPARATOR).length + 1..-1] : abs_path
13+
@filename_cache.compute_filename(abs_path, in_app, true)
3414
end
3515

3616
def split_module(name)

sentry-ruby/lib/sentry/utils/filename_cache.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
module Sentry
44
class FilenameCache
5+
attr_reader :cache
6+
57
def initialize(project_root)
68
@project_root = project_root
79
@load_paths = $LOAD_PATH.map(&:to_s).sort_by(&:size).reverse.freeze

sentry-ruby/lib/sentry/vernier/output.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ class Output
1010

1111
attr_reader :profile
1212

13-
def initialize(profile, project_root:, in_app_pattern:, app_dirs_pattern:)
13+
def initialize(profile, project_root:, in_app_pattern:, app_dirs_pattern:, filename_cache:)
1414
@profile = profile
1515
@project_root = project_root
1616
@in_app_pattern = in_app_pattern
1717
@app_dirs_pattern = app_dirs_pattern
18+
@filename_cache = filename_cache
1819
end
1920

2021
def to_h

sentry-ruby/lib/sentry/vernier/profiler.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def initialize(configuration)
2424
@project_root = configuration.project_root
2525
@app_dirs_pattern = configuration.app_dirs_pattern
2626
@in_app_pattern = Regexp.new("^(#{@project_root}/)?#{@app_dirs_pattern}")
27+
@filename_cache = configuration.stacktrace_builder.filename_cache
2728
end
2829

2930
def set_initial_sample_decision(transaction_sampled)
@@ -125,7 +126,8 @@ def output
125126
result,
126127
project_root: @project_root,
127128
app_dirs_pattern: @app_dirs_pattern,
128-
in_app_pattern: @in_app_pattern
129+
in_app_pattern: @in_app_pattern,
130+
filename_cache: @filename_cache
129131
)
130132
end
131133
end

sentry-ruby/spec/sentry/profiler_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,22 @@
320320
last_elapsed = elapsed
321321
end
322322
end
323+
324+
it 'reuses filename_cache entries with stacktrace_builder' do
325+
filename_cache = Sentry.configuration.stacktrace_builder.filename_cache
326+
spec_path = "#{Dir.pwd}/spec/sentry/profiler_spec.rb"
327+
328+
# building a stacktrace populates the cache
329+
Sentry.configuration.stacktrace_builder.build(
330+
backtrace: ["#{spec_path}:7:in `foo'"]
331+
)
332+
333+
expect(filename_cache.cache).to have_key(spec_path)
334+
335+
# profiler reuses the cached entry instead of recomputing
336+
expect(filename_cache).not_to receive(:longest_load_path).with(spec_path)
337+
subject.to_h
338+
end
323339
end
324340
end
325341
end

0 commit comments

Comments
 (0)