perf(library): memoize scan parses per save (#143)
CI / ci (push) Canceled after 0s

Every FSEvents change — each 1 s autosave echo — ran Post.scan over the
whole library: read + frontmatter parse + title per post.md, O(library
bytes) per save cycle while writing, and the posts array was replaced
unconditionally, so every observing pane regrouped and re-rendered even
when nothing changed.

Post.scan now takes an optional PostScanCache (path+mtime+size-keyed
parse memo, nil default — every other caller unchanged): the readdir
walk stays the truth, only changed files are read and re-parsed. The
stamp is captured BEFORE the read and keys the store, so a mid-scan
rewrite can never be pinned as fresh — a late overlapping store keys
its own pre-read stamp and the next scan misses (review-caught race,
pinned by a same-size-rewrite test). rescan skips the posts assignment
when the scan equals the current list; @Observable invalidates on
assignment regardless of equality, so panes stop re-rendering on the
app's own autosave echoes.

Deliberately skipped (measured cold): folder(of:) caching (stat-only
walk) and refreshCards throttling (O(open piece), card surfacing
depends on it).

Closes #143
This commit is contained in:
AlexKucera committed 2026-11-03 11:19:50 +01:00
1 parent cc317b65b9
commit a2f6dc1653
9 files changed
+287 -39

No files matched your search

+74
View File
@@ -234,3 +234,77 @@ private func writePost(_ home: URL, _ id: String, _ body: String) throws {
#expect(PostStore.folder(of: "mine", home: home)?
.pathComponents.suffix(2).first == "posts")
}
// MARK: #143 scan cache (OPT-F3)
@Test func `unchanged files parse once then serve from cache`() throws {
let home = try makeHome { home in
try writePost(home, "one", "## One\n")
try writePost(home, "two", "## Two\n")
try writePost(home, "three", "## Three\n")
}
let cache = PostScanCache()
let first = Post.scan(home: home, cache: cache)
#expect(cache.misses == 3) // cold scan: every file read + parsed once
let second = Post.scan(home: home, cache: cache)
#expect(cache.hits == 3) // warm scan: zero re-reads
#expect(second == first) // identical result through the cache
}
@Test func `changed file re-parses while untouched files hit`() throws {
let home = try makeHome { home in
try writePost(home, "one", "## One\n")
try writePost(home, "two", "## Two\n")
}
let cache = PostScanCache()
_ = Post.scan(home: home, cache: cache)
// The autosave echo: one post.md rewritten (mtime + size both move).
try "## Two Retitled\n".write(
to: home.appendingPathComponent("projects/posts/two/post.md"),
atomically: true,
encoding: .utf8,
)
let posts = Post.scan(home: home, cache: cache)
#expect(posts.first { $0.id == "two" }?.title == "Two Retitled")
#expect(cache.misses == 3) // one re-read for the changed file
#expect(cache.hits == 1) // one reuse for the untouched one
}
@Test func `moved piece re-parses at its new path`() throws {
// The cache is keyed by the post.md PATH — a folder move (archive,
// move-to-project) keeps bytes and mtime but changes the path, so the
// stale entry can never answer and isArchived re-derives from disk.
let home = try makeHome { home in
try writePost(home, "one", "## One\n")
}
let cache = PostScanCache()
_ = Post.scan(home: home, cache: cache)
try FileManager.default.createDirectory(
at: home.appendingPathComponent("archive/posts", isDirectory: true),
withIntermediateDirectories: true,
)
try FileManager.default.moveItem(
at: home.appendingPathComponent("projects/posts/one", isDirectory: true),
to: home.appendingPathComponent("archive/posts/one", isDirectory: true),
)
let posts = Post.scan(home: home, cache: cache)
#expect(posts.first?.isArchived == true)
#expect(posts.first?.project == "posts")
}
@Test func `a late store carrying a stale stamp never pins stale content`() throws {
// The overlapping-rescan race, pinned at its end-state: scan A reads
// v1 and stalls, the file rewrites (v2), scan B caches v2, then A's
// late store lands keyed on ITS pre-read (v1) stamp — the next scan
// must mismatch, re-read, and serve disk truth, never v1.
let home = try makeHome { home in
try writePost(home, "one", "## One\n")
}
let cache = PostScanCache()
let stale = try #require(Post.scan(home: home, cache: cache).first)
let url = home.appendingPathComponent("projects/posts/one/post.md")
let staleStamp = PostScanCache.stamp(at: url) // captured pre-rewrite
try "## Two\n".write(to: url, atomically: true, encoding: .utf8)
cache.store(stale, at: url, stamp: staleStamp) // A's late store
#expect(Post.scan(home: home, cache: cache).first?.title == "Two")
}