oficina / sheets Public

Mac: scrolling builds fewer cells per step, and keeps the ones it has

#72, Fable's review of 58b4cb0, finding 4. Scrolling arrived in lumps: each
window-block crossing (8 rows or 4 columns) built a block of new cells in
one frame, and every visible cell's body ran again as well because
SheetCell, carrying closures, never compared equal.

- SheetCell is Equatable on what it draws from beyond the model's observed
  state (point, size, editing state), so a crossing runs the bodies of the
  new cells only: 96 instead of 384 per vertical crossing, counted.
- Blocks are now 2 columns and 4 rows, so a crossing builds half as many.
  Cell cost is mostly SwiftUI's Button (a plain tap gesture halved it again
  but does not answer the synthetic clicks the tests use, so it stays).

Programmatic scroll, 120 steps, 1000x200 sheet, 1200x800 window, this host:
worst step 113 / 117 / 154 ms (vertical / horizontal / diagonal) before,
85 / 103 / 115 after; mean 17 / 22 / 40 before, 23 / 30 / 49 after (more,
smaller crossings). First key to glyph unchanged (23 / 31 / 29 ms).

GridScrollTests.testScrollStepsStayCheap measures and bounds the worst step.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 6e24193adadf090398c2c0bd74b3f55db6b70201 parent 11580e1
3 files changed, +62 −6
  • native/apple/Sources/SheetsKit/SheetCell.swift +12 −1
  • native/apple/Sources/SheetsKit/SheetGrid.swift +6 −5
  • native/apple/Tests/SheetsKitTests/GridGeometryTests.swift +44 −0
diff --git a/native/apple/Sources/SheetsKit/SheetCell.swift b/native/apple/Sources/SheetsKit/SheetCell.swift
index 58f4afd..0c85835 100644
--- a/native/apple/Sources/SheetsKit/SheetCell.swift
+++ b/native/apple/Sources/SheetsKit/SheetCell.swift
@@ -1,6 +1,17 @@
import SwiftUI
-struct SheetCell: View {
+/// Equatable on what it draws from besides the model's observed state (which
+/// SwiftUI tracks on its own): the grid rebuilds its rows whenever scrolling
+/// crosses a window block, and without this every visible cell's body ran
+/// again each time. Its closures act on the grid and are equal in effect.
+struct SheetCell: View, Equatable {
+ nonisolated static func == (a: Self, b: Self) -> Bool {
+ // SwiftUI compares views on the main thread, where it builds them.
+ MainActor.assumeIsolated {
+ a.model === b.model && a.point == b.point && a.columnWidth == b.columnWidth
+ && a.rowHeight == b.rowHeight && a.editState === b.editState
+ }
+ }
@Bindable var model: SheetModel
let point: Coordinate
var select: () -> Void
diff --git a/native/apple/Sources/SheetsKit/SheetGrid.swift b/native/apple/Sources/SheetsKit/SheetGrid.swift
index ddd10e2..2d0b6a3 100644
--- a/native/apple/Sources/SheetsKit/SheetGrid.swift
+++ b/native/apple/Sources/SheetsKit/SheetGrid.swift
@@ -253,9 +253,10 @@ struct SheetGridMetrics {
width: width(point.c), height: height(point.r))
}
/// The columns and rows a visible rect (content coordinates) touches,
- /// widened to whole blocks (4 columns, 8 rows), so scrolling re-renders the
+ /// widened to whole blocks (2 columns, 4 rows), so scrolling re-renders the
/// grid only every few columns or rows, never every pixel. Blocks are kept
- /// small: every cell built is a cell opening an editor has to lay out.
+ /// small: each crossing builds a block of new cells in one frame, and
+ /// every cell built is one that opening an editor has to lay out.
func window(for visible: CGRect) -> (cols: Range<Int>, rows: Range<Int>) {
func span(_ starts: [Double], _ count: Int, _ lo: Double, _ hi: Double, block: Int) -> Range<Int> {
guard count > 0 else { return 0..<0 }
@@ -271,8 +272,8 @@ struct SheetGridMetrics {
return start..<max(start, end)
}
return (
- span(colStarts, cols, visible.minX - Self.rowHeader, visible.maxX - Self.rowHeader, block: 4),
- span(rowStarts, rows, visible.minY - Self.colHeader, visible.maxY - Self.colHeader, block: 8))
+ span(colStarts, cols, visible.minX - Self.rowHeader, visible.maxX - Self.rowHeader, block: 2),
+ span(rowStarts, rows, visible.minY - Self.colHeader, visible.maxY - Self.colHeader, block: 4))
}
}
@@ -444,7 +445,7 @@ struct SheetGridContent: View, Equatable {
model: model, point: point,
select: { actions.select(point) },
beginEditing: { actions.edit(point) },
- columnWidth: m.width(col), rowHeight: m.height(row), editState: editState)
+ columnWidth: m.width(col), rowHeight: m.height(row), editState: editState).equatable()
}
#if os(macOS)
private func resizeHandle(axis: String, index: Int) -> some View {
diff --git a/native/apple/Tests/SheetsKitTests/GridGeometryTests.swift b/native/apple/Tests/SheetsKitTests/GridGeometryTests.swift
index d77b2fb..b9bf8a5 100644
--- a/native/apple/Tests/SheetsKitTests/GridGeometryTests.swift
+++ b/native/apple/Tests/SheetsKitTests/GridGeometryTests.swift
@@ -170,3 +170,47 @@ final class GridGeometryTests: XCTestCase {
}
}
#endif
+
+#if os(macOS)
+/// Review #72 (58b4cb0), finding 4: scrolling crosses a window block every
+/// few rows or columns, and each crossing rebuilt every visible cell's body.
+final class GridScrollTests: XCTestCase {
+ @MainActor func testScrollStepsStayCheap() async throws {
+ let model = fixtureModel(SheetServerFixture(), rows: 1000, cols: 200)
+ for r in 0..<60 { for c in 0..<20 { model.cells[Coordinate(r: r, c: c)] = Cell(r: r, c: c, raw: "\(r)·\(c)", display: "\(r)·\(c)") } }
+ let host = NSHostingView(rootView: SheetGrid(model: model))
+ let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 1200, height: 800), styleMask: [.titled], backing: .buffered, defer: false)
+ window.contentView = host; NSApp.setActivationPolicy(.regular); window.makeKeyAndOrderFront(nil); NSApp.activate()
+ defer { window.orderOut(nil) }
+ try await Task.sleep(for: .milliseconds(300))
+ func scrolls(_ v: NSView) -> [NSScrollView] { ((v as? NSScrollView).map { [$0] } ?? []) + v.subviews.flatMap(scrolls) }
+ let scroll = try XCTUnwrap(scrolls(host).first)
+ var report: [String] = []
+ for (name, dx, dy) in [("vertical", 0.0, 32.0), ("horizontal", 55.0, 0.0), ("diagonal", 55.0, 32.0)] {
+ scroll.contentView.scroll(to: .zero); scroll.reflectScrolledClipView(scroll.contentView)
+ try await Task.sleep(for: .milliseconds(200))
+ var steps: [Double] = []
+ for step in 1...120 {
+ let start = ProcessInfo.processInfo.systemUptime
+ scroll.contentView.scroll(to: NSPoint(x: dx * Double(step), y: dy * Double(step)))
+ scroll.reflectScrolledClipView(scroll.contentView)
+ host.layoutSubtreeIfNeeded(); window.displayIfNeeded()
+ var spent = ProcessInfo.processInfo.systemUptime - start
+ // The window update the scroll set off renders on the next turn.
+ await Task.yield(); await Task.yield()
+ let again = ProcessInfo.processInfo.systemUptime
+ host.layoutSubtreeIfNeeded(); window.displayIfNeeded()
+ spent += ProcessInfo.processInfo.systemUptime - again
+ steps.append(spent * 1000)
+ }
+ let mean = steps.reduce(0, +) / Double(steps.count)
+ let worst = steps.max() ?? 0
+ report.append("\(name) mean \(String(format: "%.1f", mean)) worst \(String(format: "%.0f", worst)) ms")
+ // Was 113 / 117 / 154 ms worst at 58b4cb0 on this host; a busy
+ // machine adds a little, so the bound is loose.
+ XCTAssertLessThan(worst, 130, "\(name): a scroll step took \(worst) ms")
+ }
+ print("MEASURE scroll steps, 1000x200, 1200x800: " + report.joined(separator: "; "))
+ }
+}
+#endif