Skip to content

Stop watching folders git ignores - #98

Merged
rocketraman merged 1 commit into
masterfrom
prune-ignored-watches
Dec 2, 2026
Merged

rocketraman merged 1 commit into
masterfrom
prune-ignored-watches

Conversation

@rocketraman

Copy link
Copy Markdown
Owner

Stops watching the folders git ignores, such as build/ and node_modules/. Each repo is currently one recursive watch on its folder, so a build's output is walked when the watch is registered, and every write a build makes there arrives as an event. One field report traced 97% of a repo's events to its build/. Those events are already dropped before they trigger a rescan, but the folder is still walked and watched.

Stacked on the Nucleus 3.0.0 update, which this depends on.

What changes

Watch set (WatchRoots.kt, AppState.kt). A repo with ignored folders is watched as:

  • a shallow watch on the repo folder, which sees top-level files and folders appearing or going;
  • a recursive watch on each top-level folder that git doesn't ignore, or that has tracked files in it (an ignored .idea/ with a shared run configuration is still watched);
  • .git always, because it's how commits, checkouts and fetches are noticed.

A repo that ignores nothing keeps the single recursive watch. Symlinked folders are skipped, as the recursive watch never followed them. git is asked before anything is registered, so an ignored folder is never walked, not even at startup.

Why this is safe now. This was tried before and withdrawn. Each registration created its own inotify instance, from a limit shared with every process on the machine, and 36 repos used 258 of 512. Nucleus 3.0.0 shares one native watcher across all of a watcher's registrations (NucleusFramework/Nucleus#571), and the app uses one watcher. WatchInstanceCost pins this against the real watcher on Linux: it counts inotify instances in /proc/self/fd and fails if 12 registrations take more than one.

Following layout changes (WatchPolicy.ignoresStale, AppState.refreshIgnoredDirs). git is asked again after:

  • a new top-level entry that the watcher reports as a directory, or can't say what it is;
  • any event for a known top-level folder, such as it going or being renamed;
  • an event inside a top-level folder that wasn't there when git was last asked;
  • a change to .gitignore or .git/info/exclude.

Only the registrations that differ are added or closed, and new ones are added before old ones close. The watcher's isDirectory flag is now passed to ignoresStale (AppState.eventIsDirectory), so saving a top-level file doesn't ask git anything.

Removed. The event filter for ignored folders (WatchPolicy.inIgnoredDirs, and the root/ignored parameters of actionFor). Nothing inside those folders is watched any more, so the filter could never apply.

Verification

  • ./gradlew check: 306 tests, 0 failures. The new inotify test was checked to be able to fail: with a temporary probe, 12 registrations on one watcher cost 1 instance, and 12 separate watchers cost 12. It reads /proc, so it only asserts on Linux; on macOS and Windows it returns without checking anything.

  • Live run in an isolated KWin session against a scratch repo with build/ ignored, reading the process's inotify watches from /proc:

    Check Result
    inotify instances 1
    Watches 20: every folder except build/ and build/classes
    mkdir app app/ gained a watch within 3s; a file in it showed as "1 untracked" on the dashboard at once
    New folder under build/ Stayed unwatched
    Removing app/ Its watch was dropped
    Editing src/App.kt Showed as "1 modified"
    Watcher errors in stderr or the diagnostics log None

Reviewer notes

  • Registration now asks git before registering: WatchRoots.ignoredDirs is two git runs per repo, run in turn under watchMutex at startup. Before, the same query ran after registering. Timed across 45 real tracked repos (warm cache): 116ms in total, 7ms at most for one repo, off the UI thread. 39 of the 45 have ignored folders that are now skipped instead of walked.
  • Only top-level folders are pruned. An ignored folder deeper down, such as a node_modules per monorepo package, is still walked; finding those would mean the walk this avoids.
  • A file written into a brand-new top-level folder before git has been asked again can be missed by the live refresh. The folder's own creation event still triggers a rescan, and the regular poll covers the rest.

@rocketraman
rocketraman added this pull request to stack #99 December 2, 2026 16:40
Base automatically changed from nucleus-3.x to master December 2, 2026 16:42
A repo's watch is one recursive registration on its folder, so a build's
output is walked at registration and every write a build makes there
arrives as an event. One field report traced 97% of a repo's events, and
half of all the app's rescans, to its build/ folder. Those events are
dropped before they reach the debounce, but the folder is still walked
and watched.

Each repo is now watched as a shallow watch on its folder, plus a
recursive watch on each top-level folder that git doesn't ignore or
that has tracked files in it. .git is always watched. A repo that
ignores nothing keeps the single recursive watch. git is asked before
anything is registered, so an ignored folder is never walked.

This was tried before and withdrawn because every registration created
its own inotify instance: 36 repos took 258 of the machine-wide 512.
Nucleus 3.0.0 shares one native watcher across all of a watcher's
registrations (NucleusFramework/Nucleus#571). A test now pins that
against the real watcher by counting inotify instances in /proc.

The watched set follows the repo's layout. The shallow watch reports a
top-level folder appearing, going or being renamed, and a change to
.gitignore or .git/info/exclude. git is asked again, and only the
registrations that differ are added or closed. 3.0.0 events say whether
a path is a directory, so saving a top-level file does not ask git
anything.

The event filter for ignored folders is removed: nothing inside them is
watched any more, so it could never apply.

Signed-off-by: Raman Gupta <rocketraman@gmail.com>
@rocketraman
rocketraman force-pushed the prune-ignored-watches branch from 41f2794 to 22944b7 Compare December 2, 2026 16:42
@rocketraman
rocketraman merged commit 188ddfa into master Dec 2, 2026
@rocketraman
rocketraman deleted the prune-ignored-watches branch December 2, 2026 16:43
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