Repository navigation
perf(provider): charge the SSD write ledger once per attention donation (~5× faster donations) - #1647
Conversation
GPT-OSS and Gemma write one SSD cache file per 256-token block, and the write-behind consumer charged the daily write budget once per block. In production that budget is the shared .write-budget ledger: each charge rewrote it with pwrite and F_FULLFSYNC under a non-blocking flock, and a writer that met the held lock was refused as rate-limited. Block data is not fsynced, so the ledger flush dominated each block's write. A donation of more than one block now first reserves the summed serialized size of all its blocks in one charge, before any block is written. When that charge is refused, near the daily cap or while another writer holds the ledger, each block is charged before its own write, so the leading blocks that fit are still written. Charges still precede writes and are never refunded; blocks skipped after the donation's charge stay charged, like a failed write. 64-block donations on an M3 Ultra, debug build: 412-445 ms -> 73-78 ms with 1 MiB blocks and 502-518 ms -> 114-118 ms with 4 MiB blocks. Two stores sharing the ledger and donating 64 blocks each at the same moment, 10 repetitions: 633/1280 -> 123/1280 blocks refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Threat model review — advisoryReviewed head Reviewers: anthropic/claude-sonnet-5.5: first pass completed; anthropic/claude-opus-5.5: selected depth completed (1 batches); openai/gpt-6.1-sol: not needed by escalation policy No actionable findings in the reviewed text. This is not a security approval. Models: budgeted Sonnet / selective Opus / selective Sol 6.1. Findings require human validation; this review never requests changes or blocks merging. Scan completed. Coverage: 46/46 source units; cross-file integration complete. 0 selected depth batch(es) pending. All threat definitions indexed; relevant full definitions selected. Unchanged callers are not scanned. Cost this run: $0.0000 reported, $0.0000 reserved with unknown cost; 0 paid request(s), 0 reused batch(es), 0 provider cache-hit tokens. Saved findings and earlier report · Historical findings are not confirmed resolved. Bedrock: 17 request(s), 825781 uncached + 0 cache-read + 0 cache-write input / 10518 output tokens; 0 request(s) with unknown usage. OpenRouter backup used 0 time(s). Dollar amounts above cover OpenRouter only; AWS billing is reconciled separately. |
Brings in #1651 (retire SSD sweeps in short slices), #1642 (reread only new or changed headers in root maintenance), #1647 (charge the write ledger once per attention donation), #1649 (single-copy SSD reads) and #1648 (startup expiry is not counted as corruption). The only textual conflict was docs/reference/ssd-kv-cache.md. Master's maintenance-sweep, header-reuse and retirement-slicing text is kept, and this branch's TTL row and its exclusive-lease wording for owned retirement (a file a stage is reading is skipped) are applied on top. Sources and tests merged cleanly: master does not touch the coordinator or the read path, and the attention write-behind keeps exclusive leases. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Brings in the SSD cache changes merged since this branch's base (#1642, #1647, #1648, #1649, #1651) plus #1639, #1640 and #1646. The only conflict was the docs/reference/ssd-kv-cache.md table: keep this PR's victim selection row and master's updated maintenance sweep row. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Brings the 13 changes that landed on master only into dev, so the next dev release and the move to master contain them: SSD cache work (Layr-Labs#1641, Layr-Labs#1642, Layr-Labs#1643, Layr-Labs#1647, Layr-Labs#1648, Layr-Labs#1649, Layr-Labs#1651, Layr-Labs#1657 and the test fixture fix 093aaf3), overlapped weight hashing (Layr-Labs#1638), GPT-OSS stop sequences in the Harmony final message (Layr-Labs#1645), faster reconnects (Layr-Labs#1613) and the provider 0.10.5 preparation (Layr-Labs#1678). Layr-Labs#1659 is master's copy of dev's Layr-Labs#1658. Conflicts: the reconnect loop keeps dev's cluster-member handling and takes master's reconnect policy (the member-aware delay now starts from the policy's range, and the session is marked open after registration for both roles). The bridge passes both dev's default max-token context limit and master's stop-string scope. WeightHasher takes master's overlapped reads with dev's reason for reading uncached. Four case lists were the same set in a different order, a test-budget line differed only in wrapping, and two docs needed both sides. libs/mlx-swift-lm moves to mlx-swift-lm main 2beb401f, which contains dev's d5bd32b8 and master's Layr-Labs#368 and Layr-Labs#369. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Summary
GPT-OSS and Gemma donate one SSD cache file per 256-token block, and the write-behind consumer charged the daily write budget once per block. In production that budget is the root-wide
.write-budgetledger: every charge rewrites it withpwrite+F_FULLFSYNCunder a non-blockingflock. Block data itself is not fsynced (strictFsyncis off), so the ledger flush dominated each block's write. Because the ledger is shared and nothing waits on it by design, another model's writer that met the held lock was refused as rate-limited.A donation of more than one block now first reserves the summed serialized size of all its blocks in one charge, before its first write. If that charge is refused (cap near, ledger busy), each block is charged before its own write exactly as before, so the leading blocks that fit are still written. Single-block donations are unchanged.
Linked issue
No linked issue. Found while measuring the SSD write path.
Test plan
SSDWriteBehindwith real DBK3 writes, a 64-block donation, a fresh root and ledger per run, M3 Ultra internal SSD, debug test build, 3 runs each.masterThe ledger cost falls from about 5.4-6.3 ms per block to one flush per donation.
SSDWriteBudgetinstances on one ledger root, like two models in a default installation.mastermightAcceptchecks while store X writes a 64-block donationNew
SSDWriteBehindLedgerChargeTests(deterministic, no sleeps):masterthis test fails: 1 block is written and 3 are refused.The last three pass on
mastertoo; they guard behavior this PR keeps.Every SSD-cache suite (all
Tests/ProviderCoreTests/KVCacheSSD/files plus the write-budget, rate-limiter, cache-volume and cache-storage suites): 403 tests in 73 suites pass, on the build with this change.make docs-impact-check BASE=origin/master,make docs-check,make changelog-check,git diff --check.Components touched
Protocol / interface changes
The ledger format, its SHA-256 record check, the inode and device checks, the no-follow path walk and the daily cap are unchanged (
SSDWriteBudget.swiftandSSDWriteRateLimiter.swiftare untouched). Cache encryption, file names and the coordinator-facing outcomes are unchanged.Documentation impact
docs/reference/ssd-kv-cache.md(write-budget paragraph).Changelog
changelog.d/<topic>.mdfragment and ranmake changelog-check.Notes for reviewers
SSDHybridCheckpointStore) already write one file per job and charge once, so they are unchanged. So is the donate-side advisory check (mightAcceptWrite, gated on a single block).Freshness receipt
master8effff7f78effff7f7libs/mlx-swift-lmaee6ebb6,libs/mlx-swift4926d3d9)Before And After
sequenceDiagram participant W as Write-behind consumer participant L as Shared .write-budget ledger participant D as Cache files Note over W,D: Before: a 64-block donation loop each of 64 blocks W->>L: flock (non-blocking), pwrite, F_FULLFSYNC (about 6 ms) W->>D: encrypt and write block end Note over W,D: After W->>L: one charge for the summed size (about 6 ms) loop each of 64 blocks W->>D: encrypt and write block end Note over W,L: If that charge is refused, each block is charged as beforeflowchart TD subgraph Before a0["consume(job)"] --> a1["for each block"] --> a2{"tryConsume(block size)?"} a2 -->|"yes"| a3["write block"] a2 -->|"no"| a4["drop, write_rate_limited"] end subgraph After b0["consume(job)"] --> b1{"more than one block and<br/>tryConsume(summed size)?"} b1 -->|"yes"| b2["for each block: write"] b1 -->|"no"| b3["for each block"] --> b4{"tryConsume(block size)?"} b4 -->|"yes"| b5["write block"] b4 -->|"no"| b6["drop, write_rate_limited"] endRefactor Pass
A dedicated refactor reviewer read the diff against the invariants: a charge before every write, no refunds, no waiting, a refused charge with no side effects and the old per-block path behind it, single-block donations unchanged, and
SSDWriteBudget.swift/SSDWriteRateLimiter.swiftuntouched. It found no logic issue and made wording edits only: the comment and doc now say "instead" rather than "as before"; the helper doc covers the overflow exit; and the reference doc's list of refusal causes is marked as examples.It confirmed two accepted over-charge cases, both in the conservative direction. A paid donation dropped because the writer closed is still charged. A ledger write whose
pwritelanded but whose flush failed is reported refused and then charged again per block, as per-block charges already were.From its other notes, two comments that described charging as always per entry now say it applies when the whole donation does not fit (
SSDPrefixCache.swift,SSDPrefixCacheTests.swift). I also added the shared-ledger near-cap test. After the pass: rebuild, 403 tests in 73 suites passing, anddocs-impact-check,docs-check,changelog-checkandgit diff --checkclean.Appendix: measurement probe (temporary test file, not committed)
Run with
ZZ_LEDGER_PROBE=1 swift test --skip-build --no-parallel --filter ZZLedgerProbe.Final verification (2026-10-28, head
cfea5a690)Re-run with the same probe at the exact head and on
master(8effff7f7), M3 Ultra, debug test build:master(3 runs)mastermightAcceptduring X's donation🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need.Freshness (2026-10-28 19:00 UTC)
masterobserved at300134d24(it gained Repair master after #1625: remove console-ui hooks, CI lane and docs citations #1629, fix(provider): stop Qwen 3.5/3.6 looping to max_tokens on omitted sampling #1652, test(provider): stop the unloaded-deletion epoch test hanging the SSD suites #1653 and test(provider): cut ~75 s of serial test time under the 900 s CI watchdog #1644). Merged into this branch with signed merge commit38e881768; the merge added exactlymaster's delta (identical patch IDs) with no conflict resolution.libs/mlx-swift-lmataee6ebb6, same asmaster).38e881768: Provider Unit Tests, Provider SDK Tests, Provider Prompt Parity and Provider Tests pass.