-
-
Notifications
You must be signed in to change notification settings - Fork 533
perf: cache backtrace line parsing and Line object creation #2905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,21 +30,55 @@ 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 = {} | ||
|
Comment on lines
+38
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The new caching mechanisms in Suggested FixReplace the plain Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thread-unsafe plain Hash caches risk corruption on JRubyMedium Severity The three new class-level caches ( Additional Locations (1) |
||
|
|
||
| # 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) | ||
| # Try full Line object cache first (avoids creating new objects entirely) | ||
| object_cache_key = unparsed_line | ||
| pattern_cache = @line_object_cache[object_cache_key] | ||
| if pattern_cache | ||
| cached_line = pattern_cache[in_app_pattern] | ||
| return cached_line if cached_line | ||
| end | ||
|
|
||
| if ruby_match | ||
| _, file, number, _, module_name, method = ruby_match.to_a | ||
| file.sub!(/\.class$/, RB_EXTENSION) | ||
| module_name = module_name | ||
| else | ||
| java_match = unparsed_line.match(JAVA_INPUT_FORMAT) | ||
| _, module_name, method, file, number = java_match.to_a | ||
| cached = @parse_cache[unparsed_line] | ||
| unless cached | ||
| ruby_match = unparsed_line.match(RUBY_INPUT_FORMAT) | ||
|
|
||
| if ruby_match | ||
| _, file, number, _, module_name, method = ruby_match.to_a | ||
| file.sub!(/\.class$/, RB_EXTENSION) | ||
| else | ||
| java_match = unparsed_line.match(JAVA_INPUT_FORMAT) | ||
| _, module_name, method, file, number = java_match.to_a | ||
| 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 | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cache key omits
project_root, returning stale RegexpHigh Severity
The
@in_app_pattern_cachekey is onlyapp_dirs_pattern, but the cachedRegexpincorporates bothproject_rootandapp_dirs_pattern. Ifproject_rootchanges whileapp_dirs_patternstays the same (e.g., across test runs, SDK reconfiguration, or different configurations), the cache returns a staleRegexpbuilt with the oldproject_root. This causes incorrectin_appclassification of backtrace frames, affecting error grouping and display in Sentry.