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

[PATCH] Avoid unnecessary 'lstat()' calls in 'get_stat_data()'

From
Linus Torvalds <torvalds@linux-foundation.org>
Date
May 9, 2009, 22:09 UTC
Message-ID
<alpine.LFD.2.01.0905091501460.3586@localhost.localdomain>
From: Linus Torvalds <torvalds@linux-foundation.org>
Date: Sat, 9 May 2009 14:57:30 -0700

When we ask get_stat_data() to get the mode and size of an index entry, we can avoid the lstat() call if we have marked the index entry as being uptodate due to earlier lstat() calls.

This avoids a lot of unnecessary lstat() calls in eg 'git checkout', where the last phase shows the differences to the working tree (requiring a diff), but earlier phases have already verified the index.

On the kernel repo (with a fast machine and everything cached), this changes timings of a nul 'git checkout' from

 - Before (best of ten):
	0.14user 0.05system 0:00.19elapsed 100%CPU (0avgtext+0avgdata 0maxresident)k
	0inputs+0outputs (0major+13237minor)pagefaults 0swaps
 - After 
	0.11user 0.03system 0:00.15elapsed 98%CPU (0avgtext+0avgdata 0maxresident)k
	0inputs+0outputs (0major+13235minor)pagefaults 0swaps

so it can obviously be noticeable, although equally obviously it's not a show-stopper on this particular machine. The difference is likely larger on slower machines, or with operating systems that don't do as good a job of name caching.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
---
I sent this as part of the "make 'git checkout' preload the index" patch, 
but since the preloading was of somewhat dubious value, and this part of 
it is not, I'll just send this one-liner as an "obvious performance fix".
 diff-lib.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/diff-lib.c b/diff-lib.c
index a310fb2..0aba6cd 100644
--- a/diff-lib.c
+++ b/diff-lib.c
@@ -214,7 +214,7 @@ static int get_stat_data(struct cache_entry *ce,
 	const unsigned char *sha1 = ce->sha1;
 	unsigned int mode = ce->ce_mode;
 
-	if (!cached) {
+	if (!cached && !ce_uptodate(ce)) {
 		int changed;
 		struct stat st;
 		changed = check_removed(ce, &st);
-- 
1.6.3
Next: Linus Torvalds
Message 1 of 6 in “Avoid unnecessary 'lstat()' calls in 'get_stat_data()'”
  1. Avoid unnecessary 'lstat()' calls in 'get_stat_data()'Linus Torvalds, May 9, 2009
  2. Teach 'git checkout' to preload the index contentsLinus Torvalds, May 9, 2009
  3. Junio C HamanoMay 10, 2009
  4. Linus TorvaldsMay 10, 2009
  5. Johannes SchindelinMay 10, 2009
  6. Junio C HamanoMay 10, 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.