Skip to content

Conversation

@mcollina
Copy link
Member

@mcollina mcollina commented Dec 2, 2025

Summary

This PR fixes a significant performance issue in FrameRenderer.ts where the frame color calculation had O(n²) time complexity. The optimization reduces this to O(n) by pre-computing depth totals once instead of filtering all frames for each frame during rendering.

Changes

Performance Optimization in src/renderer/FrameRenderer.ts:

  1. Pre-computation of depth totals (before the rendering loop):

    • Added totalValueByDepth Map that computes total values at each depth level in a single O(n) pass
    • This eliminates the need to filter through all frames repeatedly
  2. Updated #calculateFrameColor method:

    • Changed signature to accept pre-computed totalValueByDepth map instead of the full allFrames array
    • Replaced O(n) filter operation with O(1) map lookup

Performance Impact

  • Before: O(n²) complexity - for each frame, filtered through all frames to find those at the same depth
  • After: O(n) complexity - single pass to build the map, then O(1) lookups during rendering
  • Result: Significantly faster rendering for large flame graphs with many frames

Test Plan

  • Existing functionality remains unchanged (only internal implementation optimized)
  • Visual output is identical to previous behavior
  • Performance improvement is most noticeable with large profile datasets

Fixes #30
Fixes #44

Optimized frame color calculation by pre-computing depth totals once
instead of filtering all frames for each frame during rendering.

Changes:
- Added totalValueByDepth map that pre-computes total values at each
  depth level in a single O(n) pass before the rendering loop
- Updated #calculateFrameColor to accept the pre-computed map instead
  of the full frames array
- Changed depth total lookup from O(n) filter operation to O(1) map
  lookup

This reduces the overall complexity from O(n²) to O(n), significantly
improving rendering performance for large flame graphs.

Fixes #30
@mcollina mcollina requested a review from Qard December 2, 2025 12:00
@mcollina mcollina merged commit 2d0cdfc into main Dec 2, 2025
41 checks passed
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.

Sluggish performance Feature: frame renderer performance

3 participants