Repository navigation
Compute per-frame flame graph data once per report - #1103
Open
patrick-immometrica wants to merge 2 commits into
Open
patrick-immometrica wants to merge 2 commits into
patrick-immometrica wants to merge 2 commits into
Conversation
create_framegraph_node_from_stack_frame looked up the source line, escaped the function and filename and checked whether the frame is interesting for every new node, although all of that only depends on the frame itself. The same frame appears in many nodes, so cache it in a dict that lives for one report. On a capture with 2.86M flame graph nodes this takes `memray flamegraph` from 11.1 s to 9.4 s, and the HTML is byte-for-byte the same. Signed-off-by: Patrick Hintermayer <patrick@immometrica.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1103 +/- ##
==========================================
- Coverage 92.40% 92.40% -0.01%
==========================================
Files 103 103
Lines 13453 13463 +10
Branches 480 482 +2
==========================================
+ Hits 12431 12440 +9
- Misses 1022 1023 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
patrick-immometrica
marked this pull request as ready for review
November 25, 2026 01:03
This branch has not been deployed
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.
As discussed in #1100:
create_framegraph_node_from_stack_framedid the linecache lookup, twohtml.escapecalls and the interesting/import-system checks for every new node, even though the result only depends on the(function, filename, lineno)frame.That work now happens once per distinct frame.
_from_any_snapshotcreates a dict for the report and passes it throughgenerate_frames, so nothing outlives a single report and the IPython magic still picks up edited source files. The inverted tree and the inverted tree without import-system frames share the same dict. Called without a cache,create_framegraph_node_from_stack_framebehaves like before. Each node still gets its own dict, since<STACK TOO DEEP>overwrites a node's name and location.For a benchmark I used a script that recurses through 4 functions 8 levels deep and then 40 more frames before allocating, recorded with
--trace-python-allocators. That gives 65k distinct stacks and 2.86M flame graph nodes. With Python 3.13 in the repo's Docker image,memray flamegraphon it goes from 11.0–11.2 s to 9.4–9.5 s over three runs each, and the HTML is byte-for-byte identical. The speedup is smaller than the one in the issue because it was measured on a different capture.Closes: #1100