Skip to content

Commit 32c6f9d

Browse files
Kevin Willforddscho
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 f074ddd commit 32c6f9d

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
@@ -430,7 +430,29 @@ static int update_one(struct cache_tree *it,
430430
continue;
431431

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

436458
#if DEBUG_CACHE_TREE

0 commit comments

Comments
 (0)