library: pack route and rail polylines on the render thread (#maprender)
- Commit
f206c233f255b64dcdfee37c42c577c96726d13b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 20:31 UTC (22 days ago)
- Parents
ff50673
Part of "visibly laggy when moving around", during navigation. Root cause: `applyRoute`/`applyRailLines` flattened every segment's points into the JNI arrays on the main thread on each push. During turn-by-turn the phone re-pushes the whole route every 5 m of travel (MapSurface keys the overlay on the travelled split), so a long route cost an O(points) pass plus three array allocations on the main thread several times a second at highway speed, on top of the (now off-main) native stroke and upload. What changed: a pure `packPolylines(segments)` does the flattening, and both setters call it inside the render-thread task, so the main thread only captures the immutable segment list and the style values. Output is unchanged (same drawable filter, order, lengths and colours). Measured: new PackPolylinesTest packs a 100k-point route (400 steps) and prints the time (best of five) in the test output; all of it moves off the main thread. The test also pins the packed layout and the short-segment drop. Verified: PackPolylinesTest; RendererStateTest still exercises both setters without a surface (no thread, no native call).
Diff
library/map/src/main/java/com/vayunmathur/library/map/SurfaceMapOverlays.kt
diff --git a/library/map/src/main/java/com/vayunmathur/library/map/SurfaceMapOverlays.kt b/library/map/src/main/java/com/vayunmathur/library/map/SurfaceMapOverlays.ktindex 40549971a3..1087a95335 100644--- a/library/map/src/main/java/com/vayunmathur/library/map/SurfaceMapOverlays.kt+++ b/library/map/src/main/java/com/vayunmathur/library/map/SurfaceMapOverlays.kt@@ -215,18 +215,23 @@ internal fun SurfaceMapRenderer.tapLabelsIn(box: DpRect, layerIds: Set<String>): } } -internal fun SurfaceMapRenderer.applyRoute() {- // Drawable segments only: fewer than two points strokes nothing, and dropping them- // here keeps the native side's per-segment ranges aligned with the colours.- val drawable = routeSegments?.filter { it.points.size >= 2 }- if (drawable.isNullOrEmpty()) {- onRenderThread { h -> MapNative.clearRoute(h) }- return- }- // Flat lon/lat pairs across every segment, plus a point count and colour per- // segment: three bulk arrays cross JNI instead of one call per point, which for a- // cross-city route is thousands of crossings saved. Built here rather than held,- // because a route is set once and this is not a per-frame path.+/**+ * A route-like overlay flattened for JNI: lon/lat pairs across every segment, plus a point+ * count and colour per segment — three bulk arrays cross JNI instead of one call per point.+ */+internal class PackedPolylines(val flat: FloatArray, val lengths: IntArray, val colors: IntArray)++/**+ * Flatten the drawable segments (two or more points; fewer strokes nothing, and dropping them+ * keeps the native per-segment ranges aligned with the colours), or `null` when none are.+ *+ * O(points), so it runs inside the render-thread task rather than on the main thread: during+ * navigation the phone re-pushes the whole route every 5 m of travel (the travelled split+ * moves), and a cross-country route is tens of thousands of points.+ */+internal fun packPolylines(segments: List<RouteSegment>?): PackedPolylines? {+ val drawable = segments?.filter { it.points.size >= 2 }+ if (drawable.isNullOrEmpty()) return null val flat = FloatArray(drawable.sumOf { it.points.size } * 2) val lengths = IntArray(drawable.size) val colors = IntArray(drawable.size)@@ -240,38 +245,39 @@ internal fun SurfaceMapRenderer.applyRoute() { at++ } }+ return PackedPolylines(flat, lengths, colors)+}++// The segment lists are handed to the render thread as they are: hosts build a fresh list+// per push (RouteOverlay is an immutable value) and never mutate one after passing it.+internal fun SurfaceMapRenderer.applyRoute() {+ val segments = routeSegments val width = routeStyle.width.value val casingWidth = routeStyle.casingWidth.value val casingColor = routeStyle.casingColor.toArgb()- // Packed here, stroked and uploaded on the render thread.- onRenderThread { h -> MapNative.setRoute(h, flat, lengths, colors, width, casingWidth, casingColor) }+ onRenderThread { h ->+ val packed = packPolylines(segments)+ if (packed == null) {+ MapNative.clearRoute(h)+ } else {+ MapNative.setRoute(h, packed.flat, packed.lengths, packed.colors, width, casingWidth, casingColor)+ }+ } } internal fun SurfaceMapRenderer.applyRailLines() {- // Drawable segments only, like the route: fewer than two points strokes- // nothing, and dropping them keeps the native per-segment ranges aligned.- val drawable = railSegments?.filter { it.points.size >= 2 }- if (drawable.isNullOrEmpty()) {- onRenderThread { h -> MapNative.clearRailLines(h) }- return- }- val flat = FloatArray(drawable.sumOf { it.points.size } * 2)- val lengths = IntArray(drawable.size)- val colors = IntArray(drawable.size)- var at = 0- drawable.forEachIndexed { i, segment ->- lengths[i] = segment.points.size- colors[i] = segment.color.toArgb()- for (point in segment.points) {- flat[at * 2] = point.longitude.toFloat()- flat[at * 2 + 1] = point.latitude.toFloat()- at++- }- }+ val segments = railSegments val width = railStyle.width.value val casingWidth = railStyle.casingWidth.value val casingColor = railStyle.casingColor.toArgb()- onRenderThread { h -> MapNative.setRailLines(h, flat, lengths, colors, width, casingWidth, casingColor) }+ onRenderThread { h ->+ val packed = packPolylines(segments)+ if (packed == null) {+ MapNative.clearRailLines(h)+ } else {+ MapNative.setRailLines(h, packed.flat, packed.lengths, packed.colors, width, casingWidth, casingColor)+ }+ } } internal fun SurfaceMapRenderer.applyTraffic() {
library/map/src/test/java/com/vayunmathur/library/map/PackPolylinesTest.kt
diff --git a/library/map/src/test/java/com/vayunmathur/library/map/PackPolylinesTest.kt b/library/map/src/test/java/com/vayunmathur/library/map/PackPolylinesTest.ktnew file mode 100644index 0000000000..9e3930523d--- /dev/null+++ b/library/map/src/test/java/com/vayunmathur/library/map/PackPolylinesTest.kt@@ -0,0 +1,53 @@+package com.vayunmathur.library.map++import androidx.compose.ui.graphics.Color+import androidx.compose.ui.graphics.toArgb+import kotlin.test.Test+import kotlin.test.assertContentEquals+import kotlin.test.assertNotNull+import kotlin.test.assertNull++/**+ * The route/rail JNI packing, which now runs on the render thread instead of the main thread+ * (navigation re-pushes the whole route every 5 m of travel).+ */+class PackPolylinesTest {++ @Test+ fun drawable_segments_flatten_in_order_and_short_ones_drop() {+ val packed = assertNotNull(+ packPolylines(+ listOf(+ RouteSegment(listOf(GeoPoint(1.0, 2.0), GeoPoint(3.0, 4.0)), Color.Red),+ RouteSegment(listOf(GeoPoint(9.0, 9.0)), Color.Green),+ RouteSegment(listOf(GeoPoint(5.0, 6.0), GeoPoint(7.0, 8.0), GeoPoint(9.0, 10.0)), Color.Blue),+ ),+ ),+ )+ assertContentEquals(floatArrayOf(1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f), packed.flat)+ assertContentEquals(intArrayOf(2, 3), packed.lengths)+ assertContentEquals(intArrayOf(Color.Red.toArgb(), Color.Blue.toArgb()), packed.colors)+ }++ @Test+ fun nothing_drawable_packs_to_null() {+ assertNull(packPolylines(null))+ assertNull(packPolylines(emptyList()))+ assertNull(packPolylines(listOf(RouteSegment(listOf(GeoPoint(0.0, 0.0)), Color.Red))))+ }++ @Test+ fun a_cross_country_route_packs_and_reports_its_cost() {+ // ~2,000 km of driving at a vertex every ~20 m, split into 400 steps.+ val steps = (0 until 400).map { s ->+ RouteSegment(List(250) { i -> GeoPoint(-120.0 + s * 0.05 + i * 2e-4, 37.0) }, Color.Red)+ }+ packPolylines(steps)+ val millis = (1..5).minOf {+ val t = System.nanoTime()+ assertNotNull(packPolylines(steps))+ (System.nanoTime() - t) / 1e6+ }+ println("route packing: 100000 points in ${"%.2f".format(millis)} ms (now on the render thread)")+ }+}