diff --git a/src/main/java/com/mapconductor/core/OverlayCollector.kt b/src/main/java/com/mapconductor/core/OverlayCollector.kt index 627c426..2571453 100644 --- a/src/main/java/com/mapconductor/core/OverlayCollector.kt +++ b/src/main/java/com/mapconductor/core/OverlayCollector.kt @@ -78,8 +78,7 @@ interface OverlayCollectorInterface { class OverlayCollector( private val updateDebounce: Duration, scope: CoroutineScope = CoroutineScope(Dispatchers.Main.immediate), -) : OverlayCollectorInterface, - StateMutationListener { +) : OverlayCollectorInterface { private val scope = scope private val addSharedFlow = MutableSharedFlow(1000) private val removeSharedFlow = MutableSharedFlow(1000) @@ -112,6 +111,38 @@ class OverlayCollector( /** The membership [listenToWrittenStates] last listened to. */ @Volatile private var published: Map = emptyMap() + /** + * What this collector listens through, replaced at every + * [listenToWrittenStates] pass. A state's listener says whether it reports + * here (a [PassToken] of this collector) and whether this pass has seen it + * (the current one): telling which states left a bulk list takes one read + * per state, not a lookup of each id in the other map. + */ + @Volatile private var token = PassToken(this) + + @InternalMapConductorApi + private class PassToken( + val collector: OverlayCollector<*>, + ) : StateMutationListener { + override fun onMutated(state: ComponentState) = collector.onMutated(state, this) + } + + /** Each state of [states] with its id, walking an [OverlayStateList]'s list rather than its index. */ + private inline fun forEachState( + states: Map, + action: (id: String, state: T) -> Unit, + ) { + if (states is OverlayStateList<*>) { + @Suppress("UNCHECKED_CAST") + (states as OverlayStateList).forEachState { action(it.id, it) } + } else { + for ((id, state) in states) action(id, state) + } + } + + private fun tokenOf(state: T): PassToken? = + (state.mutations.currentListener as? PassToken)?.takeIf { it.collector === this } + init { scope.launch { addSharedFlow.debounceBatch(5.milliseconds, 100).collect { states -> @@ -147,23 +178,31 @@ class OverlayCollector( private fun listenToWrittenStates(states: Map) { val previous = published published = states + val pass = PassToken(this) + token = pass val anyPending = pendingAdds.isNotEmpty() - for ((id, state) in states) { + forEachState(states) { id, state -> 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) + if (tokenOf(state) == null) { + val old = previous[id] + if (old !== null && old !== state) stop(old) + } + state.mutations.listen(pass, state) } + // What the last pass listened to and this one did not see has left. // Only stop what this path started: a state added through [add] may not // have reached [flow] yet (membership is debounced). - for ((id, state) in previous) { - if (id !in states && pendingAdds[id] !== state) stop(state) + val stillPending = pendingAdds.isNotEmpty() + forEachState(previous) { id, state -> + val heard = tokenOf(state) + if (heard != null && heard !== pass && (!stillPending || pendingAdds[id] !== state)) { + state.mutations.stopListening(heard) + } } } @@ -181,11 +220,11 @@ class OverlayCollector( } private fun listen(state: T) { - state.mutations.listen(this, state) + state.mutations.listen(token, state) } private fun stop(state: T) { - state.mutations.stopListening(this) + tokenOf(state)?.let { state.mutations.stopListening(it) } } /** @@ -194,10 +233,15 @@ class OverlayCollector( * 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) { + private fun onMutated( + state: ComponentState, + via: PassToken, + ) { val channel = mutated ?: return val id = state.id - if (pendingAdds[id] !== state && flow.value[id] !== state && published[id] !== state) return + // Heard through the latest pass's token: the latest published membership + // has it. Otherwise look it up (pending adds, a newer flow value). + if (via !== token && pendingAdds[id] !== state && flow.value[id] !== state && published[id] !== state) return @Suppress("UNCHECKED_CAST") channel.trySend(state as T) } diff --git a/src/main/java/com/mapconductor/core/OverlayStateMaps.kt b/src/main/java/com/mapconductor/core/OverlayStateMaps.kt index c38e4bb..b5f6853 100644 --- a/src/main/java/com/mapconductor/core/OverlayStateMaps.kt +++ b/src/main/java/com/mapconductor/core/OverlayStateMaps.kt @@ -18,3 +18,70 @@ fun overlayStateIds(states: Iterable): Set = persistentHashSetOf().builder().apply { for (state in states) add(state.id) }.build() + +/** + * A collector's membership handed over as the list a collection was given, + * for collections of hundreds of thousands of states: building and walking a + * map of them cost seconds per hand-over, and the readers that matter (the + * collector and the overlays) only walk the states in order. + * + * [states] is the list itself. Looking a key up builds an index on first use. + * Like every published generation it is not to be written to; a write goes to + * the index and [states] then follows the index. + * + * The ids in [list] must be distinct: [states] and [forEachState] do not + * deduplicate. + */ +@InternalMapConductorApi +class OverlayStateList( + private val list: List, +) : AbstractMutableMap() { + @Volatile private var index: LinkedHashMap? = null + + @Volatile private var written = false + + private fun index(): LinkedHashMap = + index ?: synchronized(this) { + index ?: LinkedHashMap((list.size / 0.75f).toInt() + 1).also { map -> + for (state in list) map[state.id] = state + index = map + } + } + + /** The states in order: the list handed over, unless this map was written to. */ + fun states(): List = if (written) ArrayList(index().values) else list + + /** Calls [action] for each state in order, without building the index. */ + inline fun forEachState(action: (T) -> Unit) { + for (state in states()) action(state) + } + + /** The ids, through the index. */ + val ids: Set get() = index().keys + + override val size: Int get() = if (written) index().size else list.size + + override val entries: MutableSet> get() = index().entries + + override fun get(key: String): T? = index()[key] + + override fun containsKey(key: String): Boolean = index().containsKey(key) + + override fun put( + key: String, + value: T, + ): T? { + written = true + return index().put(key, value) + } + + override fun remove(key: String): T? { + written = true + return index().remove(key) + } + + override fun clear() { + written = true + index().clear() + } +} diff --git a/src/main/java/com/mapconductor/core/StateMutationSignal.kt b/src/main/java/com/mapconductor/core/StateMutationSignal.kt index 7deb718..af78e80 100644 --- a/src/main/java/com/mapconductor/core/StateMutationSignal.kt +++ b/src/main/java/com/mapconductor/core/StateMutationSignal.kt @@ -54,8 +54,8 @@ class StateMutationSignal { this.listener = listener } - /** Whether writes are reported to [listener]. */ - internal fun isListenedBy(listener: StateMutationListener): Boolean = this.listener === listener + /** Who writes are reported to now: a `() -> Unit`, a [StateMutationListener], or null. */ + internal val currentListener: Any? get() = listener /** Stops reporting, but only if [listener] is the one reported to. */ internal fun stopListening(listener: StateMutationListener) { diff --git a/src/main/java/com/mapconductor/core/circle/CircleOverlay.kt b/src/main/java/com/mapconductor/core/circle/CircleOverlay.kt index d76fb87..274c4a8 100644 --- a/src/main/java/com/mapconductor/core/circle/CircleOverlay.kt +++ b/src/main/java/com/mapconductor/core/circle/CircleOverlay.kt @@ -1,6 +1,7 @@ package com.mapconductor.core.circle import com.mapconductor.core.OverlayAddedTracker +import com.mapconductor.core.OverlayStateList import com.mapconductor.core.controller.BaseMapViewController import com.mapconductor.core.controller.MapViewControllerInterface import com.mapconductor.core.map.MapOverlayInterface @@ -29,8 +30,10 @@ class CircleOverlay tilingOptions?.value?.let { options -> (controller as? BaseMapViewController)?.circleTilers()?.forEach { it.options = options } } - val states = data.values.toList() + @Suppress("UNCHECKED_CAST") + val list = data as? OverlayStateList + val states = list?.states() ?: data.values.toList() capable.compositionCircles(states) - added.synced(controller, states, capable::hasCircle, sourceIds = data.keys) + added.synced(controller, states, capable::hasCircle, sourceIds = list?.ids ?: data.keys) } } diff --git a/src/test/java/com/mapconductor/core/OverlayCollectorMutationTest.kt b/src/test/java/com/mapconductor/core/OverlayCollectorMutationTest.kt index 08f4f40..0099694 100644 --- a/src/test/java/com/mapconductor/core/OverlayCollectorMutationTest.kt +++ b/src/test/java/com/mapconductor/core/OverlayCollectorMutationTest.kt @@ -73,6 +73,56 @@ class OverlayCollectorMutationTest { assertEquals(setOf("single", "bulk"), collector.flow.value.keys) } + @Test + fun aBulkListThatDropsAndRestoresSomeStatesKeepsTheRestReporting() = runTest { + val collector = collector(StandardTestDispatcher(testScheduler)) + val seen = mutableListOf() + collector.setUpdateHandler { seen.add("${it.id}=${it.label}") } + val states = List(5) { Thing("s$it", "0") } + fun publish(list: List) { + collector.flow.value = copyOverlayStates(list.associateBy { it.id }) + } + publish(states) + advanceUntilIdle() + publish(states.filterIndexed { index, _ -> index % 2 == 0 }) + advanceUntilIdle() + states.forEach { it.label = "1" } + advanceUntilIdle() + assertEquals(listOf("s0=1", "s2=1", "s4=1"), seen.sorted()) + seen.clear() + publish(states) + advanceUntilIdle() + states.forEach { it.label = "2" } + advanceUntilIdle() + assertEquals(states.map { "${it.id}=2" }, seen.sorted()) + } + + @Test + fun aListHandOverReportsWritesOfItsStatesOnly() = runTest { + val collector = collector(StandardTestDispatcher(testScheduler)) + val seen = mutableListOf() + collector.setUpdateHandler { seen.add("${it.id}=${it.label}") } + val states = List(4) { Thing("l$it", "0") } + collector.flow.value = OverlayStateList(states) + advanceUntilIdle() + collector.flow.value = OverlayStateList(states.take(2)) + advanceUntilIdle() + states.forEach { it.label = "1" } + advanceUntilIdle() + assertEquals(listOf("l0=1", "l1=1"), seen.sorted()) + } + + @Test + fun aWrittenListFollowsItsIndex() { + val states = List(3) { Thing("w$it", "0") } + val list = OverlayStateList(states) + assertEquals(states, list.states()) + assertEquals(states[1], list["w1"]) + list.remove("w1") + assertEquals(listOf(states[0], states[2]), list.states()) + assertEquals(setOf("w0", "w2"), list.keys) + } + private fun collector(dispatcher: TestDispatcher) = OverlayCollector( updateDebounce = window,