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
70 changes: 57 additions & 13 deletions src/main/java/com/mapconductor/core/OverlayCollector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ interface OverlayCollectorInterface<T : ComponentState> {
class OverlayCollector<T : ComponentState>(
private val updateDebounce: Duration,
scope: CoroutineScope = CoroutineScope(Dispatchers.Main.immediate),
) : OverlayCollectorInterface<T>,
StateMutationListener {
) : OverlayCollectorInterface<T> {
private val scope = scope
private val addSharedFlow = MutableSharedFlow<T>(1000)
private val removeSharedFlow = MutableSharedFlow<String>(1000)
Expand Down Expand Up @@ -112,6 +111,38 @@ class OverlayCollector<T : ComponentState>(
/** The membership [listenToWrittenStates] last listened to. */
@Volatile private var published: Map<String, T> = 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<String, T>,
action: (id: String, state: T) -> Unit,
) {
if (states is OverlayStateList<*>) {
@Suppress("UNCHECKED_CAST")
(states as OverlayStateList<T>).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 ->
Expand Down Expand Up @@ -147,23 +178,31 @@ class OverlayCollector<T : ComponentState>(
private fun listenToWrittenStates(states: Map<String, T>) {
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)
}
}
}

Expand All @@ -181,11 +220,11 @@ class OverlayCollector<T : ComponentState>(
}

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) }
}

/**
Expand All @@ -194,10 +233,15 @@ class OverlayCollector<T : ComponentState>(
* 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)
}
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/com/mapconductor/core/OverlayStateMaps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,70 @@ fun overlayStateIds(states: Iterable<ComponentState>): Set<String> =
persistentHashSetOf<String>().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<T : ComponentState>(
private val list: List<T>,
) : AbstractMutableMap<String, T>() {
@Volatile private var index: LinkedHashMap<String, T>? = null

@Volatile private var written = false

private fun index(): LinkedHashMap<String, T> =
index ?: synchronized(this) {
index ?: LinkedHashMap<String, T>((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<T> = 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<String> get() = index().keys

override val size: Int get() = if (written) index().size else list.size

override val entries: MutableSet<MutableMap.MutableEntry<String, T>> 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()
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/mapconductor/core/StateMutationSignal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/mapconductor/core/circle/CircleOverlay.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<CircleState>
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)
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/mapconductor/core/circle/CircleTiler.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapconductor.core.circle

import kotlinx.coroutines.flow.StateFlow
import com.mapconductor.core.normalizeLng
import com.mapconductor.core.raster.RasterLayerSource
import com.mapconductor.core.raster.RasterLayerState
Expand Down Expand Up @@ -68,6 +69,9 @@ class CircleTiler(
onTemplate = { template, tileSize -> onTemplate?.invoke(template, tileSize) },
)

/** Busy while this tiler takes a list or its tiles are drawn. See [com.mapconductor.core.controller.overlayTileActivity]. */
internal val activity: StateFlow<Boolean> get() = tileSet.shapes.activity.active

/** Re-decides tiling for [data] and returns the ids drawn as tiles. */
suspend fun route(data: List<CircleState>): Set<String> = tiler.route(data)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.mapconductor.core.controller

import com.mapconductor.core.polygon.PolygonTiler
import com.mapconductor.core.polygon.PolygonController
import com.mapconductor.core.OnCameraMoveHandler
import com.mapconductor.core.OnMapEventHandler
import com.mapconductor.core.OnMapInitializedHandler
Expand Down Expand Up @@ -306,6 +308,10 @@ abstract class BaseMapViewController :
internal fun circleTilers(): List<CircleTiler> =
overlayControllersOf(OverlayKind.Circle).mapNotNull { (it as? CircleController<*>)?.tiler }

/** The tilers of this map's polygon controllers. */
internal fun polygonTilers(): List<PolygonTiler> =
overlayControllersOf(OverlayKind.Polygon).mapNotNull { (it as? PolygonController<*>)?.tiler }

/** The tilers of this map's polyline controllers, for [com.mapconductor.core.polyline.PolylineOverlay]'s per-map options. */
internal fun polylineTilers(): List<PolylineTiler> =
overlayControllersOf(OverlayKind.Polyline).mapNotNull { (it as? PolylineController<*>)?.tiler }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mapconductor.core.controller

import com.mapconductor.core.InternalMapConductorApi
import kotlinx.coroutines.flow.StateFlow

/**
* Whether each tiler of [kind] on this map is busy -- taking a list or drawing
* tiles -- for the collections' onAddStart / onAddEnd. Empty for a kind without
* tiles.
*/
@InternalMapConductorApi
fun BaseMapViewController.overlayTileActivity(kind: OverlayKind): List<StateFlow<Boolean>> =
when (kind) {
OverlayKind.Circle -> circleTilers().map { it.activity }
OverlayKind.Polyline -> polylineTilers().map { it.activity }
OverlayKind.Polygon -> polygonTilers().map { it.activity }
else -> emptyList()
}
8 changes: 7 additions & 1 deletion src/main/java/com/mapconductor/core/polygon/PolygonTiler.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapconductor.core.polygon

import kotlinx.coroutines.flow.StateFlow
import com.mapconductor.core.raster.RasterLayerSource
import com.mapconductor.core.raster.RasterLayerState
import com.mapconductor.core.shapetile.ShapeTiler
Expand Down Expand Up @@ -42,16 +43,21 @@ class PolygonTiler(
) {
var onTemplate: (suspend (template: String?, tileSize: Int) -> Unit)? = null

private val tileSet = PolygonTileSet()

private val tiler =
ShapeTiler(
tileSet = PolygonTileSet().shapes,
tileSet = tileSet.shapes,
canTile = PolygonTileSet::canTile,
vertexCount = PolygonTileSet::vertexCount,
shouldUseTiles = { count, vertices -> options.shouldUseTiles(count, vertices) },
idOf = { it.id },
onTemplate = { template, tileSize -> onTemplate?.invoke(template, tileSize) },
)

/** Busy while this tiler takes a list or its tiles are drawn. See [com.mapconductor.core.controller.overlayTileActivity]. */
internal val activity: StateFlow<Boolean> get() = tileSet.shapes.activity.active

/** Re-decides tiling for [data] and returns the ids drawn as tiles. */
suspend fun route(data: List<PolygonState>): Set<String> = tiler.route(data)

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/mapconductor/core/polyline/PolylineTiler.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapconductor.core.polyline

import kotlinx.coroutines.flow.StateFlow
import com.mapconductor.core.raster.RasterLayerSource
import com.mapconductor.core.raster.RasterLayerState
import com.mapconductor.core.shapetile.ShapeTiler
Expand Down Expand Up @@ -59,6 +60,9 @@ class PolylineTiler(
onTemplate = { template, tileSize -> onTemplate?.invoke(template, tileSize) },
)

/** Busy while this tiler takes a list or its tiles are drawn. See [com.mapconductor.core.controller.overlayTileActivity]. */
internal val activity: StateFlow<Boolean> get() = tileSet.shapes.activity.active

/** Re-decides tiling for [data] and returns the ids drawn as tiles. */
suspend fun route(data: List<PolylineState>): Set<String> = tiler.route(data)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,19 @@ internal class ShapeTileRenderer<T : Any>(
}
}

/** Busy while a tile is being drawn; the owning tiler adds its own work. */
val activity = TileActivity()

override fun renderTile(request: TileRequest): ByteArray? = renderTile(request) { false }

override fun renderTile(
request: TileRequest,
isCancelled: () -> Boolean,
): ByteArray? = activity.during { drawTile(request, isCancelled) }

private fun drawTile(
request: TileRequest,
isCancelled: () -> Boolean,
): ByteArray? {
val generationAtStart = generation.get()
val m = acquireModel() ?: return null
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/mapconductor/core/shapetile/ShapeTileSet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ internal class ShapeTileSet<S, T : Any>(

val tileSize: Int get() = renderer.tileSize

/** Busy while tiles of this set are drawn, and while [ShapeTiler] hands it a list. */
val activity: TileActivity get() = renderer.activity

private val lock = Any()
// Never changed once assigned, so the sets [ids] hands out stay as they were.
// An ObjectIntMap rather than a HashMap<String, Int>: no node or boxed Int
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/mapconductor/core/shapetile/ShapeTiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ internal class ShapeTiler<S>(

/** Re-decides tiling for [data] and returns the ids drawn as tiles. */
suspend fun route(data: List<S>): Set<String> {
tileSet.activity.begin()
try {
return routeWork(data)
} finally {
tileSet.activity.end()
}
}

private suspend fun routeWork(data: List<S>): Set<String> {
// A city of shapes is a second of work; none of it belongs on the map's thread.
val (tileable, useTiles) =
withContext(Dispatchers.Default) {
Expand Down
Loading
Loading