Skip to content

perf: detach walks only the detached subtree of the identifier cache - #2306

Open
lavrton wants to merge 1 commit into
mobxjs:masterfrom
lavrton:fix/split-cache-subtree
Open

lavrton wants to merge 1 commit into
mobxjs:masterfrom
lavrton:fix/split-cache-subtree

Conversation

@lavrton

@lavrton lavrton commented Oct 15, 2026

Copy link
Copy Markdown

Hey, we hit performance issues is Polotno studio (design editor). Grouping elements detaches each one and moves it into the new group. With 600 elements on a page, long time was splitCache, and it grows with every page in the document.

detach() calls IdentifierCache.splitCache, which loops over every identifier in the tree and builds each node's path to find the detached subtree. So one detach costs O(tree size), and moving N nodes costs O(N × tree size).

The fix walks only the detached subtree and reuses notifyDied to remove each node from the old cache.

detach 600 nodes (bun, production, median of 7) before after
tree of 600 nodes 35 ms 5.4 ms
tree of 3600 nodes 298 ms 6.0 ms

The new test in identifier.test.ts detaches 1 of 1000 items. Before the fix that read path 1001 times. It also checks that identifiers still resolve, including one nested in the detached item.

@coolsoftwaretyler

Copy link
Copy Markdown
Collaborator

Thanks, I'll take a look soon!

@lavrton

lavrton commented Oct 15, 2026

Copy link
Copy Markdown
Author

@coolsoftwaretyler thanks a lot! Maybe will send couple more PR to mst or mobx. Still investigating

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.

2 participants