Skip to content

untracked-cache: skip empty subtrees with fsmonitor - #2319

Open
letmaik wants to merge 1 commit into
gitgitgadget:masterfrom
letmaik:untracked-cache-skip-empty-subtrees
Open

letmaik wants to merge 1 commit into
gitgitgadget:masterfrom
letmaik:untracked-cache-skip-empty-subtrees

Conversation

@letmaik

@letmaik letmaik commented Nov 19, 2026 •

Copy link
Copy Markdown

Timings were taken on ext4 under Linux (WSL2) with a warm cache, as
pairs of "git status --porcelain" runs of master and this patch,
alternating which ran first. The master and patched columns report
each binary's median time. The untracked cache was built for the
mode shown, and each timed output matched a run without fsmonitor or
the untracked cache.

                                        master  patched

linux.git 4405c18c95d2 (97k files, 6.3k directories),
fsmonitor--daemon, 93 pairs:
normal 34.1ms 26.6ms
all 31.9ms 25.8ms
normal, untracked file in each leaf 55.9ms 57.1ms
all, untracked file in each leaf 55.9ms 55.4ms
Chromium 6a30b86e674f (519k files, 47k directories),
fsmonitor--daemon, 33 pairs:
normal 438.9ms 136.9ms
all 442.8ms 138.5ms
normal, untracked file in each leaf 606.5ms 598.9ms
all, untracked file in each leaf 608.6ms 604.4ms
t/perf/repos/many-files.sh -d 5 -w 10 -f 9
(1M files, 111k directories), hook, 33 pairs:
normal 246.5ms 216.9ms

In the table, "hook" means a static v2 hook that reports no
changes. In the daemon runs, trace2's "directories-visited" count
went from 6,338 to 1,809 in linux.git and from 46,871 to 4,687 in
Chromium. The "untracked file in each leaf" runs added one file to
the parent directory of every tracked path: 5,023 directories in
linux.git and 29,826 in Chromium, including every leaf of both
trees. There the count went from 6,338 to 6,317 in linux.git and
stayed at 46,871 in Chromium.

The ignore-pattern tree was made with

sh t/perf/repos/many-files.sh -r repo -d 6 -w 8 -f 3
cd repo && git checkout p0006-ballast

followed by a top-level .gitignore with N patterns that match
nothing (a mix of ".extN", "buildN/", "/outN", "**/cacheN" and
"genN/
/obj"), a .gitignore with "*.tmp" and "/local/" in every
directory whose path ends in "dir1/dir1" at depths 3 to 6 below
ballast/ (4,680 files), and one untracked file at the top. It has
300k directories; directories-visited went from 299,594 to 12,874
for every N. Normal mode, static hook, 33 pairs:

patterns in .gitignore 0 50 300 1,000
master 556ms 1.09s 3.70s 10.95s
patched 477ms 0.50s 0.60s 0.90s

With modified files, the saving stayed between 237ms and 316ms in
Chromium and about 3.1s in the 300-pattern tree. In separate
normal-mode runs, N tracked files were modified and the hook
reported them on every invocation. In the traced run of each case,
both binaries reopened directories and wrote the index. Median
paired savings:

modified files 1 10 100 1,000
Chromium 295ms 307ms 316ms 237ms
300-pattern tree 3.13s 3.16s 3.11s 3.18s

The existing p7519 tests do not measure this case well. perf-lib
writes its timing files in the worktree, which can invalidate the
root directory and add an index write to the timed command.

@letmaik
letmaik force-pushed the untracked-cache-skip-empty-subtrees branch from 8de05c9 to 5ec89f7 Compare November 20, 2026 01:47
With fsmonitor and the untracked cache enabled, a whole-tree "git
status" still walks the cached directory tree, even when fsmonitor
reports no changes.  For each directory whose cached data it
accepts, valid_cached_dir() calls prep_exclude(), and the walk calls
treat_path() for each entry it reads.  In a large tree with few
untracked files, most of this walk adds nothing to the result, but
its cost grows with the number of directories.

The walk cannot stop at a directory just because fsmonitor left it
valid.  The "valid" bit only covers the directory's own entries, and
an untracked file may be cached further down.

Instead, after refresh_fsmonitor() has applied the fsmonitor
response, make one pass over the cache and mark each directory that
is valid, is not a check-only entry, and has no cached untracked
entries and no recorded .gitignore, if every subdirectory that
read_cached_dir() would recurse into is marked too.  In a scan that
is not check-only, read_directory_recursive() then returns early for
a marked directory once open_cached_dir() has accepted its cached
data.  Skipping the walk loses no paths.  Outside check-only scans,
its return value is only passed up to read_directory(), which
discards it.  Check-only scans are left alone, as their callers use
the result to classify a directory.  Entries cached by a check-only
scan are not marked either, because such a scan can stop before it
has read the whole directory.

A directory with a recorded .gitignore is never marked, and neither
is any directory that the walk passes through to reach it, so
prep_exclude() still reads the same .gitignore files as before.

The mark is computed and used only while use_fsmonitor is set.
Without fsmonitor, valid_cached_dir() must lstat() a directory
before trusting its cached contents.  refresh_fsmonitor() clears
use_fsmonitor when the query fails or returns a trivial response,
and then nothing is marked.  The mark is kept in memory only, as
storing it in the UNTR extension would change the index format.

With fsmonitor--daemon and a warm cache, "git status --porcelain"
went from 34.1ms to 26.6ms in linux.git, and from 439ms to 137ms in
Chromium, where the walk visited 4,687 directories instead of
46,871.  In a synthetic tree of 300k directories with 300 patterns
that match nothing in the top-level .gitignore, and a hook that
reports no changes, "git status" went from 3.70s to 0.60s.  In that
tree, each directory the walk no longer enters saves a scan of those
patterns.

When there is little to skip, the marking pass only adds time.  With
an untracked file in every leaf directory of linux.git and Chromium,
and in some other directories, the walk visited 21 fewer directories
in linux.git and just as many in Chromium.  Across both repositories
and both modes, median paired changes ranged from -1.3% to +2.3%.
In the two Chromium traces, the marking pass took about 1.2ms.
In small synthetic trees of 4,705 directories with untracked files
in most or all leaves, the largest median paired slowdown was 2.3%
in normal mode and 3.5% (0.25ms) in all mode.

Add tests to t7519 that compare status output with an uncached run
after new paths, index changes and ignore changes, and with fsmonitor
disabled, a failing hook or a trivial response.  Another test checks
that unchanged empty subtrees are skipped.

Signed-off-by: Maik Riechert <letmaik@outlook.com>
@letmaik
letmaik force-pushed the untracked-cache-skip-empty-subtrees branch from 5ec89f7 to e2c91ae Compare November 20, 2026 05:48
@letmaik

letmaik commented Nov 20, 2026

Copy link
Copy Markdown
Author

/preview

@gitgitgadget

gitgitgadget Bot commented Nov 20, 2026

Copy link
Copy Markdown

Preview email sent as pull.2319.git.1795153926126.gitgitgadget@gmail.com

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