notes.append slows as the note grows: each append forks the whole document #527

Closed
opened 2026-10-17 09:20:05 +00:00 by kayg · 2 comments
Owner

Found by the tail-latency measurement on #512 (comment 56705, report calternal-research/perf-tails-2026-10-17.md).

notes.append p50 at 1 client on SSD went from 12 ms to 125 ms as the probe notes grew to about 3,500 lines. Each append forks the whole document first (ydoc::fork(state)), so the cost grows with the note's length, not with the size of the append.

This matters for logs and journals, which are append-heavy and grow all day.

Goal: make append cost proportional to the appended text, keeping the same crash-safety and history behaviour. Measure p50/p95 at 100, 1k and 3.5k lines before and after.

🤖 Generated with Claude Code

Found by the tail-latency measurement on #512 (comment 56705, report `calternal-research/perf-tails-2026-10-17.md`). `notes.append` p50 at 1 client on SSD went from **12 ms to 125 ms** as the probe notes grew to about 3,500 lines. Each append forks the whole document first (`ydoc::fork(state)`), so the cost grows with the note's length, not with the size of the append. This matters for logs and journals, which are append-heavy and grow all day. **Goal:** make append cost proportional to the appended text, keeping the same crash-safety and history behaviour. Measure p50/p95 at 100, 1k and 3.5k lines before and after. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Author
Owner

Fix is up in PR #529 (not merged).

Root cause

To append, the room copied the whole note (ydoc::fork(state)) only to learn where the text ends: Yjs puts new text after the last piece of text in the document, deleted or not, and yrs keeps that piece private. Building the copy re-integrates every piece, and yrs does this about quadratically when many writers chain off each other. Every keyed append is its own writer (the writer id comes from the idempotency key), so a log is exactly that shape. Measured on the copy alone: 0.1 ms at 100 lines, 2.4 ms at 1k, 15–26 ms at 3.5k.

Fix

The room remembers where its last append ended, plus the document's state vector at that moment (room/tail.rs). If the state vector is unchanged, the document has exactly the same pieces in the same order, so the next append is built directly as a one-item Yjs update (ydoc::append_update) and applied to the live doc. Deletes don't move the end. Anything that adds text (typing, outside edits, the server's keys at save) changes the state vector, and that one append takes the old copy path, which finds the end again.

What stays the same:

  • Text and merge behaviour. The applied change is byte-identical to the old path. A new fuzz test (40 seeds × 120 steps) checks this while typists insert, delete (often at the very end) and deliver late and out of order. A mutation check (dropping the state-vector guard) makes that test fail.
  • Crash safety and idempotency. Same key-derived writer, same "writer already in the doc" check, same history write before the answer.
  • History and versions. Same rows (the same applied bytes) and the same version numbers.

Patch and Replace are untouched.

Tests

  • tests/appends.rs:
    • byte-for-byte equivalence fuzz;
    • typists at the end plus keyed appends with retries in a real room (every word exactly once, clients converge, history replays to the note, file = note);
    • an append to a 2,000-line note stays small.
  • notes_crash.rs::kill_nine_mid_append_on_a_long_note: 4 concurrent writers append to a 1,000-line note, and the server gets 12 SIGKILLs. Unconfirmed appends are retried, and so are some confirmed ones. Each confirmed append is there exactly once, the file is never torn, and the file equals the note after a save.
  • examples/append_bench.rs: append p50/p95 at 100, 1k and 3.5k lines.

Bench (append_bench, durable keyed appends, local SSD, 3 alternating runs, 200 samples per size)

lines before p50 before p95 after p50 after p95
100 10.9–15.1 28.2–28.9 9.3–9.9 18.1–19.6
1,000 10.2–14.8 26.8–45.1 5.0–7.9 16.1–19.1
3,500 37.8–49.6 89.4–110.4 7.5–9.7 17.4–29.6

After the fix, appends cost the same at every size; what remains is the history fsync (~8–10 ms). The host was shared with other builds, so absolute numbers are noisy. The trend is not.

Gates (worktree, -j4)

  • cargo fmt --all --check: rc=0
  • cargo clippy --workspace --all-targets -- -D warnings: rc=0
  • cargo test --workspace: rc=0, 456 passed, 0 failed, 2 ignored (all notes_crash*, kill_nine_*, live-typing, writers and isolation tests included)

Follow-up (not in this PR)

save.rs::sync_title still forks base_state on every save to read the last saved text: the same quadratic copy, once per save rather than once per append. Caching that text would fix it.

Fix is up in PR #529 (not merged). ## Root cause To append, the room copied the whole note (`ydoc::fork(state)`) only to learn where the text ends: Yjs puts new text after the last piece of text in the document, deleted or not, and yrs keeps that piece private. Building the copy re-integrates every piece, and yrs does this about quadratically when many writers chain off each other. Every keyed append is its own writer (the writer id comes from the idempotency key), so a log is exactly that shape. Measured on the copy alone: 0.1 ms at 100 lines, 2.4 ms at 1k, 15–26 ms at 3.5k. ## Fix The room remembers where its last append ended, plus the document's state vector at that moment (`room/tail.rs`). If the state vector is unchanged, the document has exactly the same pieces in the same order, so the next append is built directly as a one-item Yjs update (`ydoc::append_update`) and applied to the live doc. Deletes don't move the end. Anything that adds text (typing, outside edits, the server's keys at save) changes the state vector, and that one append takes the old copy path, which finds the end again. What stays the same: - **Text and merge behaviour.** The applied change is byte-identical to the old path. A new fuzz test (40 seeds × 120 steps) checks this while typists insert, delete (often at the very end) and deliver late and out of order. A mutation check (dropping the state-vector guard) makes that test fail. - **Crash safety and idempotency.** Same key-derived writer, same "writer already in the doc" check, same history write before the answer. - **History and versions.** Same rows (the same applied bytes) and the same version numbers. Patch and Replace are untouched. ## Tests - `tests/appends.rs`: - byte-for-byte equivalence fuzz; - typists at the end plus keyed appends with retries in a real room (every word exactly once, clients converge, history replays to the note, file = note); - an append to a 2,000-line note stays small. - `notes_crash.rs::kill_nine_mid_append_on_a_long_note`: 4 concurrent writers append to a 1,000-line note, and the server gets 12 SIGKILLs. Unconfirmed appends are retried, and so are some confirmed ones. Each confirmed append is there exactly once, the file is never torn, and the file equals the note after a save. - `examples/append_bench.rs`: append p50/p95 at 100, 1k and 3.5k lines. ## Bench (`append_bench`, durable keyed appends, local SSD, 3 alternating runs, 200 samples per size) | lines | before p50 | before p95 | after p50 | after p95 | |---:|---:|---:|---:|---:| | 100 | 10.9–15.1 | 28.2–28.9 | 9.3–9.9 | 18.1–19.6 | | 1,000 | 10.2–14.8 | 26.8–45.1 | 5.0–7.9 | 16.1–19.1 | | 3,500 | 37.8–49.6 | 89.4–110.4 | 7.5–9.7 | 17.4–29.6 | After the fix, appends cost the same at every size; what remains is the history fsync (~8–10 ms). The host was shared with other builds, so absolute numbers are noisy. The trend is not. ## Gates (worktree, `-j4`) - `cargo fmt --all --check`: rc=0 - `cargo clippy --workspace --all-targets -- -D warnings`: rc=0 - `cargo test --workspace`: rc=0, 456 passed, 0 failed, 2 ignored (all `notes_crash*`, `kill_nine_*`, live-typing, writers and isolation tests included) ## Follow-up (not in this PR) `save.rs::sync_title` still forks `base_state` on every save to read the last saved text: the same quadratic copy, once per save rather than once per append. Caching that text would fix it.
Author
Owner

Closing during the 26 Oct handover cleanup: The append fix landed in dev (PRs #529, #574).

Closing during the 26 Oct handover cleanup: The append fix landed in dev (PRs #529, #574).
kayg closed this issue 2026-10-26 08:46:36 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
kayg/calternal#527
No description provided.