From 9f4d9ef9ea87f824c0905edbefa50d00e79ce4a8 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Fri, 13 Nov 2026 04:21:28 +0100 Subject: [PATCH] perf(cold-turkey): minutes feeds skip word counting; word rail goes count-only MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Root cause (re-derived live at 5915178): WritingLockState.feed -> WritingGoalRun.update counted words for EVERY goal type, and DraftStats.words built the complete lowercased token array (text.split(whereSeparator:).map(lowercased)) just to return .count — O(document) allocation + lowercasing per keystroke on the main actor for minutes goals whose verdict/progress/readout never read the count (argus probe at efbde25c: minutes ~0.32/3.07/16.64 ms per update at 1k/10k/50k words, linear in document size). Fix, two halves, semantics byte-stable: - update(text:) guards on .words — a minutes run does no word work on feed at all; its accounting freezes at the arm-time baseline (currentWords was dead state for minutes runs: readout, isMet, progress, and the completion note all ride the clock). - DraftStats.words becomes a count-only Character pass over maximal isLetter/isNumber runs — never materializing the lowercased [String]. Identical count by construction: the tokenizer's split (which always omits empty subsequences) yields exactly the maximal runs; equivalence pinned against the split expression over a mixed-script/emoji/punctuation/number corpus in DraftStatsTests. WritingGoalRun.init (arm-time baseline) keeps counting once per arm through the same rail — now also allocation-free. Callers unchanged (update's countWords defaults to DraftStats.words); the words-goal net accounting is pinned unchanged ('a words update takes exactly one word-rail trip' + all existing net-delta pins). --- Museion/DraftStats.swift | 22 ++++++++++++++++++++-- Museion/WritingGoal.swift | 26 +++++++++++++++++--------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/Museion/DraftStats.swift b/Museion/DraftStats.swift index 767afd0..3aace67 100644 --- a/Museion/DraftStats.swift +++ b/Museion/DraftStats.swift @@ -79,10 +79,28 @@ nonisolated struct DraftStats: Equatable { /// Word count over the same token semantics every text metric /// rides (#161: Cold Turkey net-words counts with this — one - /// tokenizer, no drift). "writer's" is two words here, headings + /// word rail, no drift). "writer's" is two words here, headings /// are body text, frontmatter is not (the editor never shows it). + /// Count-only (opt pass 2026-11-09): one Character pass counting + /// maximal isLetter/isNumber runs — the lowercased [String] the + /// per-keystroke editor feed once paid for is never built. Equal + /// to the tokenizer's split by construction (omitting empty + /// subsequences leaves exactly the maximal runs); pinned against + /// the split expression over a mixed-script corpus. static func words(in text: String) -> Int { - tokens(in: text).count + var count = 0 + var inRun = false + for character in text { + if character.isLetter || character.isNumber { + if !inRun { + count += 1 + inRun = true + } + } else { + inRun = false + } + } + return count } /// Lowercased runs of Characters where isLetter || isNumber — the diff --git a/Museion/WritingGoal.swift b/Museion/WritingGoal.swift index 236d512..dc316a6 100644 --- a/Museion/WritingGoal.swift +++ b/Museion/WritingGoal.swift @@ -24,9 +24,9 @@ nonisolated enum WritingGoal: Equatable { /// (typing, deleting, pasting, undo all land as whole-document /// text), and `now` is injected, so the countdown can never be /// paused or deferred from inside the engine. Word counting rides -/// `DraftStats.words` — the one tokenizer all text metrics share, so -/// net-words can never drift from first-draft stats ("writer's" is -/// two words there, headings are body text; frontmatter is not body +/// `DraftStats.words` — the one word-count rail all text metrics +/// share, so net-words can never drift from first-draft stats +/// ("writer's" is two words there, headings are body text; frontmatter is not body /// text — the editor never shows it, so it never counts). nonisolated struct WritingGoalRun: Equatable { let goal: WritingGoal @@ -35,7 +35,9 @@ nonisolated struct WritingGoalRun: Equatable { let armedAt: Date /// Whole-document word count at arm. let baselineWords: Int - /// Whole-document word count as of the last `update(text:)`. + /// Whole-document word count as of the last `update(text:)` — + /// words goals only; a minutes run freezes at the arm-time + /// count (the verdict never reads it). private(set) var currentWords: Int init(goal: WritingGoal, armedAt: Date, body: String) { @@ -46,12 +48,18 @@ nonisolated struct WritingGoalRun: Equatable { currentWords = words } - /// Record the live editor text. The caller passes the whole - /// document every time; the net falls out of the delta. - /// `countWords` is the injectable word rail — the pins count - /// rail trips through this seam (a global counter would race - /// the hosted suite's parallel DraftStats calls). + /// Record the live editor text — words goals only. The caller + /// passes the whole document every time; the net falls out of + /// the delta. A minutes run never re-counts: its verdict and + /// progress ride the clock alone, so the per-keystroke feed + /// does no word work at all (opt pass 2026-11-09 — the feed + /// once tokenized the whole document per keystroke for a count + /// nothing read). `countWords` is the injectable word rail — + /// the pins count rail trips through this seam (a global + /// counter would race the hosted suite's parallel DraftStats + /// calls). mutating func update(text: String, countWords: (String) -> Int = DraftStats.words(in:)) { + guard case .words = goal else { return } currentWords = countWords(text) }