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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapconductor.core.circle

import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.unit.dp
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.mapconductor.core.ResourceProvider
Expand Down Expand Up @@ -164,4 +165,38 @@ class CircleTileFileDeviceTest {
}
assertTrue(created.none { it.exists() })
}

@Test fun filteringReusesTheFile_andAChangedCircleIsComparedNotTrusted() =
runBlocking {
ResourceProvider.init(
androidx.test.platform.app.InstrumentationRegistry
.getInstrumentation()
.targetContext,
)
val directory = File(ResourceProvider.getAppContext().cacheDir, "mapconductor-circle-geometry")
fun files() = directory.listFiles()?.toSet() ?: emptySet<File>()
val set = CircleTileSet()
try {
val states = List(2500) { state(-60.0 + it * 0.04, -71.0, true, "reuse-$it").apply { zIndex = it } }
set.setCircles(states)
val file = files()
// Filtered out and back in: the same slots, no new file.
set.setCircles(states.filterIndexed { index, _ -> index % 3 != 0 })
assertFalse(set.has(states[0].id))
set.setCircles(states)
assertTrue(set.has(states[0].id))
assertEquals(file, files())
// New objects with the same values match their slots by geometry.
val copies = states.map { it.copy() }
set.setCircles(copies)
assertEquals(file, files())
// A circle changed in place since it was drawn is compared, not
// trusted on its snapshot: the tiles are built again with its colour.
copies[3].fillColor = Color.Green
set.setCircles(copies)
assertEquals(Color.Green.toArgb(), set.renderer.itemAt(3)?.fill)
} finally {
set.dispose()
}
}
}
10 changes: 8 additions & 2 deletions src/main/java/com/mapconductor/core/OverlayCollector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,15 @@ class OverlayCollector<T : ComponentState>(
private fun listenToWrittenStates(states: Map<String, T>) {
val previous = published
published = states
val anyPending = pendingAdds.isNotEmpty()
for ((id, state) in states) {
val pending = pendingAdds.remove(id)
if (pending !== null && pending !== state) stop(pending)
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)
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/mapconductor/core/StateMutationSignal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class StateMutationSignal {
this.listener = listener
}

/** Whether writes are reported to [listener]. */
internal fun isListenedBy(listener: StateMutationListener): Boolean = this.listener === listener

/** Stops reporting, but only if [listener] is the one reported to. */
internal fun stopListening(listener: StateMutationListener) {
if (this.listener === listener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ internal class CircleTileFileStorage private constructor(
}
}

override fun forEachItem(
indices: IntArray,
checkCancelled: () -> Unit,
action: (k: Int, item: TileCircle?) -> Unit,
) {
forEachRecord(indices, checkCancelled) { k, bytes, offset, override ->
action(k, if (bytes == null) override else decode(bytes, offset))
}
// forEachRecord skips indices outside the storage; report them as empty.
for (k in indices.indices) if (indices[k] !in 0 until size) action(k, null)
}

/** What [forEachRecord] calls per circle; primitive arguments, so nothing is boxed per call. */
internal fun interface RecordAction {
/** [bytes] at [offset] is the record, or [bytes] is null and [override] the circle. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class CircleTileSet(
zIndexOf = ::effectiveZIndex,
toTile = TileCircle::from,
canReuse = { state, tile -> tile.matches(state) },
snapshotOf = { it.captureValues() },
createStorage = { states ->
if (states.size <= 2000) {
null
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/mapconductor/core/shapetile/ShapeTileRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,20 @@ internal class ShapeTileRenderer<T : Any>(
previous?.storage?.release()
}

/** [ShapeTileStorage.forEachItem] on the current storage; every item null when there is none. */
fun forEachItem(
indices: IntArray,
checkCancelled: () -> Unit,
action: (k: Int, item: T?) -> Unit,
) {
val current = acquireModel() ?: return indices.indices.forEach { action(it, null) }
try {
current.storage.forEachItem(indices, checkCancelled, action)
} finally {
current.storage.release()
}
}

fun itemAt(index: Int): T? {
val current = acquireModel() ?: return null
return try {
Expand Down
142 changes: 105 additions & 37 deletions src/main/java/com/mapconductor/core/shapetile/ShapeTileSet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ internal class ShapeTileSet<S, T : Any>(
private val toTile: (S) -> T?,
private val canReuse: ((S, T) -> Boolean)? = null,
private val createStorage: (suspend (List<S>) -> ShapeTileStorage<T>?)? = null,
// An object that is the same (===) while the state's values are unchanged and
// is replaced by any write; CircleState's snapshot record. With it a state
// registered before and untouched since is known to match its slot without
// reading the slot's geometry, which a file-backed set reads from disk.
private val snapshotOf: ((S) -> Any)? = null,
) {
val routeId: String = "$routePrefix-${UUID.randomUUID().toString().take(8)}"

Expand All @@ -58,6 +63,10 @@ internal class ShapeTileSet<S, T : Any>(
private var indexById: ObjectIntMap<String> = emptyObjectIntMap()
private var sourceStates: MutableList<S?> = mutableListOf()
private var sourceZ = IntArray(0)

// [snapshotOf] of the state each slot was drawn from, or null when unknown.
// Kept for inactive slots too, so a shape filtered out and back in is known.
private var sourceSnapshots: Array<Any?> = arrayOfNulls(0)
private var active: BitSet? = null

fun ids(): Set<String> =
Expand Down Expand Up @@ -136,53 +145,60 @@ internal class ShapeTileSet<S, T : Any>(
private val pendingRemovals = ArrayList<Int>()
private var flushJob: Job? = null

/**
* [states] in draw order (zIndex, then the order given) with their zIndex.
* Each zIndex is read once: reading a state is not free, and a sort by a
* selector reads it at every comparison.
*/
private fun orderByZ(states: List<S>): Pair<List<S>, IntArray> {
val z = IntArray(states.size) { zIndexOf(states[it]) }
// zIndex in the high half, position in the low: one primitive sort, stable by position.
val keys = LongArray(states.size) { (z[it].toLong() shl 32) or it.toLong() }
keys.sort()
val ordered = ArrayList<S>(states.size)
val orderedZ = IntArray(states.size)
for ((k, key) in keys.withIndex()) {
val i = key.toInt()
ordered.add(states[i])
orderedZ[k] = z[i]
}
return ordered to orderedZ
}

/**
* The states of [states] that [canTile] accepts, without asking it about a
* state registered here before and unchanged since: it was tileable then,
* and [canTile] reads only its values.
*/
fun filterTileable(
states: List<S>,
canTile: (S) -> Boolean,
): List<S> {
val snapshot = snapshotOf ?: return states.filter(canTile)
val (ids, snapshots) = synchronized(lock) { indexById to sourceSnapshots }
return states.filter { state ->
val index = ids.getOrDefault(idOf(state), -1)
(index >= 0 && snapshots[index] != null && snapshots[index] === snapshot(state)) || canTile(state)
}
}

/**
* Registers [states] (replacing whatever was registered) and returns the
* URL template of their tiles.
*/
suspend fun setShapes(states: List<S>): String {
cancelPending()
val ordered = withContext(Dispatchers.Default) { states.sortedBy(zIndexOf) }
val (ordered, orderedZ) = withContext(Dispatchers.Default) { orderByZ(states) }
// Taken before any geometry is made from the states: a write in between
// then shows as a different snapshot, never as a stale match.
val snapshots: Array<Any?>? =
snapshotOf?.let { snapshot -> withContext(Dispatchers.Default) { Array(ordered.size) { snapshot(ordered[it]) } } }
// A filtered collection often refers to the same geometry. Keep the
// original slots, including temporarily hidden shapes, for re-enabling.
val reused =
withContext(Dispatchers.Default) {
val context = currentCoroutineContext()
synchronized(lock) {
val matches = canReuse
if (!registered || matches == null) {
false
} else {
val mask = BitSet(sourceStates.size)
var previous = -1
var checked = 0
val reusable =
ordered.all { state ->
if (++checked % 4096 == 0) context.ensureActive()
val index = indexOf(idOf(state))
val item = if (index >= 0) renderer.itemAt(index) else null
val ok =
index > previous &&
item != null &&
sourceZ[index] == zIndexOf(state) &&
matches(state, item)
if (ok) {
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[indexOf(idOf(state))] = state
active = mask
renderer.setActiveItems(mask)
version++
}
reusable
}
}
synchronized(lock) { reuseLocked(ordered, orderedZ, snapshots) { context.ensureActive() } }
}
if (reused) return template()
var prepared: ShapeTileStorage<T>? = null
Expand Down Expand Up @@ -211,7 +227,8 @@ internal class ShapeTileSet<S, T : Any>(
if (!registered) server.register(routeId, renderer)
indexById = ids
sourceStates = sources
sourceZ = IntArray(ordered.size) { zIndexOf(ordered[it]) }
sourceZ = orderedZ
sourceSnapshots = snapshots ?: arrayOfNulls(ordered.size)
active = null
registered = true
version++
Expand All @@ -223,6 +240,56 @@ internal class ShapeTileSet<S, T : Any>(
}
}

/**
* Shows [ordered] with the slots already registered, if each of them is in
* one, in the same order, with the same zIndex and the same geometry.
*
* A state whose snapshot is the one its slot was drawn from matches without
* reading the slot. The rest are compared with their slot's geometry in one
* pass in storage order: reading a file-backed set one slot at a time in
* draw order missed its block cache on nearly every read.
*/
private fun reuseLocked(
ordered: List<S>,
orderedZ: IntArray,
snapshots: Array<Any?>?,
checkCancelled: () -> Unit,
): Boolean {
val matches = canReuse
if (!registered || matches == null) return false
val mask = BitSet(sourceStates.size)
val slots = IntArray(ordered.size)
var unknown = 0
val toCompare = IntArray(ordered.size)
var previous = -1
for ((k, state) in ordered.withIndex()) {
if (k % 4096 == 0) checkCancelled()
val index = indexOf(idOf(state))
if (index <= previous || sourceZ[index] != orderedZ[k]) return false
val known = snapshots != null && snapshots[k] != null && snapshots[k] === sourceSnapshots[index]
if (!known) toCompare[unknown++] = k
slots[k] = index
mask.set(index)
previous = index
}
if (unknown > 0) {
val compared = IntArray(unknown) { slots[toCompare[it]] }
var ok = true
renderer.forEachItem(compared, checkCancelled) { j, item ->
if (ok && (item == null || !matches(ordered[toCompare[j]], item))) ok = false
}
if (!ok) return false
}
// Cache geometry, not removed application states/handlers.
for (index in sourceStates.indices) if (!mask[index]) sourceStates[index] = null
for ((k, state) in ordered.withIndex()) sourceStates[slots[k]] = state
if (snapshots != null) for (j in 0 until unknown) sourceSnapshots[slots[toCompare[j]]] = snapshots[toCompare[j]]
active = mask
renderer.setActiveItems(mask)
version++
return true
}

fun has(id: String): Boolean =
synchronized(lock) {
indexOf(id).let { it >= 0 && active?.get(it) != false }
Expand Down Expand Up @@ -292,6 +359,7 @@ internal class ShapeTileSet<S, T : Any>(
indexById = emptyObjectIntMap()
sourceStates = mutableListOf()
sourceZ = IntArray(0)
sourceSnapshots = arrayOfNulls(0)
active = null
renderer.clear()
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/mapconductor/core/shapetile/ShapeTileStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ internal abstract class ShapeTileStorage<T : Any> {

abstract operator fun get(index: Int): T?

/**
* Calls [action] with each of [indices]' items (null for an empty slot), in
* whatever order reads the storage best; `k` says which of [indices] it is.
* A file-backed set reads them block by block in storage order, instead of
* one random read per [get].
*/
open fun forEachItem(
indices: IntArray,
checkCancelled: () -> Unit,
action: (k: Int, item: T?) -> Unit,
) {
for (k in indices.indices) {
if (k % 4096 == 0) checkCancelled()
action(k, get(indices[k]))
}
}

abstract fun bbox(index: Int): DoubleArray?

abstract fun updated(updates: List<Pair<Int, T?>>): ShapeTileStorage<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal class ShapeTiler<S>(
// A city of shapes is a second of work; none of it belongs on the map's thread.
val (tileable, useTiles) =
withContext(Dispatchers.Default) {
val tileable = data.filter(canTile)
val tileable = tileSet.filterTileable(data, canTile)
tileable to shouldUseTiles(tileable.size, vertexCount(tileable))
}
val template = if (useTiles) tileSet.setShapes(tileable) else null
Expand Down
Loading