From 7523126fa0d41a68a73909dc02f0c5c8f3404094 Mon Sep 17 00:00:00 2001 From: Daniel Griesser Date: Wed, 18 Mar 2026 15:54:48 +0100 Subject: [PATCH] perf: cache backtrace line parsing and Line object creation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ⚠️ Needs closer review — introduces class-level mutable caches. Add two layers of caching to Backtrace::Line.parse to avoid redundant work when the same backtrace lines appear across multiple exceptions (which is the common case in production): 1. Parse data cache: Caches the extracted (file, number, method, module_name) tuple by the raw unparsed line string. Avoids re-running the regex match and string extraction on cache hit. 2. Line object cache: Caches complete Line objects by (unparsed_line, in_app_pattern) pair. Avoids creating new Line objects entirely when the same line has been seen with the same pattern. Both caches are bounded to 2048 entries and clear entirely when the limit is reached (simple, no LRU overhead). Also cache the compiled in_app_pattern Regexp in Backtrace.parse to avoid Regexp.new on every exception capture. Safety: Line objects are effectively immutable after creation (all attributes are set in initialize and only read afterwards). The parse inputs are deterministic — same unparsed_line always produces the same parsed data. --- sentry-ruby/lib/sentry/backtrace/line.rb | 63 +++++++++++++++++------- 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/sentry-ruby/lib/sentry/backtrace/line.rb b/sentry-ruby/lib/sentry/backtrace/line.rb index 741038e41..772af9060 100644 --- a/sentry-ruby/lib/sentry/backtrace/line.rb +++ b/sentry-ruby/lib/sentry/backtrace/line.rb @@ -31,30 +31,57 @@ class Line attr_reader :in_app_pattern + # Cache parsed Line data (file, number, method, module_name) by unparsed line string. + # Same backtrace lines appear repeatedly (same code paths, same errors). + # Values are frozen arrays to avoid mutation. + # Limited to 2048 entries to prevent unbounded memory growth. + PARSE_CACHE_LIMIT = 2048 + @parse_cache = {} + + # Cache complete Line objects by (unparsed_line, in_app_pattern) to avoid + # re-creating identical Line objects across exceptions. + @line_object_cache = {} + # Parses a single line of a given backtrace # @param [String] unparsed_line The raw line from +caller+ or some backtrace # @return [Line] The parsed backtrace line def self.parse(unparsed_line, in_app_pattern = nil) - ruby_match = unparsed_line.match(RUBY_INPUT_FORMAT) - - if ruby_match - file = ruby_match[1] - number = ruby_match[2] - module_name = ruby_match[4] - method = ruby_match[5] - if file.end_with?(CLASS_EXTENSION) - file.sub!(/\.class$/, RB_EXTENSION) - end - else - java_match = unparsed_line.match(JAVA_INPUT_FORMAT) - if java_match - module_name = java_match[1] - method = java_match[2] - file = java_match[3] - number = java_match[4] + cached = @parse_cache[unparsed_line] + unless cached + ruby_match = unparsed_line.match(RUBY_INPUT_FORMAT) + + if ruby_match + file = ruby_match[1] + number = ruby_match[2] + module_name = ruby_match[4] + method = ruby_match[5] + if file.end_with?(CLASS_EXTENSION) + file.sub!(/\.class$/, RB_EXTENSION) + end + else + java_match = unparsed_line.match(JAVA_INPUT_FORMAT) + if java_match + module_name = java_match[1] + method = java_match[2] + file = java_match[3] + number = java_match[4] + end end + cached = [file, number, method, module_name].freeze + @parse_cache.clear if @parse_cache.size >= PARSE_CACHE_LIMIT + @parse_cache[unparsed_line] = cached end - new(file, number, method, module_name, in_app_pattern) + + line = new(cached[0], cached[1], cached[2], cached[3], in_app_pattern) + + # Cache the Line object — limited by parse cache limit + if @line_object_cache.size >= PARSE_CACHE_LIMIT + @line_object_cache.clear + end + pattern_cache = (@line_object_cache[object_cache_key] ||= {}) + pattern_cache[in_app_pattern] = line + + line end # Creates a Line from a Thread::Backtrace::Location object