Skip to content

Re-show filtered tiled circles without rereading their geometry - #56

Merged
wf9a5m75 merged 1 commit into
developfrom
perf/tile-reuse-snapshot
Nov 21, 2026
Merged

wf9a5m75 merged 1 commit into
developfrom
perf/tile-reuse-snapshot

Conversation

@wf9a5m75

Copy link
Copy Markdown
Contributor

Stacked on #54 (it touches the same ShapeTileSet index and collector listener code). Merge #54 first. This PR then retargets to develop.

Why

In the 723,800-circle antenna sample on a Pixel 5a, turning one carrier off took about 20 s before the map redrew. Timing probes on the device split it like this:

step time
ShapeTileSet reuse check (does the filtered list fit the slots already drawn?) 9.2 s
canTile over every state 1.6 s
sortedBy(zIndexOf) 0.65 s
collector re-listening on the main thread 1.56 s
the rest (compose hand-over, tiles) ~7 s

The reuse check broke down further:

  • itemAt 6.1 s: one itemAt per circle in draw order. A large set is stored by place, so nearly every read missed the 40 KiB block cache and went to the file.
  • matches 2.7 s and zIndexOf 1.0 s: state fields cost about 1.5 µs each to read off the main thread.

What

  • Snapshot per slot. Each slot keeps the snapshot record of the state it was drawn from (CircleState.captureValues(), passed as ShapeTileSet(snapshotOf = …)).
    • A state whose record is still that one matches its slot without reading anything.
    • The others are compared with their slot's geometry in one pass in storage order. The new ShapeTileStorage.forEachItem reads block by block for the file-backed storage, through the existing forEachRecord.
    • Snapshots are taken before any geometry is made from the states. A write in between therefore shows as a different record and is compared, never trusted.
    • Inactive slots keep their snapshot, so a shape filtered out and back in is known.
  • zIndex once. zIndex is read once per state and sorted as packed primitives (stable by position). sortedBy read it at every comparison. sourceZ reuses those values.
  • filterTileable. canTile is not asked again about a state registered before and unchanged since.
  • Collector fast path. OverlayCollector.listenToWrittenStates skips states that already report to it (StateMutationSignal.isListenedBy), and touches pendingAdds only when it is non-empty.

No public API change (apiCheck passes).

Measured (same toggle, Pixel 5a, 3 runs)

step before after
reuse check 9.2 s 0.78 s
canTile pass 1.6 s 0.73–0.8 s
ordering 0.65 s 0.37 s
controller hand-over to template ~11.6 s 1.7–2.0 s
tap to last tile drawn ~20 s 11.2–14.0 s

The remaining time is mostly the compose hand-over, which varied from 4 to 6 s between runs:

  • Circles removing 147k ids from the persistent map: 1.4 s
  • the collector's pass over the previous map on the main thread: 1.14 s
  • tile drawing: 2.5–3 s, of which z0 is about 1.9 s

Checked

  • :android-sdk-core:testDebugUnitTest: 448 pass. apiCheck passes.
  • Circle device tests on a Pixel 5a (CircleTileFileDeviceTest, CircleTilerDeviceTest): 17 pass, including the new filteringReusesTheFile_andAChangedCircleIsComparedNotTrusted. It checks that:
    • filtering and restoring reuses the geometry file;
    • equal copies are matched by geometry;
    • a circle recoloured after it was drawn is compared rather than trusted, and is redrawn in the new colour.

Turning one carrier off in the 723,800-circle antenna sample took about
20 s on a Pixel 5a before the map redrew, 9.2 s of it in ShapeTileSet's
check that the filtered list fits the slots already drawn. That check
read every circle's geometry with itemAt in draw order, while a large
set is stored by place, so nearly every read missed the block cache and
went to the file; and it read each state's fields again (about 1.5 us a
field off the main thread).

- Each slot keeps the snapshot record (CircleState.captureValues()) of
  the state it was drawn from. A state whose record is still that one
  matches its slot without reading anything; the rest are compared in
  one pass in storage order (ShapeTileStorage.forEachItem, block by block
  for the file).
- zIndex is read once per state and sorted as primitives, not read at
  every comparison by sortedBy.
- canTile is not asked again about a state registered before and
  unchanged since.
- The collector skips states that already report to it.

Same toggle after: the reuse check 0.78 s, route to template 1.7-2.0 s,
tap to the last tile 11-14 s.
@wf9a5m75
wf9a5m75 changed the base branch from perf/circle-bookkeeping to develop November 21, 2026 09:30
@wf9a5m75
wf9a5m75 merged commit 097c765 into develop Nov 21, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant