The resting layout is built once per snapshot — retargets stop rebuilding it per mouse sample
RestingLayoutCache (session-scoped, @ObservationIgnored on DragSession, cleared at begin and end) holds each lane's resting layout as ids + heights — never [Card] — keyed on the hovered board's applied snapshotGeneration, the registry's new heightsGeneration, the lane, and the operation-following hidden set. The grid stays event-time on purpose (autoscroll moves a lane's origin with the snapshot standing still), so re-grounding rule 1 holds exactly: nothing survives a reload, only the per-sample repetition goes. The steady-state cost of a hover is now the containment scan plus four stores and a divide. Drag-perf suspect #3, card b9f48fd1. Claude-Session: https://claude.ai/code/session_01CqjXB7ASoWtbyoGod68k97
This commit is contained in:
@@ -44,6 +44,24 @@ final class LaneDropRegistry {
|
||||
var columns: Int
|
||||
/// Spacing between columns and between stacked cards.
|
||||
var spacing: CGFloat
|
||||
|
||||
/// The masonry arithmetic this grid is drawn with — `MasonryLayout`'s own expression, so
|
||||
/// the zones and the drawn grid cannot disagree (`MasonryPlacement`).
|
||||
///
|
||||
/// **Derived at event time and deliberately never cached**, unlike the resting layout it is
|
||||
/// applied to (`RestingLayoutCache`): the grid is registered from inside the lane's scroll
|
||||
/// view, so its origin moves whenever the lane scrolls — which the autoscroll driver does on
|
||||
/// purpose, mid-drag, with the snapshot standing perfectly still. It is also free: four
|
||||
/// stores and a divide, no allocation.
|
||||
var placement: MasonryPlacement {
|
||||
MasonryPlacement(
|
||||
columnCount: columns,
|
||||
columnWidth: MasonryPlacement.columnWidth(
|
||||
totalWidth: frame.width, columnCount: columns, spacing: spacing),
|
||||
spacing: spacing,
|
||||
origin: frame.origin
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// The height a card with no registered measurement is assumed to have — a lane whose faces have
|
||||
@@ -68,6 +86,18 @@ final class LaneDropRegistry {
|
||||
private(set) var grids: [ItemID: Grid] = [:]
|
||||
private(set) var heights: [ItemID: CGFloat] = [:]
|
||||
|
||||
/// How many times a registered card height has actually **changed**, ever — the freshness signal
|
||||
/// `RestingLayoutCache` keys its heights on.
|
||||
///
|
||||
/// A height is not snapshot content, and that is the whole reason this counter exists: a face
|
||||
/// measuring itself for the first time replaces the `nominalCardHeight` stand-in its lane was
|
||||
/// tiled with, and no store's `snapshotGeneration` moves for it. A layout cached across that
|
||||
/// would keep proposing against the guess.
|
||||
///
|
||||
/// Bumped only on a real change, so the steady state — a drag over a board that has finished
|
||||
/// laying out, which is every drag after its first frames — leaves it still and the cache holds.
|
||||
private(set) var heightsGeneration = 0
|
||||
|
||||
/// Each lane's title bar, in the same global space `Grid.frame` is written in — the topmost-rule
|
||||
/// stripe (`FileDropZones.landing`). Absent for a lane that has not laid its header out yet, and
|
||||
/// the file zones then let the masonry answer alone.
|
||||
@@ -79,8 +109,164 @@ final class LaneDropRegistry {
|
||||
func update(header frame: CGRect, for laneID: ItemID) { headers[laneID] = frame }
|
||||
func removeHeader(_ laneID: ItemID) { headers.removeValue(forKey: laneID) }
|
||||
|
||||
func update(height: CGFloat, for cardID: ItemID) { heights[cardID] = height }
|
||||
func removeHeight(_ cardID: ItemID) { heights.removeValue(forKey: cardID) }
|
||||
func update(height: CGFloat, for cardID: ItemID) {
|
||||
guard heights[cardID] != height else { return }
|
||||
heights[cardID] = height
|
||||
heightsGeneration += 1
|
||||
}
|
||||
|
||||
func removeHeight(_ cardID: ItemID) {
|
||||
guard heights.removeValue(forKey: cardID) != nil else { return }
|
||||
heightsGeneration += 1
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - The resting layout a card drag proposes against
|
||||
|
||||
/// One lane's **resting layout**, as a card drag's zones read it: the cards standing in it with the
|
||||
/// dragged run already lifted out, and how tall each one is.
|
||||
///
|
||||
/// **Ids and heights, never `Card` values.** This is what the masonry zones are built over
|
||||
/// (`DropSlotMath.cardSlot`), and all they want from a card is a number; a `[Card]` of it would be
|
||||
/// nine `FieldValue`s and a whole `FrontmatterDocument` of ARC traffic per card, rebuilt on every
|
||||
/// mouse sample of the drag.
|
||||
struct LaneRestingLayout: Equatable, Sendable {
|
||||
|
||||
/// The lane's cards in logical order with the dragged run lifted out — **the index space every
|
||||
/// proposal in this lane is counted in**, which is the space `BoardStore.moveCards` /
|
||||
/// `copyCards` resolve theirs in too (each writer counted in the layout its own gesture showed).
|
||||
var cardIDs: [ItemID]
|
||||
|
||||
/// Their heights, aligned 1:1 with `cardIDs`, each one the registered measurement or
|
||||
/// `LaneDropRegistry.nominalCardHeight` for a face that has not laid out yet.
|
||||
var heights: [CGFloat]
|
||||
}
|
||||
|
||||
/// The per-lane resting layouts a drag is proposing against, built **once per snapshot** rather than
|
||||
/// once per mouse sample.
|
||||
///
|
||||
/// ### Why this is not a hole in the re-grounding rule
|
||||
///
|
||||
/// "Geometry re-derives … the resting zones are recomputed against each new snapshot. Nothing is
|
||||
/// cached across a reload because nothing needs to be" (DRAG-REORDER.md § The mid-drag re-grounding
|
||||
/// trio, rule 1). Nothing is cached across a reload here either: an entry stands only for the
|
||||
/// `snapshotGeneration` it was built from, so a foreign reload landing mid-drag retires every layout
|
||||
/// derived from the snapshot before it and the next sample re-derives against the board as it now
|
||||
/// is. What this removes is the *repetition* — the same lane rebuilt from the same snapshot dozens
|
||||
/// of times a second, for samples whose answer `DragSession.propose` then discards as unchanged.
|
||||
///
|
||||
/// ### Why the applied counter is the right one
|
||||
///
|
||||
/// **`BoardStore.snapshotGeneration`, not `landedReloads`.** The layout is a function of the
|
||||
/// snapshot's *content*, and the applied counter is exactly "the content changed": a value-equal
|
||||
/// walk lands the same cards in the same order and cannot move a zone, so keying on the walk counter
|
||||
/// would throw the layout away for reloads that provably changed nothing. The committed-overlay hold
|
||||
/// does draw an arrangement the snapshot does not describe while it stands — but it also freezes
|
||||
/// `propose` and `resolveOperation`, so nothing this cache feeds reaches a proposal for as long as
|
||||
/// it does.
|
||||
///
|
||||
/// ### What the key has to carry, and what it provably need not
|
||||
///
|
||||
/// Everything the layout derives from that can move mid-drag is in the key: the snapshot
|
||||
/// (`generation`, plus `boardRoot` because a cross-board drag retargets against the *hovered*
|
||||
/// board's store), the lane, the registered heights (`LaneDropRegistry.heightsGeneration`), and the
|
||||
/// dragged run's own hidden set — which follows the **operation**, since ⌥ pressed mid-drag leaves a
|
||||
/// copy's originals standing in the layout (`DragSession.hiddenMembers`).
|
||||
///
|
||||
/// Two inputs are deliberately absent. The **grid** is not layout content — it is read live at every
|
||||
/// sample (`LaneDropRegistry.Grid.placement`), because a lane that scrolls moves its origin without
|
||||
/// touching any snapshot. And the **dragged run's frozen heights** are frozen at pickup by
|
||||
/// construction (`DragSession.cardHeights`), so they cannot move for the life of an entry — which is
|
||||
/// one drag, since the session clears this at `begin` and at `end`.
|
||||
@MainActor
|
||||
final class RestingLayoutCache {
|
||||
|
||||
/// Which lane's layout, in which board window.
|
||||
///
|
||||
/// The **registry** rather than the store: `heights` and `grids` belong to one board window, so
|
||||
/// two windows open on one board are two layouts even though they share a store and a lane id.
|
||||
private struct Key: Hashable {
|
||||
var registry: ObjectIdentifier
|
||||
var laneID: ItemID
|
||||
}
|
||||
|
||||
/// A layout plus everything it was derived from — the whole of what invalidates it.
|
||||
private struct Entry {
|
||||
var boardRoot: BoardRootKey
|
||||
var generation: Int
|
||||
var heightsGeneration: Int
|
||||
var hidden: Set<ItemID>
|
||||
var layout: LaneRestingLayout
|
||||
}
|
||||
|
||||
private var entries: [Key: Entry] = [:]
|
||||
|
||||
/// How many layouts this drag has built, and how many samples were answered from one already
|
||||
/// standing — the observation handle that makes "the steady state is a containment scan" a test
|
||||
/// rather than a hope (`RestingLayoutCacheTests`).
|
||||
///
|
||||
/// Instance counters rather than `BoardLoader.ParseCounter`'s injected object, because the
|
||||
/// subject is different: a walk is a stateless static called from several tasks at once and needs
|
||||
/// somewhere per-walk to tally into, while this is one object per drag whose whole lifetime is
|
||||
/// the question being asked. Nothing in the app reads them.
|
||||
private(set) var builds = 0
|
||||
private(set) var reuses = 0
|
||||
|
||||
init() {}
|
||||
|
||||
/// `laneID`'s resting layout on `store`'s current snapshot, built if this is the first sample to
|
||||
/// ask for it since anything it derives from moved.
|
||||
///
|
||||
/// - Returns: `nil` when `laneID` is not in the snapshot at all — the vanished-lane case, which
|
||||
/// is rule 2's and the caller's to answer (`BoardDropContext.revalidateProposal`).
|
||||
func layout(
|
||||
inLane laneID: ItemID,
|
||||
of store: BoardStore,
|
||||
registry: LaneDropRegistry,
|
||||
hidden: Set<ItemID>
|
||||
) -> LaneRestingLayout? {
|
||||
let key = Key(registry: ObjectIdentifier(registry), laneID: laneID)
|
||||
if let entry = entries[key],
|
||||
entry.generation == store.snapshotGeneration,
|
||||
entry.heightsGeneration == registry.heightsGeneration,
|
||||
entry.boardRoot == store.rootKey,
|
||||
entry.hidden == hidden {
|
||||
reuses += 1
|
||||
return entry.layout
|
||||
}
|
||||
|
||||
guard let lane = store.snapshot.lanes.first(where: { $0.id == laneID }) else {
|
||||
entries.removeValue(forKey: key)
|
||||
return nil
|
||||
}
|
||||
|
||||
var cardIDs: [ItemID] = []
|
||||
var heights: [CGFloat] = []
|
||||
cardIDs.reserveCapacity(lane.cards.count)
|
||||
heights.reserveCapacity(lane.cards.count)
|
||||
for card in lane.cards where !hidden.contains(card.id) {
|
||||
cardIDs.append(card.id)
|
||||
heights.append(registry.heights[card.id] ?? LaneDropRegistry.nominalCardHeight)
|
||||
}
|
||||
|
||||
let layout = LaneRestingLayout(cardIDs: cardIDs, heights: heights)
|
||||
entries[key] = Entry(
|
||||
boardRoot: store.rootKey,
|
||||
generation: store.snapshotGeneration,
|
||||
heightsGeneration: registry.heightsGeneration,
|
||||
hidden: hidden,
|
||||
layout: layout
|
||||
)
|
||||
builds += 1
|
||||
return layout
|
||||
}
|
||||
|
||||
/// Drops every layout — the drag that filled them is over, or a new one is starting.
|
||||
func clear() {
|
||||
entries.removeAll(keepingCapacity: true)
|
||||
builds = 0
|
||||
reuses = 0
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - The board window's half of a drop
|
||||
@@ -224,28 +410,28 @@ struct BoardDropContext {
|
||||
/// originals, because that is the layout the user is looking at. `DropSlotMath` has no opinion
|
||||
/// on the matter — the caller decides what the resting layout contains — and the commit's two
|
||||
/// writers are counted in exactly these two spaces (`BoardStore.moveCards` / `copyCards`).
|
||||
///
|
||||
/// **The layout is rebuilt per snapshot, the zones are resolved per sample** (`RestingLayoutCache`
|
||||
/// — including why that keeps rule 1 of the re-grounding trio exactly). What is left here at
|
||||
/// event time is the geometry that genuinely moves under a stationary cursor: the grid, which the
|
||||
/// autoscroll driver scrolls on purpose, and `DropSlotMath`'s own containment arithmetic.
|
||||
func retargetCards(inLane laneID: ItemID) {
|
||||
guard session.isDraggingCards, let cursor = globalCursor() else { return }
|
||||
guard let lane = store.snapshot.lanes.first(where: { $0.id == laneID }) else {
|
||||
guard let resting = session.restingLayouts.layout(
|
||||
inLane: laneID,
|
||||
of: store,
|
||||
registry: registry,
|
||||
hidden: session.hiddenMembers(onBoardRooted: store.rootKey)
|
||||
) else {
|
||||
revalidateProposal()
|
||||
return
|
||||
}
|
||||
guard let grid = registry.grids[laneID] else { return }
|
||||
|
||||
let hidden = session.hiddenMembers(onBoardRooted: store.rootKey)
|
||||
let rendered = lane.cards.filter { !hidden.contains($0.id) }
|
||||
let heights = rendered.map { registry.heights[$0.id] ?? LaneDropRegistry.nominalCardHeight }
|
||||
let placement = MasonryPlacement(
|
||||
columnCount: grid.columns,
|
||||
columnWidth: MasonryPlacement.columnWidth(
|
||||
totalWidth: grid.frame.width, columnCount: grid.columns, spacing: grid.spacing),
|
||||
spacing: grid.spacing,
|
||||
origin: grid.frame.origin
|
||||
)
|
||||
let slot = DropSlotMath.cardSlot(
|
||||
cursor: cursor,
|
||||
placement: placement,
|
||||
heights: heights,
|
||||
placement: grid.placement,
|
||||
heights: resting.heights,
|
||||
// The run's footprint at the landing spot: the first dragged card's frozen height, which
|
||||
// is the trigger rect the cursor is over (the rest stack below it).
|
||||
draggedHeight: session.cardHeights.first ?? LaneDropRegistry.nominalCardHeight,
|
||||
@@ -392,19 +578,12 @@ struct BoardDropContext {
|
||||
|
||||
let rendered = lane.cards
|
||||
let heights = rendered.map { registry.heights[$0.id] ?? LaneDropRegistry.nominalCardHeight }
|
||||
let placement = MasonryPlacement(
|
||||
columnCount: grid.columns,
|
||||
columnWidth: MasonryPlacement.columnWidth(
|
||||
totalWidth: grid.frame.width, columnCount: grid.columns, spacing: grid.spacing),
|
||||
spacing: grid.spacing,
|
||||
origin: grid.frame.origin
|
||||
)
|
||||
let count = FinderDrop.shadowCount(info.itemProviders(for: [.fileURL]))
|
||||
|
||||
let landing = FileDropZones.landing(
|
||||
cursor: cursor,
|
||||
headerBottom: registry.headers[laneID]?.maxY,
|
||||
placement: placement,
|
||||
placement: grid.placement,
|
||||
heights: heights,
|
||||
nominalHeight: LaneDropRegistry.nominalCardHeight,
|
||||
current: session.fileLaneProposal(onBoardRooted: store.rootKey, laneID: laneID)?.index
|
||||
|
||||
Reference in New Issue
Block a user