Improve focus navigation performance by caching Screen.focus_chain between layout changes#6608
Open
RajanChavada wants to merge 1 commit into
Open
Improve focus navigation performance by caching Screen.focus_chain between layout changes#6608RajanChavada wants to merge 1 commit into
RajanChavada wants to merge 1 commit into
Conversation
…nges Recalculating the focus chain on every access traverses the entire DOM sorted by focus order, which is moderately expensive. Cache the result and clear it in the three places that make it stale: _on_layout (mount/unmount/ visibility/disabled changes), set_focus (root node depends on focused widget when _trap_focus is active), and trap_focus() (directly changes which node acts as root).
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Link to issue or discussion
Addresses the acknowledged TODO at
screen.py:774:What this PR does
Screen.focus_chaintraverses the entire DOM on every call — sorting children by focus order, walking visibility, and checking disabled state. It's called on every Tab/Shift+Tab keypress and in several internal focus-management paths.This PR caches the result in
_focus_chain_cacheand clears it in the three places that make it stale:Screen._on_layoutdisplay: none), and disabled state changes — all of which post aLayoutmessageScreen.set_focusself.focusedwhen_trap_focusis activeDOMNode.trap_focus_trap_focus, changing which node acts as the traversal root — no layout message is posted_add_children(test-only helper that bypasses the mount pipeline) also clears the cache so existing tests stay green.Testing
test_focus.py,test_app_focus_blur.py,test_screens.py,test_screen_modes.py)test_features.pyunrelated to this change