Skip to content

perf(app): stay responsive with ~100k-row WAL histories - #25594

Merged
Git-on-my-level merged 4 commits into
mainfrom
lane/wal-scale
Nov 18, 2026
Merged

Git-on-my-level merged 4 commits into
mainfrom
lane/wal-scale

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Problem

A field phone holding 92,763 WAL rows (90,889 synced history, ~3.6k pending) froze for 1.2–2.3 s every few seconds and hit "stopped responding", even in airplane mode with the pendant off. Its debug log shows mobile_render_diagnostic worst frames of 1.2–2.3 s alongside a WAL flush every ~5–10 s.

Each flush publishes a new inventory list. SyncProvider.derivedWals (and the WalDerivedSyncViews stand-in mixin) then disposed and reconstructed WalDerivedViews over every row. That is a 10-bucket _apply, a HashSet per id and a listener registration for each of ~92k rows, all on the UI isolate.

Fix

  • WalDerivedViews.sync(rows) adopts a new snapshot in place:

    • Append-only change (the capture case): recognised with pointer comparisons; only the appended rows are derived.
    • General case: an identity pass. Added rows are derived and removed rows unwound (listener, dirty set, counts, hidden twins, buckets, id index, cached selectors). A relative reorder renumbers and invalidates the order-sorted lists.
    • The fast path compares against a private pointer copy of the last snapshot, so a caller editing a list it passed earlier cannot fake an unchanged prefix.
  • Construction is cheaper:

    • Bucket hash work is skipped where membership didn't change.
    • Single-row ids are stored without a per-row HashSet.
    • _ViewEntry reuses one syncDisplayState read for both the display state and the attention bucket.
  • Newest-first lists break timerStart ties by source order, so selectors never depend on update history. Dart's sort is not stable, and tie order was already history-dependent before this change.

  • Wal.id is cached and cleared by the device / timerStart setters, its only inputs.

  • LocalWalSyncImpl:

    • Counts pending disk WALs without copying the inventory on every flush.
    • Skips the live-proof key pass when _liveProofPendingWalIds is empty.
    • Skips the loaded-key set at startup when no capture rows wait.

    These are loop guards only: no change to which rows are written, retained or counted.

Measurements

Benchmark: 89k synced + 3.6k pending rows, real LocalWalSyncImpl with the delta index, flutter test debug JIT on a host at load average ~300. Absolute numbers are inflated; compare the ratios.

Path Before After
Derived views per inventory update, longest synchronous block 1.0–3.0 s 11–14 ms
WalDerivedViews construction, 92.6k rows (once per session) ~1.4 s ~0.33 s
Dispose on every update 0.1–0.6 s none

Tests

  • New test/unit/wal_derived_views_sync_test.dart:
    • 20 seeded random walks of 300 steps each: append, remove, device-row insert, shuffle, same-id replacement, in-place status, id and sort-key edits, and duplicate listing. Every selector, count, day total, refusal summary and rowsForIds result is compared against a fresh WalDerivedViews over the same list.
    • Plus cases for in-place list edits, listener teardown of removed rows, and "appending to a 20k synced history reads no retained row and keeps the unchanged bucket's sorted list".
  • Existing suites: 98 WAL / sync / perf test files, 1,011 tests pass. bash test.sh test/perf/production_shell_gate_test.dart test/perf/global_sync_inventory_test.dart test/unit/wal_derived_views_sync_test.dart passes.

Not in this PR (measured, disclosed)

  • WalIndexWorker index saves still take seconds per added row on a 92k index. They run off the UI isolate but compete for background CPU.
  • Some UI paths still scan the full list while their screen is open:
    • SyncDetailsSheet (SyncPipelineAmounts.of(allWals))
    • the Auto Sync run tracker
    • displaySortedWals re-sort after a membership change
    • openOfflineSyncDetails twin set, built per tap
  • Synced-copy retention: existing installs have autoRemoveSyncedCopies pinned off by migration, and syncedAt == 0 rows are never swept. That is how phones reach ~100k rows. This is a product decision and is not changed here.
  • Overlaps fix: make sync backlog and Bluetooth drop counts truthful #25582 (WalDerivedViews constructor / pendingWork), which changes the same file without semantic overlap. Whichever lands second rebases.

Product invariants affected

  • INV-CAP-1 (path match only): display derivation and lookup caching. No WAL status, file, retry, deletion or upload decision changes.

Failure-Class: none
UI-Evidence: none

🤖 Generated with Claude Code

…ries

A phone holding ~92k WAL rows (90k synced history) froze for 1-2 s every
few seconds while capturing: each flush published a new inventory list, and
SyncProvider disposed and rebuilt WalDerivedViews over every row.

WalDerivedViews.sync() now adopts a new snapshot with identity checks only:
an append-only change is recognised with pointer comparisons, and full
per-row derivation runs only for added or removed rows. Retained rows keep
their bucket caches; a reorder renumbers and invalidates the order-sorted
lists. Newest-first lists break timestamp ties by source order, so results
never depend on update history.

Also: cache Wal.id (cleared by its two setters), count pending disk WALs
without copying the inventory, skip the live-proof key pass when nothing is
pending, and skip the loaded-key set at startup when no capture rows wait.

Local benchmark, 89k synced + 3.6k pending rows, debug JIT on a loaded host:
per-update derived-view block 1.0-3.0 s -> 11-14 ms; construction
~1.4 s -> ~0.33 s. WAL status, files, retry, deletion and upload decisions
are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-18T06:53:22.978686Z
We couldn’t complete this review. Comment @codex review to try again.
cdab3eb New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Git-on-my-level and others added 3 commits November 17, 2026 17:58
… path

Review follow-ups for the incremental derived views: the append fast path
compared against the caller's list, which a caller could edit in place;
keep a pointer copy instead. Tests pin the fast path, Wal.id invalidation
on device/timerStart changes, and dispose comparison views promptly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@Git-on-my-level
Git-on-my-level merged commit a00517c into main Nov 18, 2026
48 checks passed
@Git-on-my-level
Git-on-my-level deleted the lane/wal-scale branch November 18, 2026 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant