Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 677
perf(TreeView): replace O(n) TreeWalker with O(depth) sibling traversal#7544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a352417
perf(ActionList): enable React Compiler
hectahertz f80ca83
perf(TreeView): replace O(n) TreeWalker with O(depth) sibling traversal
hectahertz b3741d3
changeset
hectahertz e89089b
refactor: make getDeepestLastDescendant iterative, add collapsed subt…
hectahertz 0f106b1
revert: remove unrelated react-compiler changes
hectahertz a06b3ce
Merge branch 'main' into hectahertz/perf-treeview-sibling-traversal
primer[bot] 835820c
Merge branch 'main' into hectahertz/perf-treeview-sibling-traversal
primer[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@primer/react': patch | ||
| --- | ||
| perf(TreeView): replace O(n) TreeWalker with O(depth) sibling traversal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -142,30 +142,109 @@ export function getElementState(element: HTMLElement): 'open' | 'closed' | 'end' | ||
| } | ||
| } | ||
| /** | ||
| * Find the next or previous visible treeitem using direct DOM traversal. | ||
| * | ||
| * PERFORMANCE: This is O(tree depth) instead of O(n) because it walks | ||
| * siblings and parent/child edges directly, rather than creating a TreeWalker | ||
| * that scans from the root to find the current element on every keystroke. | ||
| * | ||
| * NOTE: This relies on TreeView.SubTree unmounting its children when collapsed | ||
| * (returning null when !isExpanded). Because collapsed subtree children are | ||
| * never in the DOM, we can safely skip them by only entering children of nodes | ||
| * with aria-expanded="true". If SubTree ever changes to keep collapsed children | ||
| * mounted (e.g. via CSS display:none), this logic would need to add filtering | ||
| * for items inside collapsed parents. | ||
| */ | ||
| export function getVisibleElement(element: HTMLElement, direction: 'next' | 'previous'): HTMLElement | undefined { | ||
| const root = element.closest('[role=tree]') | ||
| if (direction === 'next') { | ||
| return getNextVisibleElement(element) | ||
| } | ||
| return getPreviousVisibleElement(element) | ||
| } | ||
| if (!root) return | ||
| function getNextVisibleElement(element: HTMLElement): HTMLElement | undefined { | ||
| // If the current item is expanded, the next visible item is its first child | ||
| if (element.getAttribute('aria-expanded') === 'true') { | ||
| const firstChild = getFirstChildElement(element) | ||
| if (firstChild) return firstChild | ||
| } | ||
| const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT, node => { | ||
| if (!(node instanceof HTMLElement)) return NodeFilter.FILTER_SKIP | ||
| return node.getAttribute('role') === 'treeitem' ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP | ||
| }) | ||
| // Otherwise, walk up the tree looking for a next sibling | ||
| let current: HTMLElement | undefined = element | ||
| while (current) { | ||
| const next = getNextSiblingTreeItem(current) | ||
| if (next) return next | ||
| // No next sibling at this level, try the parent's next sibling | ||
| current = getParentElement(current) | ||
| } | ||
| return undefined | ||
| } | ||
| let current = walker.firstChild() | ||
| function getPreviousVisibleElement(element: HTMLElement): HTMLElement | undefined { | ||
| const prev = getPreviousSiblingTreeItem(element) | ||
| while (current !== element) { | ||
| current = walker.nextNode() | ||
| if (prev) { | ||
| // Navigate to the deepest last visible descendant of the previous sibling | ||
| return getDeepestLastDescendant(prev) | ||
| } | ||
| let next = direction === 'next' ? walker.nextNode() : walker.previousNode() | ||
| // No previous sibling, the parent is the previous visible element | ||
| return getParentElement(element) | ||
| } | ||
| /** | ||
| * Walk into expanded subtrees to find the deepest last visible descendant. | ||
| * For example, if the last sibling is an expanded directory whose last child | ||
| * is also an expanded directory, we drill all the way down. | ||
| */ | ||
| function getDeepestLastDescendant(element: HTMLElement): HTMLElement { | ||
hectahertz marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| let current = element | ||
| while (current.getAttribute('aria-expanded') === 'true') { | ||
| const lastChild = getLastChildTreeItem(current) | ||
| if (!lastChild) break | ||
| current = lastChild | ||
| } | ||
| return current | ||
| } | ||
| // If next element is nested inside a collapsed subtree, continue iterating | ||
| while (next instanceof HTMLElement && next.parentElement?.closest('[role=treeitem][aria-expanded=false]')) { | ||
| next = direction === 'next' ? walker.nextNode() : walker.previousNode() | ||
| function getNextSiblingTreeItem(element: HTMLElement): HTMLElement | undefined { | ||
| let sibling = element.nextElementSibling | ||
| while (sibling) { | ||
| if (sibling instanceof HTMLElement && sibling.getAttribute('role') === 'treeitem') { | ||
| return sibling | ||
| } | ||
| sibling = sibling.nextElementSibling | ||
| } | ||
| return undefined | ||
| } | ||
| function getPreviousSiblingTreeItem(element: HTMLElement): HTMLElement | undefined { | ||
| let sibling = element.previousElementSibling | ||
| while (sibling) { | ||
| if (sibling instanceof HTMLElement && sibling.getAttribute('role') === 'treeitem') { | ||
| return sibling | ||
| } | ||
| sibling = sibling.previousElementSibling | ||
| } | ||
| return undefined | ||
| } | ||
| return next instanceof HTMLElement ? next : undefined | ||
| function getLastChildTreeItem(element: HTMLElement): HTMLElement | undefined { | ||
| // Find the [role=group] child (the subtree container), then get its last treeitem | ||
| for (let i = element.children.length - 1; i >= 0; i--) { | ||
| const child = element.children[i] | ||
| if (child instanceof HTMLElement && child.getAttribute('role') === 'group') { | ||
| let lastChild = child.lastElementChild | ||
| while (lastChild && !(lastChild instanceof HTMLElement && lastChild.getAttribute('role') === 'treeitem')) { | ||
| lastChild = lastChild.previousElementSibling | ||
| } | ||
| return lastChild instanceof HTMLElement ? lastChild : undefined | ||
| } | ||
| } | ||
| return undefined | ||
| } | ||
| export function getFirstChildElement(element: HTMLElement): HTMLElement | undefined { | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.