Skip to contentSkip to conversation

dir: skip prep_exclude() when fsmonitor is trusted

PATCH New Stepan Koltsov via GitGitGadget 1 participant 1 message Nov 22

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dir: skip prep_exclude() when fsmonitor is trusted

From: Stepan Koltsov <[email protected]>

"git status" calls prep_exclude() for every directory in the untracked cache, to drop a cache entry if its .gitignore rules changed. That matches every directory against the exclude patterns and reads every .gitignore. With fsmonitor it is unnecessary:
fsmonitor already drops the cache entry when anything in the directory changes, including .gitignore.

Skip the call in valid_cached_dir() when fsmonitor is on and the cache entry is still valid.

One case is left: a .gitignore is edited while the monitor is not watching, for example while the daemon is stopped. The monitor then sends a trivial response. Since 1cf98cb4d9 (fsmonitor: check the untracked cache after a trivial response, 2026-11-09) the cached directories are rechecked after one, but only by their stat data. Editing a file in place does not change the stat data of its directory, so only prep_exclude() notices the edit. Make that recheck also invalidate every directory that has a .gitignore.

This is a trade-off: each such directory is read again once per trivial response. Hashing its .gitignore instead would avoid that, but would have to reproduce how add_patterns() chooses the object name it records, so prefer the simpler check.

On macOS with the builtin fsmonitor daemon, in a tree of about 89,000 directories with a warm untracked cache,
"git status --untracked-files=all" went from about 310 ms to about 65 ms, and the time spent in read_directory() from about 255 ms to about 8 ms.

Assisted-by LLM

---
dir: skip prep_exclude() when fsmonitor is trusted

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2501%2Fstepancheg%2Ffsmonitor-skip-prep-exclude-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2501/stepancheg/fsmonitor-skip-prep-exclude-v1
Pull-Request: https://github.com/git/git/pull/2501

 dir.c                        | 15 ++++++-
 dir.h                        |  7 +--
 t/t7527-builtin-fsmonitor.sh | 82 ++++++++++++++++++++++++++++++++++++
 3 files changed, 100 insertions(+), 4 deletions(-)
dir.c +14 -1
index 4f748800b1..48b11b1636 100644
@@ -2568,6 +2568,14 @@ static int valid_cached_dir(struct dir_struct *dir,
 	if (untracked->check_only != !!check_only)
 		return 0;
 
+	/*
+	 * With fsmonitor, a still-valid entry has had no working-tree
+	 * changes, including to .gitignore, so the prep_exclude()
+	 * check below is redundant.
+	 */
+	if (dir->untracked->use_fsmonitor && untracked->valid)
+		return 1;
+
 	/*
 	 * prep_exclude will be called eventually on this directory,
 	 * but it's called much later in last_matching_pattern(). We
@@ -4076,8 +4084,13 @@ static int invalidate_stale_dirs(struct untracked_cache *uc,
 	int nr_invalidated = 0;
 	unsigned int i;
 
+	/*
+	 * Editing .gitignore in place does not change the stat data of
+	 * its directory, so do not trust a directory that has one.
+	 */
 	if (ucd->valid &&
-	    (lstat(path->buf, &st) ||
+	    (!is_null_oid(&ucd->exclude_oid) ||
+	     lstat(path->buf, &st) ||
 	     match_stat_data_racy(istate, &ucd->stat_data, &st))) {
 		invalidate_one_directory(uc, ucd);
 		nr_invalidated++;
dir.h +4 -3
index 7af1562b44..b945068072 100644
@@ -605,9 +605,10 @@ void untracked_cache_invalidate_trimmed_path(struct index_state *,
 					     const char *path,
 					     int safe_path);
 /*
- * Invalidate every cached directory that no longer exists or whose
- * stat data no longer matches the working tree. valid_cached_dir()
- * skips this check while the file system monitor is trusted.
+ * Invalidate every cached directory that no longer exists, whose
+ * stat data no longer matches the working tree, or that has a
+ * .gitignore, which may have been edited in place. valid_cached_dir()
+ * skips these checks while the file system monitor is trusted.
  * Returns the number of invalidated directories.
  */
 int untracked_cache_invalidate_stale_dirs(struct index_state *);
t/t7527-builtin-fsmonitor.sh +82 -0

86 lines not on this page · raw message

base-commit: f7de81456940dd6f3274e461149bba12965de609

Signature · 1 line
gitgitgadget