Skip to content

Optimize RenderSliverVariedExtentList and introduce protected itemExtentOf API in RenderSliverFixedExtentBoxAdaptor - #192384

Merged
auto-submit[bot] merged 43 commits into
flutter:masterfrom
Mairramer:improve-item-extent-builder-performance
Nov 13, 2026
Merged

auto-submit[bot] merged 43 commits into
flutter:masterfrom
Mairramer:improve-item-extent-builder-performance

Conversation

@Mairramer

@Mairramer Mairramer commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

This PR optimizes RenderSliverVariedExtentList by memoizing item extents and prefix-sum offsets per layout pass (cleared on each performLayout()), reducing itemExtentBuilder evaluations per layout from O(N * K) to O(N) (at most once per index) with O(1) cached offset lookups and O(log N) binary search for child index lookups.

It also adds a protected itemExtentOf method to RenderSliverFixedExtentBoxAdaptor (overridden by RenderSliverVariedExtentList to memoize extents and short-circuit terminal null results via _endIndex). Because RenderTreeSliver extends RenderSliverVariedExtentList and queries extents via itemExtentOf, it automatically inherits this per-layout memoization.

Fixes #144390

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

@github-actions github-actions Bot added framework flutter/packages/flutter repository. See also f: labels. f: scrolling Viewports, list views, slivers, etc. labels Sep 7, 2026
@Mairramer

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces an item offset cache (_itemOffsetCache) in RenderSliverFixedExtentBoxAdaptor to prevent redundant calls to itemExtentBuilder and optimizes scroll offset index lookups using binary search. Feedback suggests caching the maximum child count to avoid repeated builder calls during overscroll, refactoring layout-triggered cache resets to use clearItemExtentCache(), and removing an unused callback parameter from _getChildIndexForScrollOffset.

…BoxAdaptor and restore cached offset computations
@Mairramer Mairramer added the CICD Run CI/CD label Sep 7, 2026
@Mairramer

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces caching for item extents and offsets in RenderSliverVariedExtentList and adds tests to verify builder call frequency. Feedback on these changes suggests avoiding force-unwrapping nullable fields such as _currentLayoutDimensions and the result of _getOrCreateItemExtent, hoisting childManager.estimatedChildCount out of a loop to prevent redundant accesses, and adjusting the binary search condition in _getChildIndexForScrollOffset to avoid laying out an extra off-screen child when the scroll offset aligns with an item boundary.

@Mairramer Mairramer changed the title [WIP] Improve item extent builder performance Optimize RenderSliverVariedExtentList layout performance Sep 18, 2026
@Mairramer
Mairramer marked this pull request as ready for review September 18, 2026 12:18

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces caching of item extents and offsets in RenderSliverVariedExtentList to optimize layout performance, along with cache invalidation mechanisms and corresponding unit tests. The review feedback suggests optimizing the binary search condition in _getChildIndexForScrollOffset by changing >= to > to prevent laying out an extra off-screen child when the scroll offset aligns exactly with an item boundary.

@Mairramer
Mairramer requested a review from victorsanni October 31, 2026 15:03

@override
double? itemExtentOf(int index) {
if (index < _itemExtentCache.length) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling itemExtentOf(-1) throws RangeError on RenderSliverVariedExtentList, returns itemExtent on RenderSliverFixedExtentList, and invokes itemExtentBuilder!(-1, layoutDimensions) on base RenderSliverFixedExtentBoxAdaptor.

}

double? _getOrCreateItemOffset(int index) {
if (index < _itemOffsetCache.length) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

index < length is true for every negative index (even when _itemExtentCache is empty, since -1 < 0). Calling renderSliver.indexToLayoutOffset(0.0, -1) on RenderSliverVariedExtentList returned 0.0 on master whereas on the PR head it throws RangeError (length): Invalid value: Not in inclusive range 0..x: -1

@Mairramer
Mairramer requested a review from victorsanni November 3, 2026 22:49

@victorsanni victorsanni left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please update the PR description to reflect the per-layout memoization complexity and RenderTreeSliver inheritance.

@Mairramer

Copy link
Copy Markdown
Contributor Author

Wow, this turned out to be a lot more work than I initially expected haha.
@victorsanni, thanks for being so patient with this PR. I promise I'll get it merged before GTA 6 comes out!

@Mairramer
Mairramer requested a review from victorsanni November 4, 2026 11:57
victorsanni
victorsanni previously approved these changes Nov 5, 2026

@victorsanni victorsanni left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with final comments

@victorsanni victorsanni left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the optimization here!

@Mairramer Mairramer added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 13, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Nov 13, 2026
Merged via the queue into flutter:master with commit 3c78d83 Nov 13, 2026
21 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 13, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Nov 16, 2026
flutter/flutter@1b95854...d8bd864

2026-11-16 [email protected] Roll Skia from f2790b0ea630 to 118eaff3deb4 (8 revisions) (flutter/flutter#196661)
2026-11-16 [email protected] [native_assets] Keep a dylib's install name in flutter test when its path does not fit (flutter/flutter#194802)
2026-11-16 [email protected] Reland "[android]: prewarms shaders when the context is created" (flutter/flutter#196579)
2026-11-16 [email protected] [flutter_tools] Bound Browser.close in Chromium.close (flutter/flutter#196211)
2026-11-16 [email protected] Roll Skia from e8ff7a4187ec to f2790b0ea630 (13 revisions) (flutter/flutter#196652)
2026-11-14 [email protected] Revert: Balance web_canvaskit_tests and web_skwasm_tests subshards by duration (flutter/flutter#196580)
2026-11-14 [email protected] Revert: Reland "[android]: prewarms shaders when the context is created" (flutter/flutter#196575)
2026-11-14 [email protected] [flutter_tools] Guard ExtensionManager initialization against concurrent disposal (flutter/flutter#196459)
2026-11-14 [email protected] [ImpellerC] Escape spaces in depfile paths (flutter/flutter#196555)
2026-11-14 [email protected] Reland "[android]: prewarms shaders when the context is created" (flutter/flutter#196549)
2026-11-14 [email protected] Balance web_canvaskit_tests and web_skwasm_tests subshards by duration (flutter/flutter#196260)
2026-11-13 [email protected] Safely return null on malformed stack trace line in StackFrame.fromStackTraceLine (flutter/flutter#194673)
2026-11-13 [email protected] Clarify that super.dispose() must be called synchronously (flutter/flutter#193416)
2026-11-13 [email protected] Consolidate exit-code parameters on runCommand with ExpectedExit (flutter/flutter#196479)
2026-11-13 [email protected] Support multi-view apps in widget test binding (flutter/flutter#194480)
2026-11-13 [email protected] Document using a ListView inside a Column (flutter/flutter#195145)
2026-11-13 [email protected] Add didChangeAlwaysUse24HourFormat to WidgetsBinding and update MediaQuery (flutter/flutter#196117)
2026-11-13 [email protected] Build local engine wasm configs on 32-core Linux bots (flutter/flutter#196284)
2026-11-13 [email protected] [Android] Fix Hybrid Composition resize deadlock (flutter/flutter#196255)
2026-11-13 [email protected] Roll Skia from 11f46cfe8838 to e8ff7a4187ec (9 revisions) (flutter/flutter#196557)
2026-11-13 [email protected] [Impeller] Fix SurfaceContextVK submission tracking and double HostBuffer reset (flutter/flutter#196064)
2026-11-13 [email protected] Manual roll Packages from 14f9933 to 015df97 (5 revisions) (flutter/flutter#196558)
2026-11-13 [email protected] Remove routes_transition_test from widgets (flutter/flutter#186620)
2026-11-13 [email protected] [flutter_tools] Replace TargetPlatform.simpleName with cpuArch (flutter/flutter#196264)
2026-11-13 [email protected] Add a deferred platform-view-created path to the shell and use it on Android (flutter/flutter#196342)
2026-11-13 [email protected] Optimize RenderSliverVariedExtentList and introduce protected itemExtentOf API in RenderSliverFixedExtentBoxAdaptor (flutter/flutter#192384)
2026-11-13 [email protected] Revert: [android]: prewarms shaders when the context is created (flutter/flutter#196468)

If this roll has caused a breakage, revert this CL and set the roller
to dry run mode using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD f: scrolling Viewports, list views, slivers, etc. framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

itemExtentBuilder: too many unnecessary calls

3 participants