From 2b77969f5db8b59198cb50e74038c0a4822ff0e2 Mon Sep 17 00:00:00 2001 From: Masashi Katsumata Date: Fri, 20 Nov 2026 19:32:54 -0800 Subject: [PATCH] Re-show filtered tiled circles without rereading their geometry 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. --- .../core/circle/CircleTileFileDeviceTest.kt | 35 +++++ .../com/mapconductor/core/OverlayCollector.kt | 10 +- .../mapconductor/core/StateMutationSignal.kt | 3 + .../core/circle/CircleTileFileStorage.kt | 12 ++ .../mapconductor/core/circle/CircleTileSet.kt | 1 + .../core/shapetile/ShapeTileRenderer.kt | 14 ++ .../core/shapetile/ShapeTileSet.kt | 142 +++++++++++++----- .../core/shapetile/ShapeTileStorage.kt | 17 +++ .../mapconductor/core/shapetile/ShapeTiler.kt | 2 +- 9 files changed, 196 insertions(+), 40 deletions(-) diff --git a/src/androidTest/java/com/mapconductor/core/circle/CircleTileFileDeviceTest.kt b/src/androidTest/java/com/mapconductor/core/circle/CircleTileFileDeviceTest.kt index 3590c36..d3665ee 100644 --- a/src/androidTest/java/com/mapconductor/core/circle/CircleTileFileDeviceTest.kt +++ b/src/androidTest/java/com/mapconductor/core/circle/CircleTileFileDeviceTest.kt @@ -1,6 +1,7 @@ package com.mapconductor.core.circle import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.unit.dp import androidx.test.ext.junit.runners.AndroidJUnit4 import com.mapconductor.core.ResourceProvider @@ -164,4 +165,38 @@ class CircleTileFileDeviceTest { } assertTrue(created.none { it.exists() }) } + + @Test fun filteringReusesTheFile_andAChangedCircleIsComparedNotTrusted() = + runBlocking { + ResourceProvider.init( + androidx.test.platform.app.InstrumentationRegistry + .getInstrumentation() + .targetContext, + ) + val directory = File(ResourceProvider.getAppContext().cacheDir, "mapconductor-circle-geometry") + fun files() = directory.listFiles()?.toSet() ?: emptySet() + val set = CircleTileSet() + try { + val states = List(2500) { state(-60.0 + it * 0.04, -71.0, true, "reuse-$it").apply { zIndex = it } } + set.setCircles(states) + val file = files() + // Filtered out and back in: the same slots, no new file. + set.setCircles(states.filterIndexed { index, _ -> index % 3 != 0 }) + assertFalse(set.has(states[0].id)) + set.setCircles(states) + assertTrue(set.has(states[0].id)) + assertEquals(file, files()) + // New objects with the same values match their slots by geometry. + val copies = states.map { it.copy() } + set.setCircles(copies) + assertEquals(file, files()) + // A circle changed in place since it was drawn is compared, not + // trusted on its snapshot: the tiles are built again with its colour. + copies[3].fillColor = Color.Green + set.setCircles(copies) + assertEquals(Color.Green.toArgb(), set.renderer.itemAt(3)?.fill) + } finally { + set.dispose() + } + } } diff --git a/src/main/java/com/mapconductor/core/OverlayCollector.kt b/src/main/java/com/mapconductor/core/OverlayCollector.kt index c12f962..627c426 100644 --- a/src/main/java/com/mapconductor/core/OverlayCollector.kt +++ b/src/main/java/com/mapconductor/core/OverlayCollector.kt @@ -147,9 +147,15 @@ class OverlayCollector( private fun listenToWrittenStates(states: Map) { val previous = published published = states + val anyPending = pendingAdds.isNotEmpty() for ((id, state) in states) { - val pending = pendingAdds.remove(id) - if (pending !== null && pending !== state) stop(pending) + if (anyPending) { + val pending = pendingAdds.remove(id) + if (pending !== null && pending !== state) stop(pending) + } + // Already reporting here: whatever held its id before was stopped + // when it started. Most of a bulk list is, from the last pass. + if (state.mutations.isListenedBy(this)) continue val old = previous[id] if (old !== null && old !== state) stop(old) listen(state) diff --git a/src/main/java/com/mapconductor/core/StateMutationSignal.kt b/src/main/java/com/mapconductor/core/StateMutationSignal.kt index 7f3b2e5..7deb718 100644 --- a/src/main/java/com/mapconductor/core/StateMutationSignal.kt +++ b/src/main/java/com/mapconductor/core/StateMutationSignal.kt @@ -54,6 +54,9 @@ class StateMutationSignal { this.listener = listener } + /** Whether writes are reported to [listener]. */ + internal fun isListenedBy(listener: StateMutationListener): Boolean = this.listener === listener + /** Stops reporting, but only if [listener] is the one reported to. */ internal fun stopListening(listener: StateMutationListener) { if (this.listener === listener) { diff --git a/src/main/java/com/mapconductor/core/circle/CircleTileFileStorage.kt b/src/main/java/com/mapconductor/core/circle/CircleTileFileStorage.kt index 02acf9a..4a22d2a 100644 --- a/src/main/java/com/mapconductor/core/circle/CircleTileFileStorage.kt +++ b/src/main/java/com/mapconductor/core/circle/CircleTileFileStorage.kt @@ -91,6 +91,18 @@ internal class CircleTileFileStorage private constructor( } } + override fun forEachItem( + indices: IntArray, + checkCancelled: () -> Unit, + action: (k: Int, item: TileCircle?) -> Unit, + ) { + forEachRecord(indices, checkCancelled) { k, bytes, offset, override -> + action(k, if (bytes == null) override else decode(bytes, offset)) + } + // forEachRecord skips indices outside the storage; report them as empty. + for (k in indices.indices) if (indices[k] !in 0 until size) action(k, null) + } + /** What [forEachRecord] calls per circle; primitive arguments, so nothing is boxed per call. */ internal fun interface RecordAction { /** [bytes] at [offset] is the record, or [bytes] is null and [override] the circle. */ diff --git a/src/main/java/com/mapconductor/core/circle/CircleTileSet.kt b/src/main/java/com/mapconductor/core/circle/CircleTileSet.kt index 9675782..597f001 100644 --- a/src/main/java/com/mapconductor/core/circle/CircleTileSet.kt +++ b/src/main/java/com/mapconductor/core/circle/CircleTileSet.kt @@ -48,6 +48,7 @@ class CircleTileSet( zIndexOf = ::effectiveZIndex, toTile = TileCircle::from, canReuse = { state, tile -> tile.matches(state) }, + snapshotOf = { it.captureValues() }, createStorage = { states -> if (states.size <= 2000) { null diff --git a/src/main/java/com/mapconductor/core/shapetile/ShapeTileRenderer.kt b/src/main/java/com/mapconductor/core/shapetile/ShapeTileRenderer.kt index 83ceed4..a7daeb4 100644 --- a/src/main/java/com/mapconductor/core/shapetile/ShapeTileRenderer.kt +++ b/src/main/java/com/mapconductor/core/shapetile/ShapeTileRenderer.kt @@ -190,6 +190,20 @@ internal class ShapeTileRenderer( previous?.storage?.release() } + /** [ShapeTileStorage.forEachItem] on the current storage; every item null when there is none. */ + fun forEachItem( + indices: IntArray, + checkCancelled: () -> Unit, + action: (k: Int, item: T?) -> Unit, + ) { + val current = acquireModel() ?: return indices.indices.forEach { action(it, null) } + try { + current.storage.forEachItem(indices, checkCancelled, action) + } finally { + current.storage.release() + } + } + fun itemAt(index: Int): T? { val current = acquireModel() ?: return null return try { diff --git a/src/main/java/com/mapconductor/core/shapetile/ShapeTileSet.kt b/src/main/java/com/mapconductor/core/shapetile/ShapeTileSet.kt index 6fbe72a..88327c6 100644 --- a/src/main/java/com/mapconductor/core/shapetile/ShapeTileSet.kt +++ b/src/main/java/com/mapconductor/core/shapetile/ShapeTileSet.kt @@ -43,6 +43,11 @@ internal class ShapeTileSet( private val toTile: (S) -> T?, private val canReuse: ((S, T) -> Boolean)? = null, private val createStorage: (suspend (List) -> ShapeTileStorage?)? = null, + // An object that is the same (===) while the state's values are unchanged and + // is replaced by any write; CircleState's snapshot record. With it a state + // registered before and untouched since is known to match its slot without + // reading the slot's geometry, which a file-backed set reads from disk. + private val snapshotOf: ((S) -> Any)? = null, ) { val routeId: String = "$routePrefix-${UUID.randomUUID().toString().take(8)}" @@ -58,6 +63,10 @@ internal class ShapeTileSet( private var indexById: ObjectIntMap = emptyObjectIntMap() private var sourceStates: MutableList = mutableListOf() private var sourceZ = IntArray(0) + + // [snapshotOf] of the state each slot was drawn from, or null when unknown. + // Kept for inactive slots too, so a shape filtered out and back in is known. + private var sourceSnapshots: Array = arrayOfNulls(0) private var active: BitSet? = null fun ids(): Set = @@ -136,53 +145,60 @@ internal class ShapeTileSet( private val pendingRemovals = ArrayList() private var flushJob: Job? = null + /** + * [states] in draw order (zIndex, then the order given) with their zIndex. + * Each zIndex is read once: reading a state is not free, and a sort by a + * selector reads it at every comparison. + */ + private fun orderByZ(states: List): Pair, IntArray> { + val z = IntArray(states.size) { zIndexOf(states[it]) } + // zIndex in the high half, position in the low: one primitive sort, stable by position. + val keys = LongArray(states.size) { (z[it].toLong() shl 32) or it.toLong() } + keys.sort() + val ordered = ArrayList(states.size) + val orderedZ = IntArray(states.size) + for ((k, key) in keys.withIndex()) { + val i = key.toInt() + ordered.add(states[i]) + orderedZ[k] = z[i] + } + return ordered to orderedZ + } + + /** + * The states of [states] that [canTile] accepts, without asking it about a + * state registered here before and unchanged since: it was tileable then, + * and [canTile] reads only its values. + */ + fun filterTileable( + states: List, + canTile: (S) -> Boolean, + ): List { + val snapshot = snapshotOf ?: return states.filter(canTile) + val (ids, snapshots) = synchronized(lock) { indexById to sourceSnapshots } + return states.filter { state -> + val index = ids.getOrDefault(idOf(state), -1) + (index >= 0 && snapshots[index] != null && snapshots[index] === snapshot(state)) || canTile(state) + } + } + /** * Registers [states] (replacing whatever was registered) and returns the * URL template of their tiles. */ suspend fun setShapes(states: List): String { cancelPending() - val ordered = withContext(Dispatchers.Default) { states.sortedBy(zIndexOf) } + val (ordered, orderedZ) = withContext(Dispatchers.Default) { orderByZ(states) } + // Taken before any geometry is made from the states: a write in between + // then shows as a different snapshot, never as a stale match. + val snapshots: Array? = + snapshotOf?.let { snapshot -> withContext(Dispatchers.Default) { Array(ordered.size) { snapshot(ordered[it]) } } } // A filtered collection often refers to the same geometry. Keep the // original slots, including temporarily hidden shapes, for re-enabling. val reused = withContext(Dispatchers.Default) { val context = currentCoroutineContext() - synchronized(lock) { - val matches = canReuse - if (!registered || matches == null) { - false - } else { - val mask = BitSet(sourceStates.size) - var previous = -1 - var checked = 0 - val reusable = - ordered.all { state -> - if (++checked % 4096 == 0) context.ensureActive() - val index = indexOf(idOf(state)) - val item = if (index >= 0) renderer.itemAt(index) else null - val ok = - index > previous && - item != null && - sourceZ[index] == zIndexOf(state) && - matches(state, item) - if (ok) { - mask.set(index) - previous = index - } - ok - } - if (reusable) { - // Cache geometry, not removed application states/handlers. - for (index in sourceStates.indices) if (!mask[index]) sourceStates[index] = null - for (state in ordered) sourceStates[indexOf(idOf(state))] = state - active = mask - renderer.setActiveItems(mask) - version++ - } - reusable - } - } + synchronized(lock) { reuseLocked(ordered, orderedZ, snapshots) { context.ensureActive() } } } if (reused) return template() var prepared: ShapeTileStorage? = null @@ -211,7 +227,8 @@ internal class ShapeTileSet( if (!registered) server.register(routeId, renderer) indexById = ids sourceStates = sources - sourceZ = IntArray(ordered.size) { zIndexOf(ordered[it]) } + sourceZ = orderedZ + sourceSnapshots = snapshots ?: arrayOfNulls(ordered.size) active = null registered = true version++ @@ -223,6 +240,56 @@ internal class ShapeTileSet( } } + /** + * Shows [ordered] with the slots already registered, if each of them is in + * one, in the same order, with the same zIndex and the same geometry. + * + * A state whose snapshot is the one its slot was drawn from matches without + * reading the slot. The rest are compared with their slot's geometry in one + * pass in storage order: reading a file-backed set one slot at a time in + * draw order missed its block cache on nearly every read. + */ + private fun reuseLocked( + ordered: List, + orderedZ: IntArray, + snapshots: Array?, + checkCancelled: () -> Unit, + ): Boolean { + val matches = canReuse + if (!registered || matches == null) return false + val mask = BitSet(sourceStates.size) + val slots = IntArray(ordered.size) + var unknown = 0 + val toCompare = IntArray(ordered.size) + var previous = -1 + for ((k, state) in ordered.withIndex()) { + if (k % 4096 == 0) checkCancelled() + val index = indexOf(idOf(state)) + if (index <= previous || sourceZ[index] != orderedZ[k]) return false + val known = snapshots != null && snapshots[k] != null && snapshots[k] === sourceSnapshots[index] + if (!known) toCompare[unknown++] = k + slots[k] = index + mask.set(index) + previous = index + } + if (unknown > 0) { + val compared = IntArray(unknown) { slots[toCompare[it]] } + var ok = true + renderer.forEachItem(compared, checkCancelled) { j, item -> + if (ok && (item == null || !matches(ordered[toCompare[j]], item))) ok = false + } + if (!ok) return false + } + // Cache geometry, not removed application states/handlers. + for (index in sourceStates.indices) if (!mask[index]) sourceStates[index] = null + for ((k, state) in ordered.withIndex()) sourceStates[slots[k]] = state + if (snapshots != null) for (j in 0 until unknown) sourceSnapshots[slots[toCompare[j]]] = snapshots[toCompare[j]] + active = mask + renderer.setActiveItems(mask) + version++ + return true + } + fun has(id: String): Boolean = synchronized(lock) { indexOf(id).let { it >= 0 && active?.get(it) != false } @@ -292,6 +359,7 @@ internal class ShapeTileSet( indexById = emptyObjectIntMap() sourceStates = mutableListOf() sourceZ = IntArray(0) + sourceSnapshots = arrayOfNulls(0) active = null renderer.clear() } diff --git a/src/main/java/com/mapconductor/core/shapetile/ShapeTileStorage.kt b/src/main/java/com/mapconductor/core/shapetile/ShapeTileStorage.kt index a15b579..8180225 100644 --- a/src/main/java/com/mapconductor/core/shapetile/ShapeTileStorage.kt +++ b/src/main/java/com/mapconductor/core/shapetile/ShapeTileStorage.kt @@ -13,6 +13,23 @@ internal abstract class ShapeTileStorage { abstract operator fun get(index: Int): T? + /** + * Calls [action] with each of [indices]' items (null for an empty slot), in + * whatever order reads the storage best; `k` says which of [indices] it is. + * A file-backed set reads them block by block in storage order, instead of + * one random read per [get]. + */ + open fun forEachItem( + indices: IntArray, + checkCancelled: () -> Unit, + action: (k: Int, item: T?) -> Unit, + ) { + for (k in indices.indices) { + if (k % 4096 == 0) checkCancelled() + action(k, get(indices[k])) + } + } + abstract fun bbox(index: Int): DoubleArray? abstract fun updated(updates: List>): ShapeTileStorage diff --git a/src/main/java/com/mapconductor/core/shapetile/ShapeTiler.kt b/src/main/java/com/mapconductor/core/shapetile/ShapeTiler.kt index ad3b385..2f8e525 100644 --- a/src/main/java/com/mapconductor/core/shapetile/ShapeTiler.kt +++ b/src/main/java/com/mapconductor/core/shapetile/ShapeTiler.kt @@ -36,7 +36,7 @@ internal class ShapeTiler( // A city of shapes is a second of work; none of it belongs on the map's thread. val (tileable, useTiles) = withContext(Dispatchers.Default) { - val tileable = data.filter(canTile) + val tileable = tileSet.filterTileable(data, canTile) tileable to shouldUseTiles(tileable.size, vertexCount(tileable)) } val template = if (useTiles) tileSet.setShapes(tileable) else null