Skip to contentSkip to conversation

diff-lib: skip leading path check for fsmonitor-valid entries

PATCH Superseded Maik Riechert via GitGitGadget 2 participants 4 messages Nov 18 – Nov 19

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 diff-lib: skip leading path check for fsmonitor-valid entries

From: Maik Riechert <[email protected]>

Since 4f3d6d0261 (fsmonitor: skip lstat deletion check during git diff-index, 2021-03-17), check_removed() does not lstat() entries that fsmonitor says are unchanged. But it still calls has_symlink_leading_path() for them, which lstat()s the directories leading up to each path.

That check is there to notice a leading directory that was replaced by a symlink. fsmonitor would have reported such a change, and refresh_fsmonitor() would have cleared CE_FSMONITOR_VALID for every entry below it, so we would not get here. preload_index() already trusts the flag this way and skips these entries altogether.

The S_ISDIR() check that follows cannot trigger either: the mode comes from the index, and gitlinks are never marked valid.

So return as soon as fake_lstat() has filled in the stat data.

The first new test checks the shortcut itself by replacing a parent directory with a symlink without telling fsmonitor. The other two make sure we still notice when fsmonitor does report that directory, with and without a trailing slash, since nothing else checks for it now.

On Linux, with 100,000 files (ten per directory), a hook that reports nothing and a warm cache, "git diff HEAD" goes from 57ms to 26ms.

---
diff-lib: skip leading path check for fsmonitor-valid entries

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2318%2Fletmaik%2Fdiff-lib-fsmonitor-leading-path-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2318/letmaik/diff-lib-fsmonitor-leading-path-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2318

 diff-lib.c                  |  8 +++----
 t/t7519-status-fsmonitor.sh | 44 +++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 4 deletions(-)
diff-lib.c +4 -4
index 086476bd77..3730e04149 100644
@@ -43,10 +43,10 @@ static int check_removed(const struct cache_entry *ce, struct stat *st)
 {
 	int stat_err;
 
-	if (!(ce->ce_flags & CE_FSMONITOR_VALID))
-		stat_err = lstat(ce->name, st);
-	else
-		stat_err = fake_lstat(ce, st);
+	if (ce->ce_flags & CE_FSMONITOR_VALID)
+		return fake_lstat(ce, st);
+
+	stat_err = lstat(ce->name, st);
 	if (stat_err < 0) {
 		if (!is_missing_file_error(errno))
 			return -1;
t/t7519-status-fsmonitor.sh +44 -0

50 lines not on this page · raw message

base-commit: e1ec09b23c8e0deb21a0b95ac5996422277395d4

Signature · 1 line
gitgitgadget
#1 lore raw
Quoted 9 lines · Maik Riechert via GitGitGadget
diff-lib.c +0 -1
index 086476bd77..3730e04149 100644
@@ -43,10 +43,10 @@ static int check_removed(const struct cache_entry *ce, struct stat *st)
 {
 	int stat_err;
 
-	if (!(ce->ce_flags & CE_FSMONITOR_VALID))

diff-lib.c static int check_removed(const struct cache_entry *ce, struct stat *st) · line 52

Quoted 10 lines
diff-lib.c +4 -3
-		stat_err = lstat(ce->name, st);
-	else
-		stat_err = fake_lstat(ce, st);
+	if (ce->ce_flags & CE_FSMONITOR_VALID)
+		return fake_lstat(ce, st);
+
+	stat_err = lstat(ce->name, st);
 	if (stat_err < 0) {
 		if (!is_missing_file_error(errno))
 			return -1;

Now that stat_err is used only once, we can retire the variable and inline the lstat() call inside the if() condition.

Other than that, assuming that the CE_FSMONITOR_VALID bit is trustworthy, the change looks OK. has_symlink_leading_path() would be fairly expensive and being able to avoid it is a big plus, which is done in other code paths like read-cache.c:refresh_cache_ent() and preload-index.c:preload_thread() already.

#2 lore raw

PATCH v2 1/1 diff-lib: skip leading path check for fsmonitor-valid entries

In reply to Maik Riechert via GitGitGadget

From: Maik Riechert <[email protected]>

Since 4f3d6d0261 (fsmonitor: skip lstat deletion check during git diff-index, 2021-03-17), check_removed() does not lstat() entries that fsmonitor says are unchanged. But it still calls has_symlink_leading_path() for them, which lstat()s the directories leading up to each path.

That check is there to notice a leading directory that was replaced by a symlink. fsmonitor would have reported such a change, and refresh_fsmonitor() would have cleared CE_FSMONITOR_VALID for every entry below it, so we would not get here. preload_index() already trusts the flag this way and skips these entries altogether.

The S_ISDIR() check that follows cannot trigger either: the mode comes from the index, and gitlinks are never marked valid.

So return as soon as fake_lstat() has filled in the stat data.

The first new test checks the shortcut itself by replacing a parent directory with a symlink without telling fsmonitor. The other two make sure we still notice when fsmonitor does report that directory, with and without a trailing slash, since nothing else checks for it now.

On Linux, with 100,000 files (ten per directory), a hook that reports nothing and a warm cache, "git diff HEAD" goes from 57ms to 26ms.

---
diff-lib: skip leading path check for fsmonitor-valid entries

Changes since v1:

* Dropped the stat_err variable and called lstat() directly in the if() condition (suggested by Junio).

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2318%2Fletmaik%2Fdiff-lib-fsmonitor-leading-path-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2318/letmaik/diff-lib-fsmonitor-leading-path-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/2318

Range-diff vs v1:

1: 22f99f7189 ! 1: 4f8cbd869d diff-lib: skip leading path check for fsmonitor-valid entries

     @@ Commit message
          Signed-off-by: Maik Riechert <[email protected]>
      
       ## diff-lib.c ##
     -@@ diff-lib.c: static int check_removed(const struct cache_entry *ce, struct stat *st)
     +@@
     +  */
     + static int check_removed(const struct cache_entry *ce, struct stat *st)
       {
     - 	int stat_err;
     +-	int stat_err;
     ++	if (ce->ce_flags & CE_FSMONITOR_VALID)
     ++		return fake_lstat(ce, st);
       
      -	if (!(ce->ce_flags & CE_FSMONITOR_VALID))
      -		stat_err = lstat(ce->name, st);
      -	else
      -		stat_err = fake_lstat(ce, st);
     -+	if (ce->ce_flags & CE_FSMONITOR_VALID)
     -+		return fake_lstat(ce, st);
     -+
     -+	stat_err = lstat(ce->name, st);
     - 	if (stat_err < 0) {
     +-	if (stat_err < 0) {
     ++	if (lstat(ce->name, st) < 0) {
       		if (!is_missing_file_error(errno))
       			return -1;
     + 		return 1;
      
       ## t/t7519-status-fsmonitor.sh ##
      @@ t/t7519-status-fsmonitor.sh: do
 diff-lib.c                  |  9 +++-----
 t/t7519-status-fsmonitor.sh | 44 +++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 6 deletions(-)
diff-lib.c +3 -6
index 086476bd77..b644154397 100644
@@ -41,13 +41,10 @@
  */
 static int check_removed(const struct cache_entry *ce, struct stat *st)
 {
-	int stat_err;
+	if (ce->ce_flags & CE_FSMONITOR_VALID)
+		return fake_lstat(ce, st);
 
-	if (!(ce->ce_flags & CE_FSMONITOR_VALID))
-		stat_err = lstat(ce->name, st);
-	else
-		stat_err = fake_lstat(ce, st);
-	if (stat_err < 0) {
+	if (lstat(ce->name, st) < 0) {
 		if (!is_missing_file_error(errno))
 			return -1;
 		return 1;
t/t7519-status-fsmonitor.sh +44 -0

50 lines not on this page · raw message

base-commit: e1ec09b23c8e0deb21a0b95ac5996422277395d4

Signature · 1 line
gitgitgadget
#3 lore raw
Quoted 8 lines · Maik Riechert via GitGitGadget

From: Maik Riechert <[email protected]>

Since 4f3d6d0261 (fsmonitor: skip lstat deletion check during git diff-index, 2021-03-17), check_removed() does not lstat() entries that fsmonitor says are unchanged. But it still calls has_symlink_leading_path() for them, which lstat()s the directories leading up to each path.

Quoted 8 lines

That check is there to notice a leading directory that was replaced by a symlink. fsmonitor would have reported such a change, and refresh_fsmonitor() would have cleared CE_FSMONITOR_VALID for every entry below it, so we would not get here. preload_index() already trusts the flag this way and skips these entries altogether.

The S_ISDIR() check that follows cannot trigger either: the mode comes from the index, and gitlinks are never marked valid.

This is slightly misleading. S_IFGITLINK is given S_IFDIR | 0755 by st_mode_from_ce() that is called by fake_lstat(), so S_ISDIR() check *does* trigger. We have an extra S_ISGITLINK() guard in there, preventing us from calling repo_resolve_gitlink_ref().

It does not change the conclusion, though.

Quoted 7 lines

So return as soon as fake_lstat() has filled in the stat data.

The first new test checks the shortcut itself by replacing a parent directory with a symlink without telling fsmonitor. The other two make sure we still notice when fsmonitor does report that directory, with and without a trailing slash, since nothing else checks for it now.

Quoted 6 lines

On Linux, with 100,000 files (ten per directory), a hook that reports nothing and a warm cache, "git diff HEAD" goes from 57ms to 26ms.

---

Will queue. Thanks.