8000 perf(xerj-autoindex): O(1) journal state writes and per-replay lookups (#1224 client CPU) by xerj-org · Pull Request #1228 · xerj-org/xerj · GitHub
[go: up one dir, main page]

Skip to content

perf(xerj-autoindex): O(1) journal state writes and per-replay lookups (#1224 client CPU) - #1228

Merged
xerj-org merged 2 commits into
mainfrom
fix/journal-opstate-clone
Oct 7, 2026
Merged

xerj-org merged 2 commits into
mainfrom
fix/journal-opstate-clone

Conversation

@xerj-org
@xerj-org xerj-org commented Oct 7, 2026
Copy link
Copy Markdown
Owner

What

The #1224 re-measure (after both delete gates, #1225 client + #1227 server) still ran the cve-records apply phase at 1.18 items/s, the client pinning one core in pure user time at 5.7 GB RSS. The original per-file client-CPU claim in #1224 was right; the first gdb probe's parked delete stacks had masked it. This PR removes the client-side cost.

Root cause

Journal::sync_operation_state runs twice per replayed operation (Started + Committed). Each call cloned the entire PendingSync — desired manifest with all 402,695 groups, the 402,744-entry operations Vec, the growing operation_states map — to append one small durable line afterward. Two full deep clones per file.

Behind it, three more quadratics over the same corpus run:

Site Cost per op
state.rs sync_operation_state clone ~350 ms × 2/op (dominant)
sync.rs apply_operation_state membership iter().any() O(402k) × 2/op, and × 2 per event on every journal load-replay
sync_executor.rs apply_shared group resolution O(402k) scans per op
sync_executor.rs replay_prepared artifact resolution O(402k files) scan per upsert

Change

  • sync_operation_state: validate (pure) → durable append → record in place. The journal line is the transaction boundary; a crash between append and record loses nothing (load replay rebuilds from the file), and a failed append returns before any mutation — the same guarantee the clone-on-write gave.
  • PendingSync gains a lazily-built, never-serialized OperationIdIndex (HashSet + len guard) so membership is O(1) live and on load replay.
  • EsSyncBackend builds a ReplayLookup (group_id / content_id → position for base, desired, snapshot) once per replay_operations; hits are verified against the slice they answer from, length-guarded, scan fallback outside a replay.

Event format, transition rules and observable journal behavior are unchanged. Pattern reference (approach only): redb's InMemoryState, page_manager.rs:252 — durable structure is the truth, in-memory state a derived cache (Apache-2.0/MIT).

Numbers

cve-records, 402,695 files / 1.9 GB prepared, isolated rc.89+gate node on :19285:

Tests

  • New: operation_state_split_semantics_and_membership_index_hold (split semantics, pre-split transition rules, clone-carried index, len guard).
  • cargo test --release -p xerj-autoindex --lib: 1299 passed / 0 failed.
  • Existing journal failpoint suites unchanged (same events, same order).

#1224's original report measured ~0.66 s of client CPU per file during
cve-records apply. The client no-op delete gate (#1225) and the
server-side no-flush gate (#1227) removed the delete_by_query storm, but
the re-measure still ran at 1.18 items/s with the client pinning one
core in pure user time (82% of a core, 0 kernel ticks) at 5.7 GB RSS.
The original per-file CPU claim was right for a reason nothing had
profiled yet: the parked delete stacks of the first gdb probe masked it.

Root cause: Journal::sync_operation_state runs twice per replayed
operation (Started + Committed), and each call cloned the ENTIRE
PendingSync - desired GenerationManifest with all 402,695 groups, the
402,744-entry operations Vec, and the growing operation_states map -
before appending one small durable line. Two full deep clones per file,
plus three more quadratics hiding behind them:

- PendingSync::apply_operation_state validated membership with a linear
  operations.iter().any() per state event, both live and again on every
  journal load-replay (2 events per operation).
- EsSyncBackend::apply_shared resolved each operation's group with
  base/desired groups.iter().find() over every group, per operation.
- replay_prepared resolved each content_id with a linear scan of the
  snapshot's 402k files, per upsert.

Fix, all client-side, event format and semantics unchanged:

- sync_operation_state: validate (pure) -> durable append -> record in
  place. The journal line is the transaction boundary; a crash between
  append and record loses nothing because load replay rebuilds the map
  from the file, and a failed append returns before any mutation, the
  same guarantee the clone-on-write provided.
- PendingSync carries a lazily-built, never-serialized OperationIdIndex
  (HashSet + len guard) so membership is O(1); apply_operation_state is
  now validate+record so load replay shares the fast path.
- EsSyncBackend builds a ReplayLookup (group_id and content_id to
  position maps for base, desired and snapshot) once per
  replay_operations call. Hits are verified against the slice they
  answer from and length-guarded, so a stale or absent cache falls back
  to the exact scan it replaces.

Pattern reference (approach, not code): redb's InMemoryState - the
durable structure is the truth, in-memory state is a derived cache
updated after the durable commit and rebuilt on load
(redb src/tree_store/page_store/page_manager.rs:252, Apache-2.0/MIT).

Measured, cve-records (402,695 files, 1.9 GB prepared) against the
isolated rc.89-gate node on :19285: apply phase 1.18 items/s before
this change (0.72 s client CPU per file); after-numbers land on #1224
as the resumed run reaches the apply phase.

Tests: operation_state_split_semantics_and_membership_index_hold pins
the validate/record split, the pre-split transition rules, the
clone-carried index, and the len guard against stale-shape answers.
Full xerj-autoindex lib suite 1299 passed / 0 failed.

Files:
- engine/crates/xerj-autoindex/src/state.rs :: sync_operation_state
- engine/crates/xerj-autoindex/src/sync.rs :: PendingSync,
  OperationIdIndex, validate/record_operation_state
- engine/crates/xerj-autoindex/src/sync_executor.rs :: ReplayLookup,
  EsSyncBackend::{apply_shared, replay_prepared, replay_metadata,
  replay_operations}
…test

CI clippy runs --workspace --all-targets -D warnings; the two
operation_states.get(..).is_none() asserts in the new test trip
unnecessary_get_then_check. contains_key says the same thing.

Follow-up to cc5f3a9 on the same branch.
@xerj-org
xerj-org merged commit e260e07 into main Oct 7, 2026
22 checks passed
thomas-villani pushed a commit to thomas-villani/xerj that referenced this pull request Oct 9, 2026
Workspace version + Cargo.lock (17 member entries). Release window:
xerj-org#1219, xerj-org#1223, xerj-org#1225, xerj-org#1227, xerj-org#1228.
thomas-villani pushed a commit to thomas-villani/xerj that referenced this pull request Oct 9, 2026
Window: xerj-org#1219 (Vinz2168 field report, closes the xerj-org#1091 tracker it
diagnoses), xerj-org#1223 (kNN exact-scan capture bracket, xerj-org#1220), xerj-org#1227
(no-match by-query must not flush, xerj-org#1222), xerj-org#1225 (visibility probe
gates the per-group delete), xerj-org#1228 (O(1) journal state writes, the
xerj-org#1224 client-CPU leg).

Release-notes gate: PASS locally (release-notes-gate.sh against
origin/main) after the gate itself caught the xerj-org#1224 claims-it-whole
drift in the first draft - the entry now says the tracker closes on
the completed re-measure, not on the legs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0