Skip to content

gpui: Improve path rendering and bounds performance#44655

Merged
cameron1024 merged 7 commits intozed-industries:mainfrom
marcocondrache:fix/bounds-tree
Dec 12, 2025
Merged

gpui: Improve path rendering and bounds performance#44655
cameron1024 merged 7 commits intozed-industries:mainfrom
marcocondrache:fix/bounds-tree

Conversation

@marcocondrache
Copy link
Contributor

On my macOS machine, the paths_bench example currently runs at about 40 fps in release mode. Profiling shows that at a 24 ms frame interval, roughly 6–9 ms are spent on the GPU (fragment work, which still needs optimization), while the remainder is CPU time. After more accurate profiling, the frame graph revealed that most of the CPU cost came from bounds tree insertion

image

The issue is that the existing tree structure performs poorly when many bounds are identical, which is exactly what happens in paths_bench.

The new implementation is a variant of an R-tree. I may experiment with an R*-tree variant later, but the current approach already addresses three major problems:

  1. Fast path for max leaf (now O(1) for overlapping bounds)
    Before: Every insertion walked the entire tree to find the maximum intersecting ordering.
    After: The tree maintains a pointer to the leaf with the global maximum ordering, letting us test it immediately.

  2. Higher branching factor
    The tree now uses four children per node instead of two, which reduces height and improves traversal efficiency.

  3. Better overall balancing
    Node selection and splitting strategies produce a more stable and evenly distributed tree structure.

These changes reduce insertion complexity:

  • Identical or fully overlapping bounds drop from O(n^2) to O(n)
  • Mostly overlapping bounds drop from O(n^2) to O(n log n)
  • Spatially distributed bounds keep similar asymptotic cost but benefit from reduced height
  • Non-overlapping bounds also benefit from reduced height

And specific operations improve significantly:

  • find_max_ordering: from O(n) to O(1) when the max leaf intersects, otherwise O(log n)
  • insert_leaf: from O(n) for identical bounds to O(log4 n)
  • Total cost for inserting n items: from O(n^2) worst-case to O(n) best-case and O(n log n) worst-case

In the paths_bench example, these changes produce a 100% performance improvement, increasing the frame rate from 40 fps to 80 fps. With a few more optimizations, I believe we can push the example to around 120 fps.

Before:

image

After:

image

Release Notes:

  • Improved path and rendering performance

Signed-off-by: Marco Mihai Condrache <52580954+marcocondrache@users.noreply.github.com>
@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Dec 11, 2025
@github-actions github-actions bot added the community champion Issues filed by our amazing community champions! 🫶 label Dec 11, 2025
Signed-off-by: Marco Mihai Condrache <52580954+marcocondrache@users.noreply.github.com>
@marcocondrache
Copy link
Contributor Author

marcocondrache commented Dec 12, 2025

I tested it with a higher branching factor (12) and it runs at 140fps

image

is this the right number?

Signed-off-by: Marco Mihai Condrache <52580954+marcocondrache@users.noreply.github.com>
Signed-off-by: Marco Mihai Condrache <52580954+marcocondrache@users.noreply.github.com>
Signed-off-by: Marco Mihai Condrache <52580954+marcocondrache@users.noreply.github.com>
Signed-off-by: Marco Mihai Condrache <52580954+marcocondrache@users.noreply.github.com>
Signed-off-by: Marco Mihai Condrache <52580954+marcocondrache@users.noreply.github.com>
@marcocondrache marcocondrache marked this pull request as ready for review December 12, 2025 20:06
Copy link
Member

@mikayla-maki mikayla-maki left a comment

Choose a reason for hiding this comment

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

Wow, this code is so clean! And thank you for the detailed explanation and data backing it up. Nothing looks amiss in Zed rendering + it passes the tests, so I think we're good to go :D

@cameron1024 cameron1024 merged commit e860252 into zed-industries:main Dec 12, 2025
23 checks passed
@marcocondrache marcocondrache deleted the fix/bounds-tree branch December 13, 2025 11:34
CherryWorm pushed a commit to CherryWorm/zed that referenced this pull request Dec 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement community champion Issues filed by our amazing community champions! 🫶

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants