Skip to content

Keep Marker, Polyline and Polygon state in one snapshot record - #55

Merged
wf9a5m75 merged 1 commit into
developfrom
perf/shape-state-values
Nov 21, 2026
Merged

wf9a5m75 merged 1 commit into
developfrom
perf/shape-state-values

Conversation

@wf9a5m75

Copy link
Copy Markdown
Contributor

Why

MarkerState, PolylineState and PolygonState held each mutable field in its own mutableStateOf behind a NotifyingState: four objects per field (NotifyingState, ParcelableSnapshotMutableState, its StateStateRecord, an AtomicInt), and 7 to 13 fields per state. CircleState already kept its fields in one record.

Per state on the JVM, points excluded:

develop this branch
Marker 1,340 B 293 B
Polyline 942 B 261 B
Polygon 1,152 B 269 B
Circle (unchanged) 309 B —

Street Trees (144,183 markers) on a Pixel 5a, MapLibre, 60 s after opening:

develop this branch
heap dump, app heap shallow total 334 MB 146 MB
dumpsys meminfo Java Heap 270 MB 112 MB

What

  • The three states keep their fields in a private Values data class held in a single mutableStateOf, as CircleState does. The marker's iconRevision (bumped when a URL icon's image arrives) moves into the record instead of its own mutableIntStateOf.
  • A write compares only the field it writes, as the per-field states did. Comparing the whole record would compare every point to change a colour.
  • Writes are serialized with synchronized(this). With one record a write is read-copy-write, and without the lock two threads writing different fields (an animation moving a marker while the app swaps its icon) could lose one of them. Per-field states could not.
  • Public properties and their signatures are unchanged. apiDump adds the private Values classes (80 lines, none removed), as CircleState$Values already is in the baseline.

Behaviour difference

A composition that reads any field of one of these states now recomposes when any field of it changes, as it already did for CircleState. Nothing in android-sdk-compose writes to a state from inside the composition that reads it (Marker(...) builds a new state per recomposition), so this does not loop. The iconRevision KDoc is updated to match.

Checked

  • :android-sdk-core:testDebugUnitTest: 449 pass
  • :android-sdk-core:apiCheck: pass after apiDump
  • On a Pixel 5a (example-app debug, MapLibre):
    • Street Trees draws all 144,183 markers.
    • Polygon: long-press dragging a vertex marker moves the marker and the polygon edge together, mid-drag and after release.
    • Marker Animation: tapping the Bounce marker replays the bounce. Only the marker's column changes between frames, and it settles back where it started.

iOS was measured as well (simulator, 424-488 B per state for all four types, the same @Published-per-field shape) and needs no change.

Independent of #54.

Each mutable field was its own mutableStateOf behind a NotifyingState:
four objects per field, 7 to 13 fields per state. On the JVM a marker
was 1,340 B, a polyline 942 B and a polygon 1,152 B before any points.
CircleState already kept its fields in one record.

The three now do the same: a private Values data class held in a single
mutableStateOf (marker 293 B, polyline 261 B, polygon 269 B). A write
compares only the field it writes, as before, and is serialized so two
threads writing different fields both land. The marker's iconRevision
moves into the record too.

Street Trees (144,183 markers) on a Pixel 5a: app heap 334 MB -> 146 MB.
@wf9a5m75
wf9a5m75 merged commit 2714849 into develop Nov 21, 2026
2 checks passed
@wf9a5m75
wf9a5m75 deleted the perf/shape-state-values branch November 21, 2026 22:25
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