maps: stop recomposing the whole map box on every camera frame (#maprender)
- Commit
5ae3f776144525ac8025e1fdfac21d85d4ca45e7- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 20:24 UTC (22 days ago)
- Parents
18ca750
Owner report: "visibly laggy when moving around". Root cause: `MapContentBox` (the composable that hosts the map surface, top bar, chips, search bar, FAB stack and navigation overlay) read `camera.position` in its own restart scope in two places: - `bearing = camera.position.bearing`, passed to `MapFabStack` for the compass; - `showBodySwitch`, which reads `camera.position.zoom` whenever the globe is on. `CameraState.position` is snapshot state written on every frame of a drag, pinch, fling or `animateTo`, so each frame invalidated the whole box: `MapSurface` (unstable ViewModel/list parameters, so not skipped) re-ran its remember keys and overlay builders and re-entered `VectorMap`, plus every chrome child, all on the main thread while it was also driving the gesture. `MapFabStack` already isolated the scale bar's camera read in a leaf for exactly this reason; the bearing parameter quietly undid it. What changed: - `MapFabStack` no longer takes `bearing`; a private `CameraCompass` leaf reads `camera.position.bearing` itself, like `CameraScaleBar`. The caller can no longer reintroduce the read. - `showBodySwitch` is a `derivedStateOf` (keyed on camera, globe flag and body), so the box is invalidated only when the answer flips at the globe detail threshold, not on every zoom step. Measured: host recompositions per camera frame drop from 1 (the whole box, every frame) to 0; only the compass and scale-bar leaves recompose. Pinned by a new Robolectric test that moves the camera 30 times (target, zoom and bearing) and asserts the stack's host is not recomposed at all (with the old read in the host, each of the 30 writes invalidates it by construction). Verified: Slice1MapChromeComposeTest updated for the new signature plus the recomposition test; gradle checks run in the container.
Diff
maps/src/main/java/com/vayunmathur/maps/ui/MapContentBox.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/MapContentBox.kt b/maps/src/main/java/com/vayunmathur/maps/ui/MapContentBox.ktindex a38e11d08e..408e999995 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/MapContentBox.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/MapContentBox.kt@@ -9,6 +9,9 @@ import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.systemBars import androidx.compose.foundation.layout.windowInsetsPadding import androidx.compose.runtime.Composable+import androidx.compose.runtime.derivedStateOf+import androidx.compose.runtime.getValue+import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext@@ -52,8 +55,15 @@ internal fun MapPageScope.MapContentBox( // globe detail threshold, where the sphere reads as a sphere): the category // chips collapse into it. On the Moon itself the row stays a switch (back to // Earth) — there are no POIs to filter up there.- val showBodySwitch = globeEnabled && camera.position.zoom < GLOBE_DETAIL_ZOOM ||- body == MapBody.Moon+ //+ // Derived, not read directly: `camera.position` changes every frame of a gesture, and a+ // plain read here subscribed this whole box (map surface call included) to it. The derived+ // flag invalidates this scope only when the answer flips at the detail threshold.+ val showBodySwitch by remember(camera, globeEnabled, body) {+ derivedStateOf {+ globeEnabled && camera.position.zoom < GLOBE_DETAIL_ZOOM || body == MapBody.Moon+ }+ } // No app bar. The map is the whole screen and every piece of chrome floats over it, // which is also what keeps the renderer's surface edge-to-edge — a padded parent here // is what used to leave a dead strip along the navigation bar.@@ -178,11 +188,11 @@ internal fun MapPageScope.MapContentBox( // Browse controls, plus the layers and settings buttons, which stay out while // a place is selected and ride above the sheet — see [MapFabStack].- // The compass reads the live camera bearing: the twist gesture rotates the- // whole Compose map with it, and tapping it animates back to north.+ // The compass reads the live camera bearing inside the stack (never here, or+ // every camera frame would recompose this whole box); tapping it animates+ // back to north. MapFabStack( camera = camera,- bearing = camera.position.bearing, browsing = browsing, // Whichever of the three is occupying the bottom: at most one sheet is // ever up, and the search bar is only drawn when neither is.
maps/src/main/java/com/vayunmathur/maps/ui/map/MapFabStack.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/map/MapFabStack.kt b/maps/src/main/java/com/vayunmathur/maps/ui/map/MapFabStack.ktindex 8f6326fa34..5158989a17 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/map/MapFabStack.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/map/MapFabStack.kt@@ -47,6 +47,21 @@ private fun BoxScope.CameraScaleBar(camera: CameraState, lift: () -> Int = { 0 } ) } +/**+ * The compass bound to the live camera bearing, scoped like [CameraScaleBar] so a rotate or+ * pan recomposes only this leaf.+ *+ * The bearing used to be read by the caller (`MapContentBox`) and passed in as a `Double`. That+ * read sat in `MapContentBox`'s own restart scope, so every camera change — every frame of a+ * drag, pinch or fling — recomposed the whole map box: the map surface call and its overlay+ * builders, the top bar, the chips, the search bar and this stack. Reading it here confines the+ * subscription to the one composable that draws it.+ */+@Composable+private fun CameraCompass(camera: CameraState, onResetNorth: () -> Unit) {+ CompassButton(bearing = camera.position.bearing, onResetNorth = onResetNorth)+}+ /** * The map controls: a scale bar bottom-left, a FAB stack bottom-right. *@@ -61,8 +76,8 @@ private fun BoxScope.CameraScaleBar(camera: CameraState, lift: () -> Int = { 0 } */ @Composable fun BoxScope.MapFabStack(+ /** Read only inside the scale-bar and compass leaves, never in this body or its caller. */ camera: CameraState,- bearing: Double, /** Whether the browse-only controls are shown. Layers is drawn either way. */ browsing: Boolean, /**@@ -91,7 +106,7 @@ fun BoxScope.MapFabStack( // Compass on top and only while the map is rotated; layers in the middle; // my-location is the primary action, so it sits closest to the thumb. if (browsing) {- CompassButton(bearing = bearing, onResetNorth = onResetNorth)+ CameraCompass(camera = camera, onResetNorth = onResetNorth) } LayersButton(onClick = onLayers) if (browsing) {
maps/src/test/java/com/vayunmathur/maps/ui/map/Slice1MapChromeComposeTest.kt
diff --git a/maps/src/test/java/com/vayunmathur/maps/ui/map/Slice1MapChromeComposeTest.kt b/maps/src/test/java/com/vayunmathur/maps/ui/map/Slice1MapChromeComposeTest.ktindex 08fc2d2a1e..fff6d67a76 100644--- a/maps/src/test/java/com/vayunmathur/maps/ui/map/Slice1MapChromeComposeTest.kt+++ b/maps/src/test/java/com/vayunmathur/maps/ui/map/Slice1MapChromeComposeTest.kt@@ -73,7 +73,6 @@ class Slice1MapChromeComposeTest { Box { MapFabStack( camera = camera,- bearing = 0.0, browsing = true, lift = { 0 }, onResetNorth = { north++ },@@ -88,6 +87,45 @@ class Slice1MapChromeComposeTest { assertTrue(layers == 0 && parking == 0 && mine == 0 && north == 0) } + @Test+ fun `a moving camera does not recompose the host of the fab stack`() {+ // The host stands in for MapContentBox, which carries the map surface: it must not+ // subscribe to the camera through the stack, or every frame of a drag recomposes the+ // whole map box. Only the scale-bar and compass leaves may read `camera.position`.+ val camera = CameraState(CameraPosition(GeoPoint(-122.4, 37.7), 12.0))+ var hostCompositions = 0+ rule.setContent {+ Box {+ hostCompositions+++ MapFabStack(+ camera = camera,+ browsing = true,+ lift = { 0 },+ onResetNorth = {},+ onLayers = {},+ onParking = {},+ onMyLocation = {},+ )+ }+ }+ rule.waitForIdle()+ val settled = hostCompositions+ repeat(30) { frame ->+ rule.runOnIdle {+ camera.position = camera.position.copy(+ target = GeoPoint(-122.4 + frame * 1e-3, 37.7),+ zoom = 12.0 + frame * 0.05,+ bearing = frame * 3.0,+ )+ }+ rule.waitForIdle()+ }+ assertTrue(+ hostCompositions == settled,+ "30 camera frames recomposed the host ${hostCompositions - settled} times",+ )+ }+ // --- WaypointList --------------------------------------------------------- private fun place(name: String, lon: Double) = SpecificFeature.GenericPlace(