perf(TreeView): add memoization + test demo#6362
Conversation
|
An unrelated but possible alternative for large tree view datasets would be to mock a tree-like view using a simple list structure instead of using recursion - that would also separate the state out from the stack. This would require a way to provide indentation to a nested |
|
PF4 preview: https://patternfly-react-pr-6362.surge.sh |
|
Link to demo page: https://patternfly-react-pr-6362.surge.sh/components/tree-view |
|
I can definitely notice a difference. I'm not sure how much more noticeable it'll be with more complicated treeListItems. And I don't know if the improvement is as good as the customers will want. As far as memoization goes, you're saying that you need to evaluate if activeItems has changed to know if the children should rerender, right? And mayyyyybe we could further improve this by finding a less expensive way to make that determination? |
|
Yeah that's why I think it may be worth also investigating if we can make a faux-tree view with a simple list over the recursion. For the memoization yes. |
|
Note - test few folders, shallow tree with lots of child nodes per folder (something like 3 folders, 10k+ nodes per folder). Large shallow trees may be a good starting point for trying out a faux-tree "list" component as well. |
redallen
left a comment
There was a problem hiding this comment.
I read through the React.memo docs and think the comparator function could be better
| return false; | ||
| } | ||
|
|
||
| return !(prevIncludes || nextIncludes); |
There was a problem hiding this comment.
Don't you also want to also shallowly compare the rest of prepProps and nextProps?
There was a problem hiding this comment.
Updated and tested. compareItems and children always change on any state update so I'm excluding those at the moment - because children is another TreeView and should use its own memoization and compareItems is set by default and registering as a new function every comparison despite being the same code. Not sure if there's a better way of comparing compareItems.
99d29c3 to
28243d6
Compare
What: Possible improvement for #6268
In this PR:
useMemoand the newTree viewdemo page under theDemossection are for demonstrating this memoization only and should not be merged. I will remove them if we want to proceed with the memo changes.TreeViewwill completely re-render all nodes when theonSelectis called and a state update is made toactiveItems, because a state update is being made at the top level of the recursive tree view. To help performance, I've added memoization to the TreeViewListItem to alleviate some of the unnecessary re-rendering - items should only re-render when their active state is going to change.The main caveat in adding this memoization logic is due to TreeView's recursive nature, parent nodes that do not see an update to their rendering will not re-render (or even evaluate) child nodes below them, and re-rendering when the children prop updates defeats the purpose as the whole tree would re-render. The current working solution has
activeItemschange slightly - instead of passing only the selected item, all nodes on the path from the top node to the selected node must be passed to this property. This doesn't changeactiveItemseffective behavior (only the leaf node's css will change as usual) but it would be a breaking change to the current demos (unless theuseMemoprop is kept and has a default value offalse, which would disable memoization unless it is required).Any other thoughts and ideas regarding tree view performance is welcome!