Skip to content

Commit 1c63bb2

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 <kewillf@microsoft.com>
1 parent 0a34c2e commit 1c63bb2

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

cache-tree.c

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

399399
strbuf_grow(&buffer, entlen + 100);
400-
strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
400+
401+
switch (mode) {
402+
case 0100644:
403+
strbuf_add(&buffer, "100644 ", 7);
404+
break;
405+
case 0100664:
406+
strbuf_add(&buffer, "100664 ", 7);
407+
break;
408+
case 0100755:
409+
strbuf_add(&buffer, "100755 ", 7);
410+
break;
411+
case 0120000:
412+
strbuf_add(&buffer, "120000 ", 7);
413+
break;
414+
case 0160000:
415+
strbuf_add(&buffer, "160000 ", 7);
416+
break;
417+
default:
418+
strbuf_addf(&buffer, "%o ", mode);
419+
break;
420+
}
421+
strbuf_add(&buffer, path + baselen, entlen);
422+
strbuf_addch(&buffer, '\0');
401423
strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
402424

403425
#if DEBUG_CACHE_TREE

0 commit comments

Comments
 (0)