diff --git a/Sources/MapConductorCore/circle/CircleController.swift b/Sources/MapConductorCore/circle/CircleController.swift index 0d8252d..311557f 100644 --- a/Sources/MapConductorCore/circle/CircleController.swift +++ b/Sources/MapConductorCore/circle/CircleController.swift @@ -55,15 +55,24 @@ where Renderer.ActualCircle == ActualCircle { open func add(data: [CircleState]) async { if circleManager.isDestroyed { return } + // Busy from here, not only while the tiler routes: the list waits for + // the controller and its bookkeeping, and the collection's onAddEnd + // must not come in that gap. Once the map has the tiles, drawing them + // is what keeps the tiler busy. + let hold = TileActivityHold(tiler?.activity) + defer { hold.release() } let landed: [CircleState] = await semaphore.withPermit { if circleManager.isDestroyed { return [] } - // The app's list: anything it no longer has can be added again later. - addedTracker.retain(Set(data.map(\.id))) var landed: [CircleState] = [] let wasTiled = tiledIds // `route` returns once the tile layer's URL template has been handed // to the map (the provider's `onTemplate` awaits its raster upsert). let tiled = await tiler?.route(data) ?? [] + hold.release() + // The app's list: anything it no longer has can be added again later. + // After the route, which the map's tiles wait for; before anything is + // admitted. + addedTracker.retain(Set(data.map(\.id))) circleManager.setTiledSource(tiler) if circleManager.isDestroyed { return [] } tiledIds = tiled diff --git a/Sources/MapConductorCore/circle/CircleTileSet.swift b/Sources/MapConductorCore/circle/CircleTileSet.swift index c27cdcd..d51b820 100644 --- a/Sources/MapConductorCore/circle/CircleTileSet.swift +++ b/Sources/MapConductorCore/circle/CircleTileSet.swift @@ -119,6 +119,7 @@ public final class CircleTileSet { zIndex: CircleTileSet.effectiveZIndex, fileBackedCircles: true, spatialKey: { CircleGeometryFile.mortonCellKey(latitude: $0.center.latitude, longitude: normalizeLngDegrees($0.center.longitude)) }, + revision: { $0.revision }, shape: { ShapeTileRenderer.Shape(CircleTileShape(state: $0)) } ) } diff --git a/Sources/MapConductorCore/overlay/AddProgress.swift b/Sources/MapConductorCore/overlay/AddProgress.swift index 97eb0e8..9828bcb 100644 --- a/Sources/MapConductorCore/overlay/AddProgress.swift +++ b/Sources/MapConductorCore/overlay/AddProgress.swift @@ -57,10 +57,10 @@ struct AddProgressRequest { /// the content is rebuilt on every camera frame. let identity: UnsafeRawPointer? let count: Int - /// The list's ids and state objects. Walks the whole list; called only when - /// the list changed. Holds the list, so its storage cannot be reused while - /// ``identity`` is remembered. - let entries: () -> [(id: String, state: AnyObject)] + /// The list's state objects and a way to their ids. Walks the whole list; + /// called only when the list changed. Holds the list, so its storage + /// cannot be reused while ``identity`` is remembered. + let list: () -> AddProgressList /// The map's tilers for this kind, busy while they take a list or draw /// tiles. Filled in by the collection when it joins the content. var activity: [TileActivity] = [] @@ -79,10 +79,19 @@ struct AddProgressRequest { self.callbacks = callbacks identity = states.withUnsafeBufferPointer { UnsafeRawPointer($0.baseAddress) } count = states.count - entries = { states.map { (id: $0.id, state: $0 as AnyObject) } } + list = { AddProgressList(objects: states.map { $0 as AnyObject }, id: { states[$0].id }) } } } +/// A collection's list as ``AddProgressTracker`` compares it with the last one. +struct AddProgressList { + static let empty = AddProgressList(objects: [], id: { _ in "" }) + + let objects: [AnyObject] + /// The id of `objects[i]`. + let id: (Int) -> String +} + /// The states one collection handed over and how many of them are on the map. @MainActor final class AddProgressTracker { @@ -90,7 +99,7 @@ final class AddProgressTracker { private var callbacks = AddProgressCallbacks() private var last: AddProgressRequest? - private var ids: Set = [] + private var lastList = AddProgressList.empty /// State objects handed over and not on the map yet. private var pending: [ObjectIdentifier: (id: String, state: AnyObject)] = [:] private var total = 0 @@ -130,26 +139,38 @@ final class AddProgressTracker { observe(request.activity) if let last, last.identity == request.identity, last.count == request.count { return } last = request - let entries = request.entries() - let nextIds = Set(entries.map(\.id)) - let hadStates = !ids.isEmpty - let fresh = entries.filter { !ids.contains($0.id) } - ids = nextIds - if entries.isEmpty && !hadStates { return } - if !pending.isEmpty { - let before = pending.count - pending = pending.filter { nextIds.contains($0.value.id) } - total -= before - pending.count + let next = request.list() + let previous = lastList + lastList = next + let hadStates = !previous.objects.isEmpty + if next.objects.isEmpty && !hadStates { return } + let change = Self.compare(next, previous) + // States the list brought: not in the last list, not even as another + // object with the same id (that is an update). + let fresh: [(id: String, state: AnyObject)] = change.added.compactMap { i in + let id = next.id(i) + return change.removedIds.contains(id) ? nil : (id: id, state: next.objects[i]) + } + if !pending.isEmpty, !change.removed.isEmpty { + // A state still coming that left the list no longer counts; one handed + // over again as a new object with the same id is followed by that + // object, which is the one the map reports. + var replacement: [String: AnyObject] = [:] if fresh.isEmpty { - // A state that is still coming may be handed over again as a new - // object with the same id; the map reports that object. - let pendingIds = Set(pending.values.map(\.id)) - var rekeyed: [ObjectIdentifier: (id: String, state: AnyObject)] = [:] - for entry in entries where pendingIds.contains(entry.id) { - rekeyed[ObjectIdentifier(entry.state)] = entry + for i in change.added { replacement[next.id(i)] = next.objects[i] } + } + let before = pending.count + var kept: [ObjectIdentifier: (id: String, state: AnyObject)] = [:] + for (key, entry) in pending { + if !change.removed.contains(key) { + kept[key] = entry + } else if change.addedIds.contains(entry.id) { + let state = replacement[entry.id] ?? entry.state + kept[ObjectIdentifier(state)] = (id: entry.id, state: state) } - pending = rekeyed } + pending = kept + total -= before - pending.count } if !fresh.isEmpty { pending = Dictionary(fresh.map { (ObjectIdentifier($0.state), $0) }, uniquingKeysWith: { a, _ in a }) @@ -165,6 +186,57 @@ final class AddProgressTracker { scheduleSettle() } + /// How `next` differs from `previous`, by identity: which of `next` are new + /// objects (`added`, their indices), which objects of `previous` left + /// (`removed`), and the ids of each. A list changes by a filter or by new + /// states far more often than by reordering, so the two are walked side by + /// side, and nothing is hashed for the states that line up: a filter over + /// 723,800 circles built two sets of ids for 0.5 to 0.8 s on the main thread. + /// + /// The walk can take an object that is further on in `previous` for a new + /// one; that object then shows as removed too, with the same id, and the id + /// comparison below sorts it out. Only the speed depends on the walk. + private static func compare(_ next: AddProgressList, _ previous: AddProgressList) -> ( + added: [Int], addedIds: Set, removed: Set, removedIds: Set + ) { + let old = previous.objects + var matched = [Bool](repeating: false, count: old.count) + var added: [Int] = [] + var cursor = 0 + // Where a scan to the end last found nothing: objects missing there are + // taken as new without scanning again (a run of new states). + var exhaustedAt = -1 + for (i, object) in next.objects.enumerated() { + var k = cursor + let near = min(old.count, cursor + 64) + while k < near, old[k] !== object { k += 1 } + if k == near { + // Past a run of removed states, or a new one. + guard exhaustedAt != cursor else { + added.append(i) + continue + } + while k < old.count, old[k] !== object { k += 1 } + if k == old.count { + exhaustedAt = cursor + added.append(i) + continue + } + } + matched[k] = true + cursor = k + 1 + } + var removed: Set = [] + var removedIds: Set = [] + for (p, object) in old.enumerated() where !matched[p] { + removed.insert(ObjectIdentifier(object)) + // Only a new object can share an id with one that left. + if !added.isEmpty { removedIds.insert(previous.id(p)) } + } + let addedIds = removed.isEmpty ? [] : Set(added.map(next.id)) + return (added, addedIds, removed, removedIds) + } + /// States reached the map (any collection's, any map's: only ours count). func arrived(_ states: [AnyObject]) { if pending.isEmpty { return } @@ -236,8 +308,16 @@ final class AddProgressTracker { guard busy else { return } settleToken += 1 let token = settleToken - DispatchQueue.main.asyncAfter(deadline: .now() + settleDelay) { [weak self] in + let due = DispatchTime.now() + settleDelay + DispatchQueue.main.asyncAfter(deadline: due) { [weak self] in guard let self, self.settleToken == token else { return } + // Late because the main thread was held: the hand-over that held it + // may have queued the work that keeps the map busy, so give that + // work the whole delay to show up. + if DispatchTime.now().uptimeNanoseconds > due.uptimeNanoseconds + 100_000_000 { + self.scheduleSettle() + return + } self.finishIfIdle() } } diff --git a/Sources/MapConductorCore/overlay/OverlayCollector.swift b/Sources/MapConductorCore/overlay/OverlayCollector.swift index 4da5d00..94f368d 100644 --- a/Sources/MapConductorCore/overlay/OverlayCollector.swift +++ b/Sources/MapConductorCore/overlay/OverlayCollector.swift @@ -28,13 +28,43 @@ import Foundation /// android-sdk の `sample(updateDebounce)` と同じ。 @MainActor public final class OverlayCollector { - private var statesById: [String: S] = [:] - private var order: [String] = [] - /// Who is currently reporting mutations. A state reports to at most one - /// collector, so leaving a stale listener on a dropped state would send its - /// writes to a collector that no longer holds it. - private var listening: [String: S] = [:] + /// One state's place in this collector: the listener its signal calls, + /// holding the pass of ``sync(_:)`` the state was last listed in. Telling + /// who joined and who left is then a read per state, not sets and + /// dictionaries of ids: those cost 1.4 s on the main thread for a filter + /// over 723,800 circles on an iPad Pro. + /// Read and written on the main thread only (a write from elsewhere hops there). + private final class Member: StateMutationListener, @unchecked Sendable { + // Unowned, not weak: a weak reference would give every state a side + // table, and its retains and releases would all take the slow path. + unowned let state: S + weak var collector: OverlayCollector? + let owner: ObjectIdentifier? + var pass: UInt32 + + init(state: S, collector: OverlayCollector, pass: UInt32) { + self.state = state + self.collector = collector + owner = ObjectIdentifier(collector) + self.pass = pass + } + + func stateMutated() { + if Thread.isMainThread { + MainActor.assumeIsolated { collector?.memberMutated(self) } + } else { + DispatchQueue.main.async { [self] in collector?.memberMutated(self) } + } + } + } + + /// The current pass of ``sync(_:)``; 0 is none. + private var pass: UInt32 = 0 + /// How many states the current pass holds. + private var memberCount = 0 private var latest: [S] = [] + /// The latest list by id, for ``get(_:)`` and ``values()``; built when asked for. + private var byId: [String: S]? private var membershipHandler: (([S]) -> Void)? private var updateHandler: ((S) -> Void)? @@ -90,49 +120,63 @@ public final class OverlayCollector { // which made every pan run at 5 fps. The same instances in the same order // cannot have changed membership, and their listeners are already in place. if isSameList(states) { return } - let newIds = Set(states.map { $0.id }) - let oldIds = Set(statesById.keys) - var membershipChanged = newIds != oldIds - - var next: [String: S] = [:] + let previous = pass + pass = pass == .max ? 1 : pass + 1 + var stayed = 0 + var joined = false + var count = 0 for state in states { - if let existing = statesById[state.id], existing !== state { - // Same id, new instance → stop listening to the old one and - // treat it as a membership change so the renderer re-adds it. - existing.mutations.listen(nil) - membershipChanged = true + if let member = member(of: state) { + if member.pass == pass { continue } // listed twice + if member.pass == previous { stayed += 1 } else { joined = true } + member.pass = pass + } else { + // A new state, or a new instance of an id (the old one leaves + // below): either way the renderer has to add it. + joined = true + state.mutations.listen(target: Member(state: state, collector: self, pass: pass)) } - next[state.id] = state + count += 1 } - statesById = next - order = states.map { $0.id } - latest = states - - for id in oldIds.subtracting(newIds) { - listening.removeValue(forKey: id)?.mutations.listen(nil) + // A state reports to at most one collector, so leaving a stale listener + // on a dropped state would send its writes to a collector that no longer + // holds it. + for state in latest { + if let member = member(of: state), member.pass == previous { state.mutations.listen(target: nil) } } + let membershipChanged = joined || stayed != memberCount + memberCount = count + latest = states + byId = nil if membershipChanged { scheduleMembership() } + } - for state in listening.values where next[state.id] !== state { - state.mutations.listen(nil) - } - listening = next - for state in states { - // The state is found again by id rather than held weakly. A weak - // reference gives the object a side table, and from then on every - // retain and release of it takes the slow path: with 100,000 polygons - // rebuilt into the map content on every camera frame, that was a - // third of a pan's main-thread time on the iPad. - let id = state.id - let identity = ObjectIdentifier(state) - state.mutations.listen { [weak self] in - guard let self, let current = self.statesById[id], ObjectIdentifier(current) == identity else { return } - self.scheduleUpdate(current) - } - } + /// The state's membership here, if it listens to this collector. + private func member(of state: S) -> Member? { + guard let target = state.mutations.target, target.owner == ObjectIdentifier(self) else { return nil } + return unsafeDowncast(target, to: Member.self) + } + + /// In the latest list. + private func holds(_ state: S) -> Bool { + member(of: state)?.pass == pass + } + + private func memberMutated(_ member: Member) { + guard member.pass == pass else { return } + scheduleUpdate(member.state) + } + + private func statesById() -> [String: S] { + if let byId { return byId } + var map: [String: S] = [:] + map.reserveCapacity(latest.count) + for state in latest { map[state.id] = state } + byId = map + return map } /// The same instances in the same order. `S` is a class, so each element is @@ -200,7 +244,7 @@ public final class OverlayCollector { self.emitMembershipIfPending() let batch = self.pendingUpdates self.pendingUpdates.removeAll() - for (id, state) in batch where self.statesById[id] === state { + for state in batch.values where self.holds(state) { self.updateHandler?(state) } } @@ -216,18 +260,29 @@ public final class OverlayCollector { /// Current states in declaration order. public func values() -> [S] { - order.compactMap { statesById[$0] } + let byId = statesById() + return latest.compactMap { byId[$0.id] } } public func get(_ id: String) -> S? { - statesById[id] + statesById()[id] + } + + deinit { + // A listener of ours left on a state would keep this collector's + // identity, which a new collector could be given. + for state in latest { + if let target = state.mutations.target, target.owner == ObjectIdentifier(self) { + state.mutations.listen(target: nil) + } + } } public func clear() { - listening.values.forEach { $0.mutations.listen(nil) } - listening.removeAll() - statesById.removeAll() - order.removeAll() + for state in latest where holds(state) { state.mutations.listen(target: nil) } + pass = pass == .max ? 1 : pass + 1 + memberCount = 0 + byId = nil latest.removeAll() updateFlushTask?.cancel() updateFlushTask = nil diff --git a/Sources/MapConductorCore/overlay/StateMutationSignal.swift b/Sources/MapConductorCore/overlay/StateMutationSignal.swift index 207ed3d..e30aa1f 100644 --- a/Sources/MapConductorCore/overlay/StateMutationSignal.swift +++ b/Sources/MapConductorCore/overlay/StateMutationSignal.swift @@ -18,7 +18,9 @@ import Foundation /// there from the opposite direction — one observer reading every state, rather /// than one subscription per state. public final class StateMutationSignal { - private var listener: (() -> Void)? + /// An object rather than a closure: ``OverlayCollector`` keeps what it + /// knows of the state on it, and finds it again on the next sync. + private(set) var target: StateMutationListener? public init() {} @@ -27,11 +29,30 @@ public final class StateMutationSignal { /// A state belongs to at most one collector, so there is one listener /// rather than a list. public func listen(_ listener: (() -> Void)?) { - self.listener = listener + target = listener.map(ClosureListener.init) + } + + func listen(target: StateMutationListener?) { + self.target = target } /// Reports a write. public func notifyMutated() { - listener?() + target?.stateMutated() } } + +/// Hears the writes to one state (``StateMutationSignal``). +protocol StateMutationListener: AnyObject { + /// The collector this listener belongs to, so that it can find its own + /// listeners without a cast; nil for a plain closure. + var owner: ObjectIdentifier? { get } + func stateMutated() +} + +private final class ClosureListener: StateMutationListener { + private let body: () -> Void + init(_ body: @escaping () -> Void) { self.body = body } + var owner: ObjectIdentifier? { nil } + func stateMutated() { body() } +} diff --git a/Sources/MapConductorCore/polygon/PolygonController.swift b/Sources/MapConductorCore/polygon/PolygonController.swift index 788bc3b..3987059 100644 --- a/Sources/MapConductorCore/polygon/PolygonController.swift +++ b/Sources/MapConductorCore/polygon/PolygonController.swift @@ -43,6 +43,12 @@ where Renderer.ActualPolygon == ActualPolygon { open func add(data: [PolygonState]) async { if polygonManager.isDestroyed { return } + // Busy from here, not only while the tiler routes: the list waits for + // the controller and its bookkeeping, and the collection's onAddEnd + // must not come in that gap. Once the map has the tiles, drawing them + // is what keeps the tiler busy. + let hold = TileActivityHold(tiler?.activity) + defer { hold.release() } let landed: [PolygonState] = await semaphore.withPermit { if polygonManager.isDestroyed { return [] } // The app's list: anything it no longer has can be added again later. @@ -52,6 +58,7 @@ where Renderer.ActualPolygon == ActualPolygon { // `route` returns once the tile layer's URL template has been handed // to the map (the provider's `onTemplate` awaits its raster upsert). let tiled = await tiler?.route(data) ?? [] + hold.release() tiledIds = tiled if !tiled.isEmpty, let tiler { // Tiled polygons are on the map now. Report them before the diff --git a/Sources/MapConductorCore/polyline/PolylineController.swift b/Sources/MapConductorCore/polyline/PolylineController.swift index 06aaabc..d2c2251 100644 --- a/Sources/MapConductorCore/polyline/PolylineController.swift +++ b/Sources/MapConductorCore/polyline/PolylineController.swift @@ -55,6 +55,12 @@ where Renderer.ActualPolyline == ActualPolyline { open func add(data: [PolylineState]) async { if polylineManager.isDestroyed { return } + // Busy from here, not only while the tiler routes: the list waits for + // the controller and its bookkeeping, and the collection's onAddEnd + // must not come in that gap. Once the map has the tiles, drawing them + // is what keeps the tiler busy. + let hold = TileActivityHold(tiler?.activity) + defer { hold.release() } let landed: [PolylineState] = await semaphore.withPermit { if polylineManager.isDestroyed { return [] } // The app's list: anything it no longer has can be added again later. @@ -64,6 +70,7 @@ where Renderer.ActualPolyline == ActualPolyline { // `route` returns once the tile layer's URL template has been handed // to the map (the provider's `onTemplate` awaits its raster upsert). let tiled = await tiler?.route(data) ?? [] + hold.release() if polylineManager.isDestroyed { return [] } tiledIds = tiled if !tiled.isEmpty, let tiler { diff --git a/Sources/MapConductorCore/shapetile/ShapeTileRenderer.swift b/Sources/MapConductorCore/shapetile/ShapeTileRenderer.swift index 651745b..dffa72d 100644 --- a/Sources/MapConductorCore/shapetile/ShapeTileRenderer.swift +++ b/Sources/MapConductorCore/shapetile/ShapeTileRenderer.swift @@ -336,10 +336,27 @@ final class ShapeTileRenderer: TileProvider { } } - func reuseCircles(_ states: [CircleState], slots: [Int], active: [Bool]) -> Bool { + /// `unchanged[i]` says the circle in `slots[i]` is known to be `states[i]` + /// as it was registered, so it is not compared with the file. + func reuseCircles(_ states: [CircleState], slots: [Int], active: [Bool], unchanged: [Bool]) -> Bool { lock.lock() defer { lock.unlock() } - for (state, slot) in zip(states, slots) where !data.shapes.matches(state, at: slot) { return false } + for (i, (state, slot)) in zip(states, slots).enumerated() where !unchanged[i] && !data.shapes.matches(state, at: slot) { + return false + } + guard data.shapes.isFileBacked else { return false } + data.shapes.active = active + data.generation += 1 + dropAllTilesLocked() + return true + } + + /// Draws only the circles of `active`, each still the one registered in its + /// slot (the caller knows them unchanged). + func reuseCircleSlots(active: [Bool]) -> Bool { + lock.lock() + defer { lock.unlock() } + guard data.shapes.isFileBacked, data.shapes.count == active.count else { return false } data.shapes.active = active data.generation += 1 dropAllTilesLocked() diff --git a/Sources/MapConductorCore/shapetile/ShapeTileSet.swift b/Sources/MapConductorCore/shapetile/ShapeTileSet.swift index 65f7001..9ea7b37 100644 --- a/Sources/MapConductorCore/shapetile/ShapeTileSet.swift +++ b/Sources/MapConductorCore/shapetile/ShapeTileSet.swift @@ -12,7 +12,7 @@ import UIKit renumbering the rest, and the teardown. What a state looks like in a tile is the caller's: it passes the conversion to ``ShapeTileRenderer/Shape``. */ -final class ShapeTileSet { +final class ShapeTileSet { /// In-place changes arriving within this window become one new template. static var updateCoalesceMs: Int { 16 } @@ -33,14 +33,33 @@ final class ShapeTileSet { private let fileBackedCircles: Bool /// Where a state is, for storing a large set by place (see ``CircleGeometryFile``). private let spatialKeyOf: ((State) -> Int)? + /// A state's revision (it moves on every write to the state), for telling + /// an unchanged state by identity without reading its properties. Only + /// circles give one. + private let revisionOf: ((State) -> UInt64)? private let lock = NSLock() private var indexById: [String: Int] = [:] + /// The state registered in each slot; nil once it was removed. A slot left + /// out by ``reuse(_:sortedZ:slots:registration:)`` keeps its state, so that + /// the state can be matched to it again: ``sourceActive`` says which are drawn. private var sourceStates: [State?] = [] + /// Which slots are drawn; empty when all are. + private var sourceActive: [Bool] = [] private var sourceZ: [Int] = [] + /// Each slot's state's revision when it was registered, or + /// ``unknownRevision`` once the slot was changed or removed. Empty without + /// ``revisionOf``. + private var sourceRevisions: [UInt64] = [] + private static var unknownRevision: UInt64 { .max } + + /// Called with ``lock`` held. + private func isDrawnLocked(_ slot: Int) -> Bool { + sourceActive.isEmpty || sourceActive[slot] + } func source(_ id: String) -> State? { lock.lock(); defer { lock.unlock() } - guard let index = indexById[id] else { return nil } + guard let index = indexById[id], isDrawnLocked(index) else { return nil } return sourceStates[index] } @@ -53,7 +72,7 @@ final class ShapeTileSet { defer { lock.unlock() } var states: [State] = [] var seen = Set() - for slot in slots where Int(slot) < sourceStates.count { + for slot in slots where Int(slot) < sourceStates.count && isDrawnLocked(Int(slot)) { guard let state = sourceStates[Int(slot)] else { continue } if seen.insert(idOf(state)).inserted { states.append(state) } } @@ -64,8 +83,10 @@ final class ShapeTileSet { } func visitSources(_ action: (State) -> Void) { - lock.lock(); let sources = sourceStates; lock.unlock() - for state in sources { if let state { action(state) } } + lock.lock(); let sources = sourceStates; let active = sourceActive; lock.unlock() + for (slot, state) in sources.enumerated() where active.isEmpty || active[slot] { + if let state { action(state) } + } } private var version = 0 private var registration = 0 @@ -81,6 +102,7 @@ final class ShapeTileSet { zIndex: @escaping (State) -> Int, fileBackedCircles: Bool = false, spatialKey: ((State) -> Int)? = nil, + revision: ((State) -> UInt64)? = nil, shape: @escaping (State) -> ShapeTileRenderer.Shape? ) { routeId = "\(routePrefix)-\(UUID().uuidString.prefix(8).lowercased())" @@ -90,6 +112,7 @@ final class ShapeTileSet { shapeOf = shape self.fileBackedCircles = fileBackedCircles spatialKeyOf = spatialKey + revisionOf = revision } deinit { @@ -99,22 +122,33 @@ final class ShapeTileSet { /// Registers `states` (replacing whatever was registered) and returns the URL /// template of their tiles. The states are read and projected off the /// calling thread. - func setStates(_ states: [State]) async -> String? { + /// + /// `slots` (from ``registeredSlots(_:)``, one per state) lets the states + /// already registered and unchanged skip being read again. + func setStates(_ states: [State], slots: [Int32]? = nil) async -> String? { cancelPending() let registration = nextRegistration() let renderer = self.renderer let idOf = self.idOf, zIndexOf = self.zIndexOf, shapeOf = self.shapeOf let fileBackedCircles = self.fileBackedCircles let spatialKeyOf = self.spatialKeyOf + let revisionOf = self.revisionOf let work = Task.detached(priority: .userInitiated) { () -> Bool in + // A filter over the registered states: all of them unchanged and in + // the registered order, which is already the draw order. + if fileBackedCircles, let slots, self.reuse(states, slots: slots, registration: registration) { return true } // Draw order is list order: zIndex, then the order given. Each zIndex // is read once: a state's properties are not free to read, and a // sort reads them n log n times. - let zIndex = states.map(zIndexOf) - let order = states.indices.sorted { zIndex[$0] != zIndex[$1] ? zIndex[$0] < zIndex[$1] : $0 < $1 } - let ordered = order.map { states[$0] } - let sortedZ = fileBackedCircles ? order.map { zIndex[$0] } : [] - if fileBackedCircles, self.reuse(ordered, sortedZ: sortedZ, registration: registration) { return true } + let zIndex = self.zIndices(states, slots: slots, zIndexOf: zIndexOf) + // Usually already in order (the app keeps its own), and then the + // sort is not needed. + let inOrder = zIndex.indices.dropFirst().allSatisfy { zIndex[$0 - 1] <= zIndex[$0] } + let order = inOrder ? Array(states.indices) : states.indices.sorted { zIndex[$0] != zIndex[$1] ? zIndex[$0] < zIndex[$1] : $0 < $1 } + let ordered = inOrder ? states : order.map { states[$0] } + let sortedZ = fileBackedCircles ? (inOrder ? zIndex : order.map { zIndex[$0] }) : [] + let orderedSlots = slots.map { slots in inOrder ? slots : order.map { slots[$0] } } + if fileBackedCircles, self.reuse(ordered, sortedZ: sortedZ, slots: orderedSlots, registration: registration) { return true } var indexById: [String: Int] = [:] indexById.reserveCapacity(ordered.count) for (i, state) in ordered.enumerated() { @@ -137,7 +171,7 @@ final class ShapeTileSet { let install = renderer.prepareStorage(storage) return self.publish( registration: registration, indexById: indexById, sources: ordered.map { Optional($0) }, - sortedZ: sortedZ, install: install + sortedZ: sortedZ, revisions: revisionOf.map { ordered.map($0) } ?? [], install: install ) } let published = await withTaskCancellationHandler(operation: { await work.value }, onCancel: { work.cancel() }) @@ -152,7 +186,8 @@ final class ShapeTileSet { } private func publish( - registration: Int, indexById: [String: Int], sources: [State?], sortedZ: [Int], install: () -> Void + registration: Int, indexById: [String: Int], sources: [State?], sortedZ: [Int], revisions: [UInt64], + install: () -> Void ) -> Bool { lock.lock() defer { lock.unlock() } @@ -162,13 +197,78 @@ final class ShapeTileSet { server.register(routeId: routeId, provider: renderer) self.indexById = indexById self.sourceStates = sources + self.sourceActive = [] self.sourceZ = sortedZ + self.sourceRevisions = revisions registered = true version += 1 return true } - private func reuse(_ ordered: [State], sortedZ: [Int], registration: Int) -> Bool { + /// For each of `states`, the slot it is registered in when it is the very + /// object registered there and has not been written to since, or -1. Nil + /// when there is nothing to match against. Reads no state's properties: a + /// filter over hundreds of thousands of circles spent seconds reading them + /// to find that nothing had changed. + func registeredSlots(_ states: [State]) -> [Int32]? { + guard let revisionOf else { return nil } + lock.lock() + defer { lock.unlock() } + guard registered, sourceStates.count > 2_000, sourceRevisions.count == sourceStates.count else { return nil } + let count = sourceStates.count + var slots = [Int32](repeating: -1, count: states.count) + // The list usually keeps the registered order, so the slot is found a + // little after the last one; past that, by id. + var next = 0 + for (i, state) in states.enumerated() { + var slot = -1 + var k = next + let end = min(count, next + 64) + while k < end { + if sourceStates[k] === state { slot = k; break } + k += 1 + } + if slot < 0, let byId = indexById[idOf(state)], sourceStates[byId] === state { slot = byId } + guard slot >= 0 else { continue } + next = slot + 1 + if sourceRevisions[slot] == revisionOf(state) { slots[i] = Int32(slot) } + } + return slots + } + + /// Each state's zIndex, from its slot when it is registered and unchanged. + private func zIndices(_ states: [State], slots: [Int32]?, zIndexOf: (State) -> Int) -> [Int] { + guard let slots else { return states.map(zIndexOf) } + lock.lock() + let z = sourceZ + lock.unlock() + return states.indices.map { i in + let slot = Int(slots[i]) + return slot >= 0 && slot < z.count ? z[slot] : zIndexOf(states[i]) + } + } + + /// Shows `states` (all registered and unchanged, `slots` from + /// ``registeredSlots(_:)``) by switching slots on and off, when they are in + /// the registered order. + private func reuse(_ states: [State], slots: [Int32], registration: Int) -> Bool { + lock.lock() + defer { lock.unlock() } + guard registered, sourceStates.count > 2_000, self.registration == registration, !Task.isCancelled else { return false } + var previous: Int32 = -1 + for slot in slots { + guard slot > previous else { return false } + previous = slot + } + var active = [Bool](repeating: false, count: sourceStates.count) + for slot in slots { active[Int(slot)] = true } + guard renderer.reuseCircleSlots(active: active) else { return false } + sourceActive = active + version += 1 + return true + } + + private func reuse(_ ordered: [State], sortedZ: [Int], slots knownSlots: [Int32]?, registration: Int) -> Bool { guard let circles = ordered as? [CircleState] else { return false } lock.lock() defer { lock.unlock() } @@ -176,15 +276,28 @@ final class ShapeTileSet { var active = [Bool](repeating: false, count: sourceStates.count) var slots: [Int] = [] slots.reserveCapacity(ordered.count) + // Slots whose circle is known unchanged need no comparing with the file. + var unchanged = [Bool](repeating: false, count: ordered.count) for (i, state) in ordered.enumerated() { - guard let slot = indexById[idOf(state)], slot > (slots.last ?? -1), - sortedZ[i] == sourceZ[slot] else { return false } + let known = knownSlots.map { Int($0[i]) } ?? -1 + let slot: Int + if known >= 0 { + slot = known + unchanged[i] = true + } else { + guard let byId = indexById[idOf(state)] else { return false } + slot = byId + } + guard slot > (slots.last ?? -1), sortedZ[i] == sourceZ[slot] else { return false } slots.append(slot) active[slot] = true } - guard renderer.reuseCircles(circles, slots: slots, active: active) else { return false } - for i in sourceStates.indices where !active[i] { sourceStates[i] = nil } - for (state, slot) in zip(ordered, slots) { sourceStates[slot] = state } + guard renderer.reuseCircles(circles, slots: slots, active: active, unchanged: unchanged) else { return false } + for (i, (state, slot)) in zip(ordered, slots).enumerated() where !unchanged[i] { + sourceStates[slot] = state + if !sourceRevisions.isEmpty { sourceRevisions[slot] = revisionOf?(state) ?? Self.unknownRevision } + } + sourceActive = active version += 1 return true } @@ -192,7 +305,7 @@ final class ShapeTileSet { func has(_ id: String) -> Bool { lock.lock() defer { lock.unlock() } - return indexById[id].map { sourceStates[$0] != nil } ?? false + return indexById[id].map { sourceStates[$0] != nil && isDrawnLocked($0) } ?? false } /// One state changed in place. A change of zIndex reorders the list and @@ -205,7 +318,9 @@ final class ShapeTileSet { return } pending[id] = state - sourceStates[indexById[id]!] = state + let slot = indexById[id]! + sourceStates[slot] = state + if !sourceRevisions.isEmpty { sourceRevisions[slot] = Self.unknownRevision } scheduleFlushLocked() } @@ -220,6 +335,7 @@ final class ShapeTileSet { } pending[id] = nil sourceStates[index] = nil + if !sourceRevisions.isEmpty { sourceRevisions[index] = Self.unknownRevision } pendingRemovals.append(index) scheduleFlushLocked() } @@ -254,7 +370,9 @@ final class ShapeTileSet { registered = false indexById.removeAll() sourceStates.removeAll() + sourceActive.removeAll() sourceZ.removeAll() + sourceRevisions.removeAll() lock.unlock() if wasRegistered { TileServerRegistry.get().unregister(routeId: routeId) } renderer.clear() diff --git a/Sources/MapConductorCore/shapetile/ShapeTileStorage.swift b/Sources/MapConductorCore/shapetile/ShapeTileStorage.swift index 6b0768e..34a411c 100644 --- a/Sources/MapConductorCore/shapetile/ShapeTileStorage.swift +++ b/Sources/MapConductorCore/shapetile/ShapeTileStorage.swift @@ -18,6 +18,9 @@ struct ShapeTileStorage { var count: Int { file?.count ?? memory.count } + /// Circles kept in a ``CircleGeometryFile``: the storage ``ShapeTileRenderer/reuseCircles(_:slots:active:unchanged:)`` reuses. + var isFileBacked: Bool { file != nil } + /// Stored position -> index, when the file is stored by place (see /// ``CircleGeometryFile``). A pass over every shape walks this order. var storageOrder: [Int32]? { file?.indexAt } diff --git a/Sources/MapConductorCore/shapetile/ShapeTiler.swift b/Sources/MapConductorCore/shapetile/ShapeTiler.swift index fec4724..949ec28 100644 --- a/Sources/MapConductorCore/shapetile/ShapeTiler.swift +++ b/Sources/MapConductorCore/shapetile/ShapeTiler.swift @@ -10,7 +10,7 @@ import Foundation ``refreshIfServerMoved()`` is called from the map's content update on the main thread) is read and written under ``lock``. */ -final class ShapeTiler { +final class ShapeTiler { var onTemplate: ((String?, Int) async -> Void)? let tileSet: ShapeTileSet @@ -50,24 +50,32 @@ final class ShapeTiler { let activity = tileSet.renderer.activity activity.begin() defer { activity.end() } + // A state registered and unchanged since could be tiled then and still can. + let registered = tileSet.registeredSlots(data) var tileable: [State] = [] tileable.reserveCapacity(data.count) + var slots: [Int32] = [] + if registered != nil { slots.reserveCapacity(data.count) } var vertices = 0 - for state in data where canTile(state) { + for (i, state) in data.enumerated() { + let slot = registered?[i] ?? -1 + guard slot >= 0 || canTile(state) else { continue } tileable.append(state) + if registered != nil { slots.append(slot) } vertices += vertexCountOf(state) } let useTiles = shouldUseTiles(tileable.count, vertices) - let template = useTiles ? await tileSet.setStates(tileable) : nil + let template = useTiles ? await tileSet.setStates(tileable, slots: registered == nil ? nil : slots) : nil if let template { - let ids = Set(tileable.map(idOf)) let baseUrl = TileServerRegistry.get().baseUrl withLock { - tiled = ids states = tileable shownBaseUrl = baseUrl } + // The map starts asking for tiles here; the ids are only bookkeeping. await onTemplate?(template, tileSet.tileSize) + let ids = Set(tileable.map(idOf)) + withLock { tiled = ids } return ids } await clear() diff --git a/Sources/MapConductorCore/shapetile/TileActivity.swift b/Sources/MapConductorCore/shapetile/TileActivity.swift index 164004a..11bbdad 100644 --- a/Sources/MapConductorCore/shapetile/TileActivity.swift +++ b/Sources/MapConductorCore/shapetile/TileActivity.swift @@ -83,3 +83,21 @@ func overlayTileActivity(_ registry: MapServiceRegistry, kind: String) -> [TileA return provider.tileActivity } } + +/// Keeps a ``TileActivity`` busy from a controller's `add` until its tiler has +/// handed the map the new tiles (``release()``), or until the hold goes away. +final class TileActivityHold { + private var activity: TileActivity? + + init(_ activity: TileActivity?) { + activity?.begin() + self.activity = activity + } + + func release() { + activity?.end() + activity = nil + } + + deinit { release() } +} diff --git a/Tests/MapConductorCoreTests/AddProgressTests.swift b/Tests/MapConductorCoreTests/AddProgressTests.swift index 74cdf7d..8d0b2ce 100644 --- a/Tests/MapConductorCoreTests/AddProgressTests.swift +++ b/Tests/MapConductorCoreTests/AddProgressTests.swift @@ -257,4 +257,69 @@ final class AddProgressTests: XCTestCase { Circles([circle("a")], onAddEnd: { _ in }).append(to: &content) XCTAssertEqual(content.addProgressRequests.count, 1) } + + // MARK: - Comparing lists + + func testAFilterThatDropsALongRunStartsAndEndsWithZeroAndTheRunComesBackAsNew() { + let tracker = tracker() + let states = (0..<300).map { circle("s\($0)") } + tracker.sync(request(states)) + drain() + tracker.arrived(states) + settleDown() + calls.removeAll() + tracker.sync(request(Array(states[..<50]) + Array(states[250...]))) + settleDown() + XCTAssertEqual(calls, ["start 0", "end 0"]) + calls.removeAll() + tracker.sync(request(states)) + drain() + XCTAssertEqual(calls, ["start 200"]) + } + + func testARunOfNewStatesInTheMiddleIsCounted() { + let tracker = tracker() + let states = (0..<300).map { circle("s\($0)") } + tracker.sync(request(states)) + drain() + tracker.arrived(states) + settleDown() + calls.removeAll() + let inserted = (0..<120).map { circle("n\($0)") } + tracker.sync(request(Array(states[..<100]) + inserted + Array(states[100...]))) + drain() + XCTAssertEqual(calls, ["start 120"]) + tracker.arrived(inserted) + settleDown() + XCTAssertEqual(Array(calls.suffix(2)), ["progress 120/120", "end 120"]) + } + + func testTheSameStatesInAnotherOrderAreNotNew() { + let tracker = tracker() + let states = (0..<300).map { circle("s\($0)") } + tracker.sync(request(states)) + drain() + tracker.arrived(states) + settleDown() + calls.removeAll() + tracker.sync(request(states.reversed())) + settleDown() + XCTAssertEqual(calls, ["start 0", "end 0"]) + } + + /// The settle timer comes late when the main thread was held (by the + /// hand-over itself, typically); it then waits once more instead of ending + /// before the work the hand-over queued has shown up. + func testALateSettleTimerWaitsOnceMore() { + let tracker = tracker() + let a = circle("a") + tracker.sync(request([a])) + drain() + tracker.arrived([a]) + Thread.sleep(forTimeInterval: settle + 0.2) + drain() + XCTAssertEqual(calls, ["start 1"], "late: waits once more") + settleDown() + XCTAssertEqual(calls, ["start 1", "progress 1/1", "end 1"]) + } } diff --git a/Tests/MapConductorCoreTests/CircleTilingTests.swift b/Tests/MapConductorCoreTests/CircleTilingTests.swift index f16ff2e..ac09275 100644 --- a/Tests/MapConductorCoreTests/CircleTilingTests.swift +++ b/Tests/MapConductorCoreTests/CircleTilingTests.swift @@ -415,4 +415,82 @@ final class CircleTilingTests: XCTestCase { XCTAssertEqual(controller.resolveTap(position: north(12))?.kind, .circle) await controller.clear() } + + // MARK: - A filter over registered circles + + /// The slots of the tile set the circles are in, as the next route would find them. + private func slots(_ tiler: CircleTiler, _ states: [CircleState]) -> [Int32] { + tiler.tileSet.core.registeredSlots(states) ?? [] + } + + /// A filter switches slots off instead of registering the circles again, and + /// the circles left out are neither drawn nor found; bringing them back + /// switches them on again. With zIndex in list order, as the antenna sample + /// has it, nothing about the circles is read. + func testAFilterSwitchesSlotsAndBringsTheCirclesBack() async { + let tiler = CircleTiler() + var templates: [String?] = [] + tiler.onTemplate = { template, _ in templates.append(template) } + defer { tiler.dispose() } + let all = circles(4_500) + for (i, state) in all.enumerated() { state.zIndex = i } + _ = await tiler.route(all) + XCTAssertEqual(slots(tiler, all), all.indices.map(Int32.init)) + let kept = all.enumerated().filter { $0.offset % 3 != 0 }.map(\.element) + let ids = await tiler.route(kept) + XCTAssertEqual(ids, Set(kept.map(\.id))) + // Still in the slots they were registered in: nothing was registered again. + XCTAssertEqual(slots(tiler, kept), all.indices.filter { $0 % 3 != 0 }.map(Int32.init)) + XCTAssertNil(tiler.state("c0")) + XCTAssertTrue(tiler.state("c1") === all[1]) + XCTAssertNotEqual(templates[templates.count - 1], templates[templates.count - 2], "a new version, so the map asks again") + let manager = CircleManager() + manager.setTiledSource(tiler) + XCTAssertNil(manager.find(position: all[0].center)) + XCTAssertTrue(manager.find(position: all[1].center)?.state === all[1]) + + _ = await tiler.route(all) + XCTAssertEqual(slots(tiler, all), all.indices.map(Int32.init)) + XCTAssertTrue(tiler.state("c0") === all[0]) + XCTAssertTrue(manager.find(position: all[0].center)?.state === all[0]) + manager.clear() + } + + /// A circle written to since it was registered is read again, so its tiles + /// and taps follow the new values. + func testACircleWrittenToBetweenFiltersIsReadAgain() async throws { + let tiler = CircleTiler() + tiler.onTemplate = { _, _ in } + defer { tiler.dispose() } + let all = circles(3_000) + _ = await tiler.route(all) + all[5].radiusMeters = 30 + XCTAssertEqual(slots(tiler, all)[5], -1, "written to: not taken as unchanged") + _ = await tiler.route(Array(all.dropFirst())) + // 25 m north: outside the radius it was registered with, inside the new + // one. The renderer draws what its storage holds there. + let center = all[5].center + let north = ShapeTileRenderer.toMercator([center.latitude + 25 / 111_320, center.longitude]) + let drawnThere = tiler.tileSet.core.renderer.candidates(minX: north[0], minY: north[1], maxX: north[0], maxY: north[1]) + let slot = try XCTUnwrap(slots(tiler, [all[5]]).first) + XCTAssertGreaterThanOrEqual(slot, 0) + XCTAssertTrue(drawnThere.contains(slot), "drawn with the new radius") + } + + /// New objects with the same ids and values are compared with what is + /// drawn, and the tiles are kept (the circles stay in their slots). + func testNewObjectsWithTheSameValuesKeepTheTiles() async { + let tiler = CircleTiler() + tiler.onTemplate = { _, _ in } + defer { tiler.dispose() } + let all = circles(3_000) + _ = await tiler.route(all) + let registered = slots(tiler, all) + let again = circles(3_000) + XCTAssertTrue(slots(tiler, again).allSatisfy { $0 == -1 }, "other objects: not taken as registered") + _ = await tiler.route(Array(again.prefix(2_500))) + XCTAssertEqual(slots(tiler, Array(again.prefix(2_500))), Array(registered.prefix(2_500))) + XCTAssertTrue(tiler.state("c7") === again[7]) + XCTAssertNil(tiler.state("c2600")) + } } diff --git a/Tests/MapConductorCoreTests/OverlayCollectorDebounceTests.swift b/Tests/MapConductorCoreTests/OverlayCollectorDebounceTests.swift index b2be165..ce3972f 100644 --- a/Tests/MapConductorCoreTests/OverlayCollectorDebounceTests.swift +++ b/Tests/MapConductorCoreTests/OverlayCollectorDebounceTests.swift @@ -186,4 +186,99 @@ final class OverlayCollectorDebounceTests: XCTestCase { XCTAssertEqual(updates, 0) XCTAssertTrue(collector.values().isEmpty) } + + // MARK: - Who belongs + + /// A state that left stops reporting its writes; one that joined starts. + func testAStateThatLeftStopsReportingAndOneThatJoinedStarts() async { + let collector = OverlayCollector() + var emissions: [[String]] = [] + var updates: [String] = [] + collector.onMembershipChange { emissions.append($0.map(\.id)) } + collector.onStateChange { updates.append($0.id) } + let (a, b, c) = (TestState(id: "a"), TestState(id: "b"), TestState(id: "c")) + collector.sync([a, b]) + await waitForWindow() + collector.sync([b, c]) + await waitForWindow() + XCTAssertEqual(emissions, [["a", "b"], ["b", "c"]]) + a.mutate() + b.mutate() + c.mutate() + await waitForWindow() + XCTAssertEqual(Set(updates), ["b", "c"]) + XCTAssertNil(collector.get("a")) + XCTAssertTrue(collector.get("c") === c) + } + + /// Another object with an id already held is a membership change, and only + /// the new object reports. + func testANewObjectForAnIdIsAMembershipChange() async { + let collector = OverlayCollector() + var emissions = 0 + var updates: [ObjectIdentifier] = [] + collector.onMembershipChange { _ in emissions += 1 } + collector.onStateChange { updates.append(ObjectIdentifier($0)) } + let a = TestState(id: "a") + collector.sync([a]) + await waitForWindow() + let a2 = TestState(id: "a") + collector.sync([a2]) + await waitForWindow() + XCTAssertEqual(emissions, 2) + a.mutate() + a2.mutate() + await waitForWindow() + XCTAssertEqual(updates, [ObjectIdentifier(a2)]) + } + + /// The same states in another order are not a membership change. + func testReorderingIsNotAMembershipChange() async { + let collector = OverlayCollector() + var emissions = 0 + collector.onMembershipChange { _ in emissions += 1 } + let states = (0..<5).map { TestState(id: "s\($0)") } + collector.sync(states) + await waitForWindow() + collector.sync(states.reversed()) + await waitForWindow() + XCTAssertEqual(emissions, 1) + XCTAssertEqual(collector.values().map(\.id), ["s4", "s3", "s2", "s1", "s0"]) + } + + /// A state handed to another collector reports there, and the first one + /// still counts it as a member it lost. + func testAStateTakenByAnotherCollectorReportsThere() async { + let first = OverlayCollector() + let second = OverlayCollector() + var firstUpdates = 0, secondUpdates = 0 + var firstEmissions = 0 + first.onStateChange { _ in firstUpdates += 1 } + second.onStateChange { _ in secondUpdates += 1 } + first.onMembershipChange { _ in firstEmissions += 1 } + let a = TestState(id: "a") + first.sync([a]) + second.sync([a]) + await waitForWindow() + a.mutate() + await waitForWindow() + XCTAssertEqual(firstUpdates, 0) + XCTAssertEqual(secondUpdates, 1) + first.sync([a, TestState(id: "b")]) + await waitForWindow() + XCTAssertEqual(firstEmissions, 2) + } + + /// A write from another thread is delivered on the main thread. + func testAWriteFromAnotherThreadArrivesOnTheMainThread() async { + let collector = OverlayCollector() + var onMain: [Bool] = [] + collector.onStateChange { _ in onMain.append(Thread.isMainThread) } + let a = TestState(id: "a") + collector.sync([a]) + await waitForWindow() + await Task.detached { a.mutate() }.value + await waitForWindow() + XCTAssertEqual(onMain, [true]) + } }