Skip to content

Commit bf32c09

Browse files
Kevin Willforddscho
Kevin Willford
authored andcommitted
cache-tree: remove use of strbuf_addf in update_one
String formatting can be a performance issue when there are hundreds of thousands of trees. Change to stop using the strbuf_addf and just add the strings or characters individually. There are a limited number of modes so added a switch for the known ones and a default case if something comes through that are not a known one for git. In one scenario regarding a huge worktree, this reduces the time required for a `git checkout <branch>` from 44 seconds to 38 seconds, i.e. it is a non-negligible performance improvement. Signed-off-by: Kevin Willford <[email protected]>
1 parent 60e7946 commit bf32c09

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

cache-tree.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,29 @@ static int update_one(struct cache_tree *it,
432432
continue;
433433

434434
strbuf_grow(&buffer, entlen + 100);
435-
strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
435+
436+
switch (mode) {
437+
case 0100644:
438+
strbuf_add(&buffer, "100644 ", 7);
439+
break;
440+
case 0100664:
441+
strbuf_add(&buffer, "100664 ", 7);
442+
break;
443+
case 0100755:
444+
strbuf_add(&buffer, "100755 ", 7);
445+
break;
446+
case 0120000:
447+
strbuf_add(&buffer, "120000 ", 7);
448+
break;
449+
case 0160000:
450+
strbuf_add(&buffer, "160000 ", 7);
451+
break;
452+
default:
453+
strbuf_addf(&buffer, "%o ", mode);
454+
break;
455+
}
456+
strbuf_add(&buffer, path + baselen, entlen);
457+
strbuf_addch(&buffer, '\0');
436458
strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
437459

438460
#if DEBUG_CACHE_TREE

0 commit comments

Comments
 (0)