-
-
Notifications
You must be signed in to change notification settings - Fork 532
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 |
|---|---|---|
|
|
@@ -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 = {} | ||
|
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) | ||
|
|
||
| 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] ||= {}) | ||
|
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 variable Suggested FixReplace the undefined variable Prompt for AI AgentThere 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. Undefined method
|
||
| pattern_cache[in_app_pattern] = line | ||
|
|
||
| line | ||
|
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. Line object cache is written but never readMedium Severity The |
||
| 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.
Bug: The new caching mechanisms in
Backtrace::Lineuse non-thread-safeHashobjects, creating race conditions in multi-threaded environments during cache read, write, and clear operations.Severity: MEDIUM
Suggested Fix
Replace the plain
Hashobjects used for@parse_cacheand@line_object_cachewith a thread-safe alternative. Since the project already depends onconcurrent-ruby, usingConcurrent::Mapis the recommended approach, as it is already used for other caches in the codebase.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.