From caa46da73ec59be8ad767059274cff164b217a24 Mon Sep 17 00:00:00 2001 From: Masashi Katsumata Date: Fri, 20 Nov 2026 20:00:15 -0800 Subject: [PATCH] Hand Circles' list over as it is while it is the only writer Circles merged its list into a copy of the collector's persistent map at every change: removing the 147k circles of one carrier and putting back 576k took about 1.8 s in the antenna sample on a Pixel 5a. While the published membership is still the list this Circles() published last (or there is nothing yet), and the ids are distinct, it now publishes an OverlayStateList of its list instead. Otherwise it merges as before, so several Circles()/Circle() still share a collector. Needs android-sdk-core's OverlayStateList. --- .../compose/circle/CircleCompose.kt | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/mapconductor/compose/circle/CircleCompose.kt b/src/main/java/com/mapconductor/compose/circle/CircleCompose.kt index 881efc0..263883e 100644 --- a/src/main/java/com/mapconductor/compose/circle/CircleCompose.kt +++ b/src/main/java/com/mapconductor/compose/circle/CircleCompose.kt @@ -12,6 +12,7 @@ import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import com.mapconductor.compose.MapViewScope import com.mapconductor.core.circle.CircleState +import com.mapconductor.core.OverlayStateList import com.mapconductor.core.copyOverlayStates import com.mapconductor.core.overlayStateIds import com.mapconductor.core.circle.CircleTilingOptions @@ -63,6 +64,8 @@ fun MapViewScope.Circles( ) { val collector = LocalCircleCollector.current val prevIdsState = remember { mutableStateOf>(emptySet()) } + // The membership this Circles() last published as its own list, while it is still the published one. + val ownList = remember { arrayOfNulls>(1) } val progress = rememberAddProgress(onAddStart, onAddProgress, onAddEnd) val tracking = onAddStart != null || onAddProgress != null || onAddEnd != null @@ -76,14 +79,25 @@ fun MapViewScope.Circles( // Before the hand-over, so that no arrival comes ahead of it. progress.track(states, nextIds, added) withContext(Dispatchers.Default) { - // Merge only the ids this Circles() owns, so several - // Circles()/Circle() can share one map's collector. + // While this Circles() is the only writer -- the published membership + // is still the list it published last, or there is nothing yet -- its + // list is handed over as it is. Building a map of hundreds of + // thousands of circles took seconds per hand-over, and so did walking it. + val handOver = if (nextIds.size == states.size) OverlayStateList(states.toList()) else null collector.flow.update { current -> - val next = copyOverlayStates(current) - for (id in prevIds) if (id !in nextIds) next.remove(id) - for (state in states) next[state.id] = state - next + val alone = current === ownList[0] || (current.isEmpty() && prevIds.isEmpty()) + if (handOver != null && alone) { + handOver + } else { + // Merge only the ids this Circles() owns, so several + // Circles()/Circle() can share one map's collector. + val next = copyOverlayStates(current) + for (id in prevIds) if (id !in nextIds) next.remove(id) + for (state in states) next[state.id] = state + next + } } + ownList[0] = handOver?.takeIf { collector.flow.value === it } } } @@ -93,11 +107,16 @@ fun MapViewScope.Circles( val ownIds = prevIdsState.value if (ownIds.isNotEmpty()) { collector.flow.update { current -> - val next = copyOverlayStates(current) - for (id in ownIds) next.remove(id) - next + if (current === ownList[0]) { + mutableMapOf() + } else { + val next = copyOverlayStates(current) + for (id in ownIds) next.remove(id) + next + } } } + ownList[0] = null prevIdsState.value = emptySet() } }