Tree from react-aria crashes when expanding nodes muntiple in big trees
Provide a general summary of the issue here
When the user tried to multiple expand nodes in big trees it crashes the browser, even when using virtualization
Codebox: https://codesandbox.io/p/sandbox/confident-butterfly-f3lgyl
๐ค Expected Behavior?
Libraries like https://www.npmjs.com/package/react-sortable-tree support this type of operation.
๐ฏ Current Behavior
Page crashed when user clicks expand all, expanding a large number of nodes at once seems to be the main cause
๐ Possible Solution
It could be associated with virtualization or the state management of react-aria https://github.com/adobe/react-spectrum/blob/main/packages/react-aria-components/src/Tree.tsx, I can try to run the project locally but would be easier if I had a few pointers on where to start looking
๐ฆ Context
No response
๐ฅ๏ธ Steps to Reproduce
Codebox: https://codesandbox.io/p/sandbox/confident-butterfly-f3lgyl
Have a large tree, try to expand a large number of nodes at once
Version
1.11
What browsers are you seeing the problem on?
Firefox
If other, please specify.
No response
What operating system are you using?
linux
๐งข Your Company/Team
No response
๐ท Tracking Issue
No response
Does it work better if you use the items prop on the Tree instead? Or is there a reason you're not using that?
https://react-spectrum.adobe.com/react-aria/Tree.html#examples
@snowystinger I updated to use the file explorer example, the result is the same ๐ข
https://codesandbox.io/p/sandbox/confident-butterfly-f3lgyl
Maybe you forgot to save? The sandbox passes items, but it's not using the items. Please see the example specifically from https://react-spectrum.adobe.com/react-aria/Tree.html#content
Specifically the blue highlighted lines.
https://codesandbox.io/p/sandbox/confident-butterfly-f3lgyl
Sorry! Im able to see it , could you try hard refresh?
Hard refresh did the trick. Sorry about that.
It looks like you're recreating the entire data structure every time you expand any key. Can you track the expanded keys in a separate data structure so you don't recreate every node in the whole tree for every change?
Something like this https://codesandbox.io/p/sandbox/bbg-tree-forked-vl4qxv There are probably some more optimisations applicable, if you search our discussions and issues you can probably find some more.
How likely is it that you actually need to expand 100,000 items? You could use our async list to mange the items so they aren't all there at once https://react-spectrum.adobe.com/react-aria/Tree.html#asynchronous-loading There are some issues/discussions with more info on this. Those deal with actual API's. You however would essentially work with a fake API that can instantly return results. If you know all the ids ahead of time (which you do since it's not a real API and it's all local), you could expand in pieces as scrolling happens.
We can see if anyone else on the team has more ideas.
It looks like you're recreating the entire data structure every time you expand any key. Can you track the expanded keys in a separate data structure so you don't recreate every node in the whole tree for every change?
It don't seem to affect the end result
Something like this https://codesandbox.io/p/sandbox/bbg-tree-forked-vl4qxv Unfortunately it still crashes
How likely is it that you actually need to expand 100,000 items? I extrapolated for tests, but I see it happening when expanding 2.000 nodes
My suspicion is that it could be a problem with the way the component manage expandedKeys state or an issue with virtualization
I ran the chrome profiler. Much of the time is spent in the collection builder, which would indicate to me that we aren't caching enough. This can happen when new objects are created and looped over. That's why I suggested using the dynamic collection approach, we have some caching already built in there.
Will keep thinking about what else might contribute.
I also moved the inner part of the tree item out into its own component to reduce rendering in the collection builder.
@snowystinger I gave a deeper look at the Tree component, there the issue with useMemo rebuilding the tree as you mentioned, but I also noticed an easier to fix issue that would reduce the performance impact
On the line https://github.com/adobe/react-spectrum/blob/341ae3f8ab9993420b4bc9c204d9e9428aabac8b/packages/react-aria-components/src/Tree.tsx#L847
It loops on every node of the tree to find if the parent belongs to the tree, we could use a Map to avoid this extra o(n)
let itemCount = 0;
let parentLookup: Map<Key, boolean> = new Map();
/// ...
if (modifiedNode.level === 0 || (modifiedNode.parentKey != null && expandedKeys.has(modifiedNode.parentKey) && parentLookup.get(modifiedNode.parentKey))) {
if (modifiedNode.type === 'item') {
itemCount++;
}
flattenedRows.push(modifiedNode);
parentLookup.set(modifiedNode.key, true);
I recorder a video with a test story with 100.000 items, it improved the performance:
https://github.com/user-attachments/assets/41d35bed-483e-4876-9ffa-717b22005de0
Thanks, that does look like a reasonable optimisation. I've opened a PR with the change, I'll try to get some metrics to back it soon.