Skip to content

Improve flame graph rendering performance#4455

Merged
julienw merged 4 commits into
firefox-devtools:mainfrom
krsh732:pan-flamegraph-perf
Feb 10, 2023
Merged

Improve flame graph rendering performance#4455
julienw merged 4 commits into
firefox-devtools:mainfrom
krsh732:pan-flamegraph-perf

Conversation

@krsh732

@krsh732 krsh732 commented Feb 1, 2023

Copy link
Copy Markdown
Contributor

I'm not sure if I'm missing something more obvious, but these are the things being done in this PR:

  • Reduce fillStyle and fillRect calls:
    • By using FastFillStyle.
    • By drawing the fill rectangle at an offset instead of drawing another white border.
  • Removing the need to memoize style colors by storing them in a map ahead of time.
  • Reduce render calls by pulling dragX/dragY out of chart viewport state.
  • Replace getFittedText with an equivalent implementation that does less work.

Fixes #4451

Production | Profile of panning the flame graph in production
Deploy preview | Profile of panning the flame graph with PR changes


Some other things I tried:

  1. Splitting the flame graph rendering into two passes (pass 1 draws rectangles and pushes visible texts to a list, pass 2 draws visible texts). It seemed to have made it ever so slightly more smoother, but numbers only showed average _drawCanvas time reducing from 4 ms to 3.8 ms.
  2. Re-enabling requestAnimationFrame for FlameGraph canvas. This seems to make it slightly smoother, but not sure how to confirm. Profile
  3. Disabling highlighting when panning. This also seems to make it slightly smoother but can't say for certain. Profile

@codecov

codecov Bot commented Feb 1, 2023

Copy link
Copy Markdown

Codecov Report

Base: 88.59% // Head: 88.64% // Increases project coverage by +0.04% 🎉

Coverage data is based on head (e558603) compared to base (b69a16c).
Patch coverage: 94.28% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4455      +/-   ##
==========================================
+ Coverage   88.59%   88.64%   +0.04%     
==========================================
  Files         284      284              
  Lines       25559    25528      -31     
  Branches     6877     6860      -17     
==========================================
- Hits        22644    22629      -15     
+ Misses       2708     2692      -16     
  Partials      207      207              
Impacted Files Coverage Δ
src/utils/colors.js 95.45% <75.00%> (+13.93%) ⬆️
src/components/flame-graph/Canvas.js 91.93% <100.00%> (-0.13%) ⬇️
src/components/shared/chart/Viewport.js 96.08% <100.00%> (+0.05%) ⬆️
src/components/stack-chart/Canvas.js 94.41% <100.00%> (-0.04%) ⬇️
src/utils/text-measurement.js 100.00% <100.00%> (+6.66%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@krsh732 krsh732 marked this pull request as ready for review February 2, 2023 02:06
@krsh732 krsh732 marked this pull request as draft February 2, 2023 06:47
@krsh732 krsh732 force-pushed the pan-flamegraph-perf branch 2 times, most recently from cd5fc51 to eb78e56 Compare February 3, 2023 06:05
@krsh732 krsh732 marked this pull request as ready for review February 3, 2023 07:16

@julienw julienw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty good to me, thanks a ton!

I only have a small comment about the result of the fitted text algorithm, tell me what you think.

In the future I'd like to use the real measureText, but with bisecting instead of iterating, which hopefully should be faster... but we'll have to profile to be sure :).

Comment thread src/utils/text-measurement.js Outdated
@krsh732 krsh732 force-pushed the pan-flamegraph-perf branch from eb78e56 to e558603 Compare February 9, 2023 23:46
@krsh732

krsh732 commented Feb 9, 2023

Copy link
Copy Markdown
Contributor Author

In the future I'd like to use the real measureText, but with bisecting instead of iterating, which hopefully should be faster... but we'll have to profile to be sure :).

I was actually looking into improving this further locally. I tried bisection using ctx.measureText directly and the performance hit was real and noticeable for the profile in the linked issue. However, bisection does become more feasible if we use getTextWidth instead (ie. cache every intermediate step). Though the cache might very well be useless the moment maxWidths change (eg. window resize/node transforms?).

@julienw

julienw commented Feb 10, 2023

Copy link
Copy Markdown
Contributor

In the future I'd like to use the real measureText, but with bisecting instead of iterating, which hopefully should be faster... but we'll have to profile to be sure :).

I was actually looking into improving this further locally. I tried bisection using ctx.measureText directly and the performance hit was real and noticeable for the profile in the linked issue. However, bisection does become more feasible if we use getTextWidth instead (ie. cache every intermediate step). Though the cache might very well be useless the moment maxWidths change (eg. window resize/node transforms?).

Thanks for looking at it at least, we can discuss other solutions later. Maybe we can still find approximations that would work with the current not-very-good cases.

@julienw julienw merged commit c05d0a3 into firefox-devtools:main Feb 10, 2023
@krsh732 krsh732 deleted the pan-flamegraph-perf branch February 11, 2023 03:09
@canova canova mentioned this pull request Feb 27, 2023
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.

Panning the flame graph is sluggish in this profile

2 participants