react-spectrum icon indicating copy to clipboard operation
react-spectrum copied to clipboard

Tree from react-aria crashes when expanding nodes muntiple in big trees

Open jonasmayer opened this issue 1 year ago โ€ข 9 comments

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

jonasmayer avatar Aug 14 '25 07:08 jonasmayer

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 avatar Aug 20 '25 01:08 snowystinger

@snowystinger I updated to use the file explorer example, the result is the same ๐Ÿ˜ข

https://codesandbox.io/p/sandbox/confident-butterfly-f3lgyl

jonasmayer avatar Aug 20 '25 04:08 jonasmayer

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. Image

snowystinger avatar Aug 20 '25 04:08 snowystinger

https://codesandbox.io/p/sandbox/confident-butterfly-f3lgyl

Sorry! Im able to see it , could you try hard refresh?

Image

jonasmayer avatar Aug 20 '25 04:08 jonasmayer

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.

snowystinger avatar Aug 20 '25 05:08 snowystinger

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

jonasmayer avatar Aug 20 '25 07:08 jonasmayer

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 avatar Aug 20 '25 10:08 snowystinger

@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

jonasmayer avatar Aug 22 '25 07:08 jonasmayer

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.

snowystinger avatar Aug 26 '25 00:08 snowystinger