Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/main/java/com/mapconductor/compose/ListVersions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.mapconductor.compose

import androidx.compose.runtime.snapshotFlow
import androidx.compose.runtime.snapshots.SnapshotStateList
import com.mapconductor.core.InternalMapConductorApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map

/**
* [list] now and, when it is a `mutableStateListOf`, again after each change
* made to it in place.
*
* `Markers(states)`, `Circles(states)`, `Polylines(states)` and
* `Polygons(states)` hand their list to the map again when they are given a
* different list. A `mutableStateListOf` that the app adds to is the same list
* every time, so the states added after the first hand-over never reached the
* map: a pin per tap logged one more marker per tap and drew none.
*
* Each version is a copy taken on the main thread, so the work done with it on
* another thread never reads the list while the app is changing it.
*/
@InternalMapConductorApi
internal fun <T> listVersions(list: List<T>): Flow<List<T>> =
if (list is SnapshotStateList<T>) {
// Wrapped so that each change counts, even one that leaves an equal list:
// a state's equals does not look at its id, so replacing one state with
// another drawn the same way would otherwise go unseen.
snapshotFlow { Version(list.toList()) }.map { it.items }
} else {
flowOf(list)
}

@InternalMapConductorApi
private class Version<T>(
val items: List<T>,
)
54 changes: 29 additions & 25 deletions src/main/java/com/mapconductor/compose/circle/CircleCompose.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapconductor.compose.circle

import com.mapconductor.compose.listVersions
import com.mapconductor.compose.LocalOverlayTileActivity
import com.mapconductor.core.controller.OverlayKind
import com.mapconductor.compose.rememberAddProgress
Expand Down Expand Up @@ -74,34 +75,37 @@ fun MapViewScope.Circles(
val tracking = onAddStart != null || onAddProgress != null || onAddEnd != null

LaunchedEffect(states) {
val prevIds = prevIdsState.value
val (nextIds, added) =
// Again after each add to a mutableStateListOf: see listVersions.
listVersions(states).collect { list ->
val prevIds = prevIdsState.value
val (nextIds, added) =
withContext(Dispatchers.Default) {
overlayStateIds(list) to progress.newStates(list, prevIds, tracking)
}
prevIdsState.value = nextIds
// Before the hand-over, so that no arrival comes ahead of it.
progress.track(list, nextIds, added)
withContext(Dispatchers.Default) {
overlayStateIds(states) to progress.newStates(states, prevIds, tracking)
}
prevIdsState.value = nextIds
// Before the hand-over, so that no arrival comes ahead of it.
progress.track(states, nextIds, added)
withContext(Dispatchers.Default) {
// 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 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
// 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 == list.size) OverlayStateList(list.toList()) else null
collector.flow.update { current ->
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 list) next[state.id] = state
next
}
}
ownList[0] = handOver?.takeIf { collector.flow.value === it }
}
ownList[0] = handOver?.takeIf { collector.flow.value === it }
}
}

Expand Down
58 changes: 31 additions & 27 deletions src/main/java/com/mapconductor/compose/marker/MarkerCompose.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapconductor.compose.marker

import com.mapconductor.compose.listVersions
import com.mapconductor.compose.rememberAddProgress
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
Expand Down Expand Up @@ -62,35 +63,38 @@ fun MapViewScope.Markers(
val tracking = onAddStart != null || onAddProgress != null || onAddEnd != null

LaunchedEffect(states) {
val startedAt = SystemClock.elapsedRealtime()
markerTrace("collector effect start count=${states.size}")
val prevIds = prevIdsState.value
val (nextIds, added) =
// Again after each add to a mutableStateListOf: see listVersions.
listVersions(states).collect { list ->
val startedAt = SystemClock.elapsedRealtime()
markerTrace("collector effect start count=${list.size}")
val prevIds = prevIdsState.value
val (nextIds, added) =
withContext(Dispatchers.Default) {
list.asSequence().map { it.id }.toSet() to progress.newStates(list, prevIds, tracking)
}
markerTrace("collector ids created count=${nextIds.size}")
prevIdsState.value = nextIds
// Before the hand-over, so that no arrival comes ahead of it.
progress.track(list, nextIds, added)
// For very large marker sets, avoid per-marker SharedFlow emits which can backpressure and
// block rendering; instead publish the whole map in one StateFlow update.
withContext(Dispatchers.Default) {
states.asSequence().map { it.id }.toSet() to progress.newStates(states, prevIds, tracking)
}
markerTrace("collector ids created count=${nextIds.size}")
prevIdsState.value = nextIds
// Before the hand-over, so that no arrival comes ahead of it.
progress.track(states, nextIds, added)
// For very large marker sets, avoid per-marker SharedFlow emits which can backpressure and
// block rendering; instead publish the whole map in one StateFlow update.
withContext(Dispatchers.Default) {
val removedIds = prevIds - nextIds
// Merge only the ids this Markers() owns into the shared collector so
// several Markers()/Marker() can share one map without clobbering each
// other. Assigning the whole map (a full replace) dropped everyone
// else's markers, so the last Markers() to run won and erased the rest.
collector.flow.update { current ->
val next = current.toMutableMap()
for (id in removedIds) next.remove(id)
for (state in states) next[state.id] = state
next
val removedIds = prevIds - nextIds
// Merge only the ids this Markers() owns into the shared collector so
// several Markers()/Marker() can share one map without clobbering each
// other. Assigning the whole map (a full replace) dropped everyone
// else's markers, so the last Markers() to run won and erased the rest.
collector.flow.update { current ->
val next = current.toMutableMap()
for (id in removedIds) next.remove(id)
for (state in list) next[state.id] = state
next
}
markerTrace(
"collector flow assigned count=${list.size} " +
"elapsedMs=${SystemClock.elapsedRealtime() - startedAt}",
)
}
markerTrace(
"collector flow assigned count=${states.size} " +
"elapsedMs=${SystemClock.elapsedRealtime() - startedAt}",
)
}
}

Expand Down
46 changes: 25 additions & 21 deletions src/main/java/com/mapconductor/compose/polygon/PolygonComponent.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapconductor.compose.polygon

import com.mapconductor.compose.listVersions
import com.mapconductor.compose.LocalOverlayTileActivity
import com.mapconductor.core.controller.OverlayKind
import com.mapconductor.compose.rememberAddProgress
Expand Down Expand Up @@ -72,29 +73,32 @@ fun MapViewScope.Polygons(
val tracking = onAddStart != null || onAddProgress != null || onAddEnd != null

LaunchedEffect(states) {
val prevIds = prevIdsState.value
val (nextIds, added) =
withContext(Dispatchers.Default) {
// Union the holes once per newly added state, as Polygon(state) does.
for (state in states) {
if (state.id !in prevIds && state.holes.size > 1) {
state.unionHolesInPlace()
// Again after each add to a mutableStateListOf: see listVersions.
listVersions(states).collect { list ->
val prevIds = prevIdsState.value
val (nextIds, added) =
withContext(Dispatchers.Default) {
// Union the holes once per newly added state, as Polygon(state) does.
for (state in list) {
if (state.id !in prevIds && state.holes.size > 1) {
state.unionHolesInPlace()
}
}
list.asSequence().map { it.id }.toSet() to progress.newStates(list, prevIds, tracking)
}
prevIdsState.value = nextIds
// Before the hand-over, so that no arrival comes ahead of it.
progress.track(list, nextIds, added)
withContext(Dispatchers.Default) {
val removedIds = prevIds - nextIds
// Merge only the ids this Polygons() owns, so several
// Polygons()/Polygon() can share one map's collector.
collector.flow.update { current ->
val next = current.toMutableMap()
for (id in removedIds) next.remove(id)
for (state in list) next[state.id] = state
next
}
states.asSequence().map { it.id }.toSet() to progress.newStates(states, prevIds, tracking)
}
prevIdsState.value = nextIds
// Before the hand-over, so that no arrival comes ahead of it.
progress.track(states, nextIds, added)
withContext(Dispatchers.Default) {
val removedIds = prevIds - nextIds
// Merge only the ids this Polygons() owns, so several
// Polygons()/Polygon() can share one map's collector.
collector.flow.update { current ->
val next = current.toMutableMap()
for (id in removedIds) next.remove(id)
for (state in states) next[state.id] = state
next
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapconductor.compose.polyline

import com.mapconductor.compose.listVersions
import com.mapconductor.compose.LocalOverlayTileActivity
import com.mapconductor.core.controller.OverlayKind
import com.mapconductor.compose.rememberAddProgress
Expand Down Expand Up @@ -71,23 +72,26 @@ fun MapViewScope.Polylines(
val tracking = onAddStart != null || onAddProgress != null || onAddEnd != null

LaunchedEffect(states) {
val prevIds = prevIdsState.value
val (nextIds, added) =
// Again after each add to a mutableStateListOf: see listVersions.
listVersions(states).collect { list ->
val prevIds = prevIdsState.value
val (nextIds, added) =
withContext(Dispatchers.Default) {
list.asSequence().map { it.id }.toSet() to progress.newStates(list, prevIds, tracking)
}
prevIdsState.value = nextIds
// Before the hand-over, so that no arrival comes ahead of it.
progress.track(list, nextIds, added)
withContext(Dispatchers.Default) {
states.asSequence().map { it.id }.toSet() to progress.newStates(states, prevIds, tracking)
}
prevIdsState.value = nextIds
// Before the hand-over, so that no arrival comes ahead of it.
progress.track(states, nextIds, added)
withContext(Dispatchers.Default) {
val removedIds = prevIds - nextIds
// Merge only the ids this Polylines() owns, so several
// Polylines()/Polyline() can share one map's collector.
collector.flow.update { current ->
val next = current.toMutableMap()
for (id in removedIds) next.remove(id)
for (state in states) next[state.id] = state
next
val removedIds = prevIds - nextIds
// Merge only the ids this Polylines() owns, so several
// Polylines()/Polyline() can share one map's collector.
collector.flow.update { current ->
val next = current.toMutableMap()
for (id in removedIds) next.remove(id)
for (state in list) next[state.id] = state
next
}
}
}
}
Expand Down
Loading
Loading