Skip to content

perf(main): gate the git-common worktrees readdir while visible and idle - #9882

Closed
nwparker wants to merge 3 commits into
nwparker/perf-s3-poller-gatefrom
nwparker/perf-git-common-idle-gate
Closed

nwparker wants to merge 3 commits into
nwparker/perf-s3-poller-gatefrom
nwparker/perf-git-common-idle-gate

Conversation

@nwparker

@nwparker nwparker commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

What

Stacked on #9849 (base = nwparker/perf-s3-poller-gate). #9849 parks the worktree pollers while the window is hidden; this trims their cost while the window is visible and idle.

The non-darwin (Windows/Linux) git-common poll re-enumerated the linked-worktrees directory (readdir) on every 2s tick even when no linked worktree was added or removed. This gates that readdir on the worktrees/ directory signature: it only re-enumerates when a linked worktree is actually added/removed (or on the 15-tick backstop). Everything else — the worktrees/ stat, each known entry's dir stat, all structural leaves (HEAD, gitdir, locked, config.worktree) and logs/HEAD, and the primary-checkout leaves — is still stat'd every tick, exactly as before, so detection latency is unchanged. index keeps its pre-existing 15-tick gate.

ELI5

While Orca sat open and idle, it kept re-listing each repo's worktree folder twice a second even when no worktree was added or removed. Now it only re-lists when the folder actually changes; it still checks each worktree's HEAD/ref-log/metadata every tick, so nothing is noticed any slower.

Proof of zero regression

Scope corrected after adversarial review (which caught a real regression in the first cut): the initial version also gated the structural leaves behind the entry-dir signature, which would have deferred an in-place HEAD/gitdir/config.worktree rewrite (that doesn't bump the entry-dir mtime) to the ~30s backstop. Fixed by re-statting all structural leaves + logs/HEAD every tick (matching the pre-idle-gate poller) and gating only the readdir.

  • Structural leaves + logs/HEAD + primary leaves are re-stat'd every tick → an in-place HEAD/branch/ref change surfaces within one tick, identical to before. New tests assert an in-place HEAD rewrite (no entry-dir change) is detected within a tick without triggering the gated readdir.
  • A worktree add/remove bumps the worktrees/ signature (now including size, so a same-granule coarse-timestamp change can't slip) → readdir re-runs → detected; the 15-tick backstop covers pathological cases; resume-after-hidden forces a full scan.
  • macOS uses its native narrow watcher (untouched); this is the non-darwin path only.
  • Tests: 53 pass across the three watcher test files. typecheck:node / oxlint clean.

Proof of perf improvement

While visible and idle, skips one worktrees/ readdir per repo per 2s tick (each enumerating every linked worktree with withFileTypes), plus its per-entry Dirent processing — ~10 readdirs/s eliminated at 20 repos, more as linked-worktree counts grow. A smaller, provably-safe subset of the original idea (the structural-stat skipping was reverted as unsafe).

@nwparker
nwparker force-pushed the nwparker/perf-git-common-idle-gate branch from 589bee4 to 1588043 Compare July 22, 2026 07:08
@nwparker

Copy link
Copy Markdown
Contributor Author

Adversarial review caught a real MEDIUM regression in the first cut and I fixed it: gating the structural leaves (HEAD/gitdir/locked/config.worktree) behind the entry-dir signature would defer an in-place HEAD rewrite (which doesn't bump the entry-dir mtime) to the ~30s backstop instead of 1 tick. Now all structural leaves + logs/HEAD are re-stat'd every tick (matching the pre-idle-gate poller); only the worktrees/ readdir is gated, and size is back in the dir signature to close a coarse-granularity gap. Added a non-vacuous test (plain in-place HEAD rewrite, no utimes) asserting detection within one tick without a full scan. The win is smaller but provably zero-regression. 53 watcher tests pass.

@nwparker nwparker changed the title perf(main): skip the git-common fan-out while visible and idle perf(main): gate the git-common worktrees readdir while visible and idle Jul 22, 2026
@nwparker
nwparker force-pushed the nwparker/perf-s3-poller-gate branch from f784f65 to a90d5a5 Compare July 22, 2026 23:00
@nwparker
nwparker force-pushed the nwparker/perf-git-common-idle-gate branch 3 times, most recently from c95316c to 934fdd3 Compare July 22, 2026 23:26
@nwparker
nwparker force-pushed the nwparker/perf-s3-poller-gate branch from 4dedb21 to 16455fd Compare July 22, 2026 23:27
@nwparker
nwparker force-pushed the nwparker/perf-git-common-idle-gate branch from 934fdd3 to f69788c Compare July 22, 2026 23:27
@nwparker
nwparker force-pushed the nwparker/perf-s3-poller-gate branch from 16455fd to a8f397a Compare July 23, 2026 00:49
nwparker and others added 3 commits July 22, 2026 17:49
…FS add/remove

The worktrees-dir readdir was gated on the dir's mtime:ctime:ino:size signature, so on
a coarse-mtime/FAT filesystem a same-granule add+remove (all four fields collide) went
undetected until the ~30s index backstop. A single readdir of a small dir is negligible
next to the per-entry structural stats that already run every tick, so always enumerate —
the listing is the authoritative add/remove signal. The expensive per-entry index read
stays gated on each entry's own dir signature; onFullScan now reflects the ungated
index-metadata backstop fan-out (the real periodic cost) rather than the readdir.

Co-authored-by: Orca <[email protected]>
…on readdir failure

Follow-up to always-enumerating the worktrees dir: the readdir catch-all treated ANY
error as an authoritative empty listing, so a transient failure (EIO/ESTALE/EMFILE,
network/SSH hiccup) emitted a false delete for every linked worktree (and a false create
next tick) — and enumerating every tick widened that exposure. Only ENOENT (dir truly
absent) now yields an empty listing; other errors retain the known entries so per-entry
stats still run and a real removal surfaces as that entry's own stat miss. Adds a
regression test (readdir → ENOTDIR) asserting no false delete.

Co-authored-by: Orca <[email protected]>
@nwparker
nwparker force-pushed the nwparker/perf-git-common-idle-gate branch from f69788c to ce0a64e Compare July 23, 2026 00:49
@nwparker
nwparker deleted the branch nwparker/perf-s3-poller-gate July 23, 2026 03:38
@nwparker nwparker closed this Jul 23, 2026
nwparker added a commit that referenced this pull request Jul 23, 2026
…re-open of #9882) (#10074)

* perf(main): gate idle git-common polling

Co-authored-by: Orca <[email protected]>

* fix(main): enumerate git-common worktrees every tick to catch coarse-FS add/remove

The worktrees-dir readdir was gated on the dir's mtime:ctime:ino:size signature, so on
a coarse-mtime/FAT filesystem a same-granule add+remove (all four fields collide) went
undetected until the ~30s index backstop. A single readdir of a small dir is negligible
next to the per-entry structural stats that already run every tick, so always enumerate —
the listing is the authoritative add/remove signal. The expensive per-entry index read
stays gated on each entry's own dir signature; onFullScan now reflects the ungated
index-metadata backstop fan-out (the real periodic cost) rather than the readdir.

Co-authored-by: Orca <[email protected]>

* fix(main): don't fabricate worktree deletions on a transient git-common readdir failure

Follow-up to always-enumerating the worktrees dir: the readdir catch-all treated ANY
error as an authoritative empty listing, so a transient failure (EIO/ESTALE/EMFILE,
network/SSH hiccup) emitted a false delete for every linked worktree (and a false create
next tick) — and enumerating every tick widened that exposure. Only ENOENT (dir truly
absent) now yields an empty listing; other errors retain the known entries so per-entry
stats still run and a real removal surfaces as that entry's own stat miss. Adds a
regression test (readdir → ENOTDIR) asserting no false delete.

Co-authored-by: Orca <[email protected]>

---------

Co-authored-by: Orca <[email protected]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…re-open of stablyai#9882) (stablyai#10074)

* perf(main): gate idle git-common polling

Co-authored-by: Orca <[email protected]>

* fix(main): enumerate git-common worktrees every tick to catch coarse-FS add/remove

The worktrees-dir readdir was gated on the dir's mtime:ctime:ino:size signature, so on
a coarse-mtime/FAT filesystem a same-granule add+remove (all four fields collide) went
undetected until the ~30s index backstop. A single readdir of a small dir is negligible
next to the per-entry structural stats that already run every tick, so always enumerate —
the listing is the authoritative add/remove signal. The expensive per-entry index read
stays gated on each entry's own dir signature; onFullScan now reflects the ungated
index-metadata backstop fan-out (the real periodic cost) rather than the readdir.

Co-authored-by: Orca <[email protected]>

* fix(main): don't fabricate worktree deletions on a transient git-common readdir failure

Follow-up to always-enumerating the worktrees dir: the readdir catch-all treated ANY
error as an authoritative empty listing, so a transient failure (EIO/ESTALE/EMFILE,
network/SSH hiccup) emitted a false delete for every linked worktree (and a false create
next tick) — and enumerating every tick widened that exposure. Only ENOENT (dir truly
absent) now yields an empty listing; other errors retain the known entries so per-entry
stats still run and a real removal surfaces as that entry's own stat miss. Adds a
regression test (readdir → ENOTDIR) asserting no false delete.

Co-authored-by: Orca <[email protected]>

---------

Co-authored-by: Orca <[email protected]>
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