From 2dc843e79ccfa53273200ed1f3ec8def430c6a22 Mon Sep 17 00:00:00 2001 From: Masashi Katsumata Date: Fri, 20 Nov 2026 16:58:07 -0800 Subject: [PATCH] Cut per-shape bookkeeping in the collector and tile index With the 723,800-circle antenna page on a Pixel 5a the Java heap sat a few percent under the 512 MB largeHeap limit. Three structures held one or more objects per circle only to find it again by id: - ShapeTileSet.indexById was a HashMap: a node and a boxed Int per shape, plus a 2M-slot table. It is now an ObjectIntMap, still replaced rather than changed once assigned. - OverlayCollector kept `listening`, a ConcurrentHashMap of every state it listens to, duplicating the flow map. It now finds them in the published map and the pending direct adds, and drops a write from a state that is no longer a member. - Each state got its own closure as listener. The collector is now the one listener for all its states (StateMutationListener). --- .../com/mapconductor/core/OverlayCollector.kt | 71 +++++++++++-------- .../mapconductor/core/StateMutationSignal.kt | 41 ++++++++++- .../core/shapetile/ShapeTileSet.kt | 68 +++++++++++------- 3 files changed, 122 insertions(+), 58 deletions(-) diff --git a/src/main/java/com/mapconductor/core/OverlayCollector.kt b/src/main/java/com/mapconductor/core/OverlayCollector.kt index 3ad897a..c12f962 100644 --- a/src/main/java/com/mapconductor/core/OverlayCollector.kt +++ b/src/main/java/com/mapconductor/core/OverlayCollector.kt @@ -78,7 +78,8 @@ interface OverlayCollectorInterface { class OverlayCollector( private val updateDebounce: Duration, scope: CoroutineScope = CoroutineScope(Dispatchers.Main.immediate), -) : OverlayCollectorInterface { +) : OverlayCollectorInterface, + StateMutationListener { private val scope = scope private val addSharedFlow = MutableSharedFlow(1000) private val removeSharedFlow = MutableSharedFlow(1000) @@ -98,18 +99,18 @@ class OverlayCollector( override val flow = MutableStateFlow>(mutableMapOf()) /** - * Who is currently reporting mutations, kept here rather than read back out - * of [flow]. + * Direct adds waiting for their debounced membership publication. * * Membership reaches [flow] through a debounce, so a state added and * removed inside the same window is not in the map when the removal - * arrives — clearing by id there found nothing and left it reporting for - * the rest of the collector's life. + * arrives; it is found here instead. Together with [published] this is + * every state this collector listens to, without a second map of the whole + * collection (with 720k circles that map alone was 28 MB). */ - private val listening = ConcurrentHashMap() + private val pendingAdds = ConcurrentHashMap() - /** Only direct adds waiting for their debounced membership publication. */ - private val pendingAdds = ConcurrentHashMap.newKeySet() + /** The membership [listenToWrittenStates] last listened to. */ + @Volatile private var published: Map = emptyMap() init { scope.launch { @@ -144,45 +145,55 @@ class OverlayCollector( } private fun listenToWrittenStates(states: Map) { + val previous = published + published = states for ((id, state) in states) { - if (listening[id] !== state) { - listen(state) - } - pendingAdds.remove(id) + val pending = pendingAdds.remove(id) + if (pending !== null && pending !== state) stop(pending) + val old = previous[id] + if (old !== null && old !== state) stop(old) + listen(state) } // Only stop what this path started: a state added through [add] may not // have reached [flow] yet (membership is debounced). - // The listener map already identifies every registered state. Keep a - // small exception set for pending direct adds rather than duplicating - // the IDs of an entire bulk collection in another concurrent hash set. - for (id in listening.keys) { - if (id !in states && id !in pendingAdds) { - unlisten(id) - } + for ((id, state) in previous) { + if (id !in states && pendingAdds[id] !== state) stop(state) } } override suspend fun add(state: T) { - pendingAdds.add(state.id) + val previous = pendingAdds.put(state.id, state) ?: published[state.id] + if (previous !== null && previous !== state) stop(previous) listen(state) addSharedFlow.emit(state) } override fun remove(id: String) { - pendingAdds.remove(id) - unlisten(id) + pendingAdds.remove(id)?.let(::stop) + published[id]?.let(::stop) removeSharedFlow.tryEmit(id) } - private fun listen(state: T) { - val previous = listening.put(state.id, state) - if (previous !== null && previous !== state) previous.mutations.listen(null) - state.mutations.listen { mutated?.trySend(state) } + state.mutations.listen(this, state) } - private fun unlisten(id: String) { - listening.remove(id)?.mutations?.listen(null) + private fun stop(state: T) { + state.mutations.stopListening(this) + } + + /** + * A write to a state this collector listens to. Delivered only while the + * state is still a member (published, or added and waiting to be): a + * state left listening by a membership change this collector never saw + * (a [flow] value replaced before it was collected) is dropped here. + */ + override fun onMutated(state: ComponentState) { + val channel = mutated ?: return + val id = state.id + if (pendingAdds[id] !== state && flow.value[id] !== state && published[id] !== state) return + @Suppress("UNCHECKED_CAST") + channel.trySend(state as T) } override fun setUpdateHandler(handler: (suspend (T) -> Unit)?) { @@ -199,8 +210,8 @@ class OverlayCollector( override fun replaceAll(states: List) { val next = copyOverlayStates(emptyMap()) for (state in states) next[state.id] = state - for (id in listening.keys.toList()) { - if (next[id] !== listening[id]) unlisten(id) + for (map in listOf(published, flow.value, pendingAdds)) { + for ((id, state) in map) if (next[id] !== state) stop(state) } for (state in next.values) listen(state) flow.value = next diff --git a/src/main/java/com/mapconductor/core/StateMutationSignal.kt b/src/main/java/com/mapconductor/core/StateMutationSignal.kt index b1810a2..7f3b2e5 100644 --- a/src/main/java/com/mapconductor/core/StateMutationSignal.kt +++ b/src/main/java/com/mapconductor/core/StateMutationSignal.kt @@ -22,8 +22,12 @@ import androidx.compose.runtime.mutableStateOf */ @InternalMapConductorApi class StateMutationSignal { + /** A `() -> Unit`, or a [StateMutationListener] that is handed [source]. */ @Volatile - private var listener: (() -> Unit)? = null + private var listener: Any? = null + + @Volatile + private var source: ComponentState? = null /** * Sets who hears about writes, or clears it with null. @@ -32,12 +36,39 @@ class StateMutationSignal { * than a list. */ internal fun listen(listener: (() -> Unit)?) { + source = null + this.listener = listener + } + + /** + * Sets [listener] to hear about writes as `listener.onMutated(state)`. + * + * One listener object serves every state of a collector, instead of a + * closure per state: with 720k circles the closures alone were 17 MB. + */ + internal fun listen( + listener: StateMutationListener, + state: ComponentState, + ) { + source = state this.listener = listener } + /** Stops reporting, but only if [listener] is the one reported to. */ + internal fun stopListening(listener: StateMutationListener) { + if (this.listener === listener) { + this.listener = null + source = null + } + } + /** Reports a write. Called from whatever thread performed it. */ fun notifyMutated() { - listener?.invoke() + when (val current = listener) { + null -> Unit + is StateMutationListener -> source?.let(current::onMutated) + else -> @Suppress("UNCHECKED_CAST") (current as () -> Unit)() + } } /** @@ -62,6 +93,12 @@ class StateMutationSignal { fun notifying(initial: T): MutableState = NotifyingState(mutableStateOf(initial), this) } +/** Hears about writes to the states it was given with [StateMutationSignal.listen]. */ +@InternalMapConductorApi +internal fun interface StateMutationListener { + fun onMutated(state: ComponentState) +} + /** * Writes through to [inner] and reports the write, so the field stays readable * from a composition exactly as a plain `mutableStateOf` field would be. diff --git a/src/main/java/com/mapconductor/core/shapetile/ShapeTileSet.kt b/src/main/java/com/mapconductor/core/shapetile/ShapeTileSet.kt index 3e7c68c..6fbe72a 100644 --- a/src/main/java/com/mapconductor/core/shapetile/ShapeTileSet.kt +++ b/src/main/java/com/mapconductor/core/shapetile/ShapeTileSet.kt @@ -1,5 +1,8 @@ package com.mapconductor.core.shapetile +import androidx.collection.MutableObjectIntMap +import androidx.collection.ObjectIntMap +import androidx.collection.emptyObjectIntMap import com.mapconductor.core.InternalMapConductorApi import com.mapconductor.core.tileserver.LocalTileServer import java.util.BitSet @@ -49,32 +52,40 @@ internal class ShapeTileSet( val tileSize: Int get() = renderer.tileSize private val lock = Any() - private var indexById: Map = emptyMap() + // Never changed once assigned, so the sets [ids] hands out stay as they were. + // An ObjectIntMap rather than a HashMap: no node or boxed Int + // per shape, which was 50 MB with 720k circles. + private var indexById: ObjectIntMap = emptyObjectIntMap() private var sourceStates: MutableList = mutableListOf() private var sourceZ = IntArray(0) private var active: BitSet? = null fun ids(): Set = synchronized(lock) { - val mask = active ?: return@synchronized indexById.keys + val mask = active val ids = indexById object : AbstractSet() { - override val size = mask.cardinality() + override val size = mask?.cardinality() ?: ids.size - override fun contains(element: String): Boolean = ids[element]?.let { mask[it] } == true + override fun contains(element: String): Boolean { + val index = ids.getOrDefault(element, -1) + return index >= 0 && mask?.get(index) != false + } - override fun iterator(): Iterator = - ids.entries - .asSequence() - .filter { mask[it.value] } - .map { it.key } - .iterator() + override fun iterator(): Iterator { + val keys = ArrayList(size) + ids.forEach { id, index -> if (mask?.get(index) != false) keys.add(id) } + return keys.iterator() + } } } + /** The slot of [id], or -1. Called with [lock] held. */ + private fun indexOf(id: String): Int = indexById.getOrDefault(id, -1) + fun source(id: String): S? = synchronized(lock) { - indexById[id]?.takeIf { active?.get(it) != false }?.let { sourceStates[it] } + indexOf(id).takeIf { it >= 0 && active?.get(it) != false }?.let { sourceStates[it] } } fun visitSources(action: (S) -> Unit) = @@ -109,7 +120,7 @@ internal class ShapeTileSet( if (seen.add(idOf(state))) states.add(state) } for ((id, state) in pending) { - if (indexById.containsKey(id) && seen.add(id)) states.add(state) + if (indexById.contains(id) && seen.add(id)) states.add(state) } states } @@ -148,16 +159,15 @@ internal class ShapeTileSet( val reusable = ordered.all { state -> if (++checked % 4096 == 0) context.ensureActive() - val index = indexById[idOf(state)] - val item = index?.let(renderer::itemAt) + val index = indexOf(idOf(state)) + val item = if (index >= 0) renderer.itemAt(index) else null val ok = - index != null && - index > previous && + index > previous && item != null && sourceZ[index] == zIndexOf(state) && matches(state, item) if (ok) { - mask.set(index!!) + mask.set(index) previous = index } ok @@ -165,7 +175,7 @@ internal class ShapeTileSet( 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[indexById.getValue(idOf(state))] = state + for (state in ordered) sourceStates[indexOf(idOf(state))] = state active = mask renderer.setActiveItems(mask) version++ @@ -180,7 +190,7 @@ internal class ShapeTileSet( val (shapes, ids, sources) = withContext(Dispatchers.Default) { // Draw order is list order: zIndex, then the order given (sortedBy is stable). - val ids = HashMap(ordered.size * 2) + val ids = MutableObjectIntMap(ordered.size) ordered.forEachIndexed { index, state -> ids[idOf(state)] = index } val sources: MutableList = ordered.toMutableList() prepared = createStorage?.invoke(ordered) @@ -215,7 +225,7 @@ internal class ShapeTileSet( fun has(id: String): Boolean = synchronized(lock) { - indexById[id]?.let { active?.get(it) != false } == true + indexOf(id).let { it >= 0 && active?.get(it) != false } } /** @@ -225,8 +235,9 @@ internal class ShapeTileSet( fun update(state: S) { synchronized(lock) { val id = idOf(state) - if (!indexById.containsKey(id)) return - sourceStates[indexById.getValue(id)] = state + val index = indexOf(id) + if (index < 0) return + sourceStates[index] = state pending[id] = state scheduleFlushLocked() } @@ -239,9 +250,14 @@ internal class ShapeTileSet( */ fun remove(id: String) { synchronized(lock) { - val index = indexById[id] ?: return + val index = indexOf(id) + if (index < 0) return sourceStates[index] = null - indexById = indexById - id + val previous = indexById + indexById = + MutableObjectIntMap(previous.size).also { next -> + previous.forEach { key, value -> if (key != id) next[key] = value } + } active?.let { previous -> val next = previous.clone() as BitSet next.clear(index) @@ -273,7 +289,7 @@ internal class ShapeTileSet( synchronized(lock) { if (registered) server.unregister(routeId) registered = false - indexById = emptyMap() + indexById = emptyObjectIntMap() sourceStates = mutableListOf() sourceZ = IntArray(0) active = null @@ -287,7 +303,7 @@ internal class ShapeTileSet( flushJob = null val updates: List> = pending.values.mapNotNull { state -> - indexById[idOf(state)]?.let { index -> index to toTile(state) } + indexOf(idOf(state)).takeIf { it >= 0 }?.let { index -> index to toTile(state) } } + pendingRemovals.map { index -> index to null } pending.clear() pendingRemovals.clear()