Perf: Index findViewById to avoid O(n) tree walk per lookup#2
Draft
collinversluis wants to merge 1 commit into
Draft
Perf: Index findViewById to avoid O(n) tree walk per lookup#2collinversluis wants to merge 1 commit into
collinversluis wants to merge 1 commit into
Conversation
Phase 7 of the perf series. CPU profile on a 200-element page showed findViewRecursive at 509ms self-time, called ~420 times during boot via findViewById (316) and findContainerById (104). Each call was an O(n) walk of the full Marionette view tree. This builds a Map<id, view> index lazily on first findViewById call within a frame, clears it via requestAnimationFrame, and validates _isDestroyed before returning a cached view. Cache misses fall through to a rebuild-and-retry so mid-frame view creation/destruction is handled safely. For the boot scenario (~420 lookups across ~200 elements in a tight burst), this turns 420 * 200 = 84,000 ops into ~200 (build) + 420 (lookups) ≈ 620 ops. Gated by e_memoize_active_controls.
5 tasks
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.
Summary
findViewByIdwalks the entire element tree on every call. On element-heavy pages this is O(n²) when many lookups happen during boot (each element's parent lookup triggers a walk).Add an id→view index, populated on view add and invalidated on view remove. Lookups become O(1).
Impact
Profile attribution: this lookup accumulated ~60-100ms self-time on a 200-widget bench. Should scale further on pages with deeper trees.
Test plan
Notes
Cherry-pick from
claude/great-mayer-klkFh. No experiment flag — invalidation is unconditional. If the team prefers a flag for the first release, happy to gate it.Generated by Claude Code