git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4 7/9] write_entry(): use fstat() instead of lstat() when file is open

From
KBKjetil Barvik <barvik@broadpark.no>
Date
Feb 9, 2009, 20:54 UTC
Message-ID
<989e0b547a09f372d16375ea08397d1115331470.1234211595.git.barvik@broadpark.no>
In-Reply-To
<cover.1234211594.git.barvik@broadpark.no>

Currently inside write_entry() we do an lstat(path, &st) call on a file which have just been opened inside the exact same function. It should be better to call fstat(fd, &st) on the file while it is open, and it should be at least as fast as the lstat() method.

Signed-off-by: Kjetil Barvik <barvik@broadpark.no>
---
 entry.c |   12 +++++++++---
 1 files changed, 9 insertions(+), 3 deletions(-)
diff --git a/entry.c b/entry.c
index 1f53588..5daacc2 100644
--- a/entry.c
+++ b/entry.c
@@ -94,11 +94,12 @@ static void *read_blob_entry(struct cache_entry *ce, unsigned long *size)
 static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
 {
 	unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
-	int fd, ret;
+	int fd, ret, fstat_done = 0;
 	char *new;
 	struct strbuf buf = STRBUF_INIT;
 	unsigned long size;
 	size_t wrote, newsize = 0;
+	struct stat st;
 
 	switch (ce_mode_s_ifmt) {
 	case S_IFREG:
@@ -145,6 +146,11 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout
 		}
 
 		wrote = write_in_full(fd, new, size);
+		/* use fstat() only when path == ce->name */
+		if (state->refresh_cache && !to_tempfile && !state->base_dir_len) {
+			fstat(fd, &st);
+			fstat_done = 1;
+		}
 		close(fd);
 		free(new);
 		if (wrote != size)
@@ -161,8 +167,8 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout
 	}
 
 	if (state->refresh_cache) {
-		struct stat st;
-		lstat(ce->name, &st);
+		if (!fstat_done)
+			lstat(ce->name, &st);
 		fill_stat_cache_info(ce, &st);
 	}
 	return 0;
-- 
1.6.1.349.g99fa5
Previous: Kjetil BarvikNext: Kjetil Barvik
Message 8 of 10 in “git checkout: more cleanups, optimisation, less lstat() calls”
  1. 0/9 git checkout: more cleanups, optimisation, less lstat() callsKjetil Barvik, Feb 9, 2009
  2. 1/9 lstat_cache(): small cleanup and optimisationKjetil Barvik, Feb 9, 2009
  3. 2/9 lstat_cache(): generalise longest_match_lstat_cache()Kjetil Barvik, Feb 9, 2009
  4. 3/9 lstat_cache(): swap func(length, string) into func(string, length)Kjetil Barvik, Feb 9, 2009
  5. 4/9 unlink_entry(): introduce schedule_dir_for_removal()Kjetil Barvik, Feb 9, 2009
  6. 5/9 create_directories(): remove some memcpy() and strchr() callsKjetil Barvik, Feb 9, 2009
  7. 6/9 write_entry(): cleanup of some duplicated codeKjetil Barvik, Feb 9, 2009
  8. 7/9 write_entry(): use fstat() instead of lstat() when file is openKjetil Barvik, Feb 9, 2009
  9. 8/9 show_patch_diff(): remove a call to fstat()Kjetil Barvik, Feb 9, 2009
  10. 9/9 lstat_cache(): print a warning if doing ping-pong between cache typesKjetil Barvik, Feb 9, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.