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
71 changes: 41 additions & 30 deletions src/main/java/com/mapconductor/core/OverlayCollector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ interface OverlayCollectorInterface<T : ComponentState> {
class OverlayCollector<T : ComponentState>(
private val updateDebounce: Duration,
scope: CoroutineScope = CoroutineScope(Dispatchers.Main.immediate),
) : OverlayCollectorInterface<T> {
) : OverlayCollectorInterface<T>,
StateMutationListener {
private val scope = scope
private val addSharedFlow = MutableSharedFlow<T>(1000)
private val removeSharedFlow = MutableSharedFlow<String>(1000)
Expand All @@ -98,18 +99,18 @@ class OverlayCollector<T : ComponentState>(
override val flow = MutableStateFlow<MutableMap<String, T>>(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<String, T>()
private val pendingAdds = ConcurrentHashMap<String, T>()

/** Only direct adds waiting for their debounced membership publication. */
private val pendingAdds = ConcurrentHashMap.newKeySet<String>()
/** The membership [listenToWrittenStates] last listened to. */
@Volatile private var published: Map<String, T> = emptyMap()

init {
scope.launch {
Expand Down Expand Up @@ -144,45 +145,55 @@ class OverlayCollector<T : ComponentState>(
}

private fun listenToWrittenStates(states: Map<String, T>) {
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)?) {
Expand All @@ -199,8 +210,8 @@ class OverlayCollector<T : ComponentState>(
override fun replaceAll(states: List<T>) {
val next = copyOverlayStates<T>(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
Expand Down
41 changes: 39 additions & 2 deletions src/main/java/com/mapconductor/core/StateMutationSignal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)()
}
}

/**
Expand All @@ -62,6 +93,12 @@ class StateMutationSignal {
fun <T> notifying(initial: T): MutableState<T> = 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.
Expand Down
68 changes: 42 additions & 26 deletions src/main/java/com/mapconductor/core/shapetile/ShapeTileSet.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -49,32 +52,40 @@ internal class ShapeTileSet<S, T : Any>(
val tileSize: Int get() = renderer.tileSize

private val lock = Any()
private var indexById: Map<String, Int> = emptyMap()
// 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
// per shape, which was 50 MB with 720k circles.
private var indexById: ObjectIntMap<String> = emptyObjectIntMap()
private var sourceStates: MutableList<S?> = mutableListOf()
private var sourceZ = IntArray(0)
private var active: BitSet? = null

fun ids(): Set<String> =
synchronized(lock) {
val mask = active ?: return@synchronized indexById.keys
val mask = active
val ids = indexById
object : AbstractSet<String>() {
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<String> =
ids.entries
.asSequence()
.filter { mask[it.value] }
.map { it.key }
.iterator()
override fun iterator(): Iterator<String> {
val keys = ArrayList<String>(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) =
Expand Down Expand Up @@ -109,7 +120,7 @@ internal class ShapeTileSet<S, T : Any>(
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
}
Expand Down Expand Up @@ -148,24 +159,23 @@ internal class ShapeTileSet<S, T : Any>(
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
}
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++
Expand All @@ -180,7 +190,7 @@ internal class ShapeTileSet<S, T : Any>(
val (shapes, ids, sources) =
withContext(Dispatchers.Default) {
// Draw order is list order: zIndex, then the order given (sortedBy is stable).
val ids = HashMap<String, Int>(ordered.size * 2)
val ids = MutableObjectIntMap<String>(ordered.size)
ordered.forEachIndexed { index, state -> ids[idOf(state)] = index }
val sources: MutableList<S?> = ordered.toMutableList()
prepared = createStorage?.invoke(ordered)
Expand Down Expand Up @@ -215,7 +225,7 @@ internal class ShapeTileSet<S, T : Any>(

fun has(id: String): Boolean =
synchronized(lock) {
indexById[id]?.let { active?.get(it) != false } == true
indexOf(id).let { it >= 0 && active?.get(it) != false }
}

/**
Expand All @@ -225,8 +235,9 @@ internal class ShapeTileSet<S, T : Any>(
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()
}
Expand All @@ -239,9 +250,14 @@ internal class ShapeTileSet<S, T : Any>(
*/
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<String>(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)
Expand Down Expand Up @@ -273,7 +289,7 @@ internal class ShapeTileSet<S, T : Any>(
synchronized(lock) {
if (registered) server.unregister(routeId)
registered = false
indexById = emptyMap()
indexById = emptyObjectIntMap()
sourceStates = mutableListOf()
sourceZ = IntArray(0)
active = null
Expand All @@ -287,7 +303,7 @@ internal class ShapeTileSet<S, T : Any>(
flushJob = null
val updates: List<Pair<Int, T?>> =
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()
Expand Down
Loading