Skip to content

Compute per-frame flame graph data once per report - #1103

Open
patrick-immometrica wants to merge 2 commits into
bloomberg:mainfrom
patrick-immometrica:perf-flamegraph-frame-cache
Open

patrick-immometrica wants to merge 2 commits into
bloomberg:mainfrom
patrick-immometrica:perf-flamegraph-frame-cache

Conversation

@patrick-immometrica

Copy link
Copy Markdown

As discussed in #1100: create_framegraph_node_from_stack_frame did the linecache lookup, two html.escape calls 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_snapshot creates a dict for the report and passes it through generate_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_frame behaves 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 flamegraph on 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

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-commenter

codecov-commenter commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 92.40%. Comparing base (4e4dd93) to head (253146d).

Files with missing lines Patch % Lines
src/memray/reporters/flamegraph.py 91.66% 1 Missing ⚠️
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     
Flag Coverage Δ
cpp 92.40% <91.66%> (-0.01%) ⬇️
python_and_cython 92.40% <91.66%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@patrick-immometrica
patrick-immometrica marked this pull request as ready for review November 25, 2026 01:03

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flame graph generation redoes per-frame work for every node

2 participants