Skip to content

Conversation

@ThierryMT
Copy link

@ThierryMT ThierryMT commented Oct 27, 2025

Fixes #140677

This PR adds a --heatmap output format to the sampling profiler that generates an HTML visualization showing line-by-line sample intensity with color coding.

Problem:
Flamegraphs show which functions are hot and how they're called, but they don't show which lines within a function are the bottlenecks.

Solution:
The heatmap shows the actual source code with lines colored from blue (few samples) to red (many samples), making it easy to identify hot loops, expensive operations, and unexpected bottlenecks within functions.

Features:

  • Color-coded source code (blue to red gradient)
  • Line numbers and sample counts displayed
  • Collapsible file sections for easy navigation
  • Dark theme matching modern code editors
  • Standalone HTML output (no external dependencies)
  • Log-scale color normalization for better visualization

Usage:

python -m profiling.sampling --heatmap script.py
python -m profiling.sampling --heatmap -o results.html -p 1234

Files Changed:

  • Lib/profiling/sampling/heatmap_collector.py (new)
  • Lib/profiling/sampling/__init__.py
  • Lib/profiling/sampling/sample.py

Add --heatmap option to the sampling profiler that generates an HTML
visualization showing line-by-line sample intensity with color coding.

The heatmap shows the actual source code with lines colored from blue
(few samples) to red (many samples), making it easy to identify hot
loops, expensive operations, and unexpected bottlenecks within functions.

This complements flamegraphs by providing line-level granularity that
flamegraphs don't show. While flamegraphs show which functions are hot
and their call relationships, heatmaps show which specific lines within
those functions are the bottlenecks.

Features:
- Color-coded source code (blue to red gradient)
- Line numbers and sample counts
- Collapsible file sections
- Dark theme matching modern editors
- Standalone HTML output
- Log-scale color normalization

Usage:
  python -m profiling.sampling --heatmap script.py
  python -m profiling.sampling --heatmap -o results.html -p 1234

Fixes pythongh-140677
@bedevere-app
Copy link

bedevere-app bot commented Oct 27, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@pablogsal pablogsal closed this Oct 27, 2025
@pablogsal
Copy link
Member

pablogsal commented Oct 27, 2025

Sorry @ThierryMT but this PR has several problems:

  • It's done with generative AI without checking anything.
  • You didn't ask in the issue and gather what the feature required
  • You should ask first in the issue to know you are not racing (as I had a different PR already prepared)

@pablogsal
Copy link
Member

@ThierryMT ThierryMT deleted the add-heatmap-profiler-output branch October 27, 2025 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a new heatmap output to the tachyon profiler

2 participants