perf: optimize LineCache to reduce allocations#2903
Merged
sl0thentr0py merged 1 commit intomasterfrom Mar 19, 2026
Merged
Conversation
HazAT
added a commit
that referenced
this pull request
Mar 18, 2026
Reduce total allocated memory from 442k to 206k bytes (-53.5%) and
objects from 3305 to 1538 (-53.5%) per Rails exception capture.
All changes are internal optimizations with zero behavior changes.
Key optimizations:
- Cache longest_load_path and compute_filename results (class-level,
invalidated on $LOAD_PATH changes)
- Cache backtrace line parsing and Line/Frame object creation (bounded
at 2048 entries)
- Optimize LineCache with Hash#fetch, direct context setting, and
per-(filename, lineno) caching
- Avoid unnecessary allocations: indexed regex captures, match? instead
of =~, byteslice, single-pass iteration in StacktraceBuilder
- RequestInterface: avoid env.dup, cache header name transforms, ASCII
fast-path for encoding
- Scope/BreadcrumbBuffer: shallow dup instead of deep_dup where inner
values are not mutated after duplication
- Hub#add_breadcrumb: hint default nil instead of {} to avoid empty
hash allocation
See sub-PRs for detailed review by risk level:
- #2902 (low risk) — hot path allocation avoidance
- #2903 (low risk) — LineCache optimization
- #2904 (medium risk) — load path and filename caching
- #2905 (needs review) — backtrace parse caching
- #2906 (needs review) — Frame object caching
- #2907 (needs review) — Scope/BreadcrumbBuffer shallow dup
- #2908 (medium risk) — RequestInterface optimizations
7ce022e to
542ee21
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
There are 2 total unresolved issues (including 1 from previous review).
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Member
|
|
Refactor LineCache internals to reduce memory allocations when reading source context for stacktrace frames: - Use Hash#fetch instead of ||= to avoid double hash lookup in getlines - Remove valid_path?/getline indirection — inline bounds checking via line_at helper - Build pre/post context arrays directly with Array.new instead of creating a single large array and slicing it - Add set_frame_context method that sets frame attributes directly, avoiding the intermediate [pre, context_line, post] array allocation - Cache context results per (filename, lineno) since the same frames repeat across exceptions — avoids recreating identical arrays The public get_file_context API is preserved for custom LineCache implementations.
542ee21 to
5eb86fb
Compare
sl0thentr0py
approved these changes
Mar 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

✅ Low risk — internal refactor of LineCache
Part of #2901 (reduce memory allocations by ~53%)
Changes
Hash#fetchinstead of||=to avoid double hash lookup ingetlinesvalid_path?/getlineindirection — inline bounds checking vialine_athelperArray.newinstead of creating a single large array and slicing itset_frame_contextmethod that sets frame attributes directly, avoiding the intermediate[pre, context_line, post]array allocation(filename, lineno)since the same frames repeat across exceptions — avoids recreating identical arraysThe public
get_file_contextAPI is preserved for custom LineCache implementations.