Subject: [PATCH] read-cache: avoid memcpy in expand_name_field in index v4

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Mon, 18 Mar 2013 19:58:02 +0700

perf reports memcpy at the the 6th position [1] in "git status -uno"
using index v4, and strbuf_remove() in expand_name_field() accounts
for 25% of that. What we need here is a simple string cut and a
cheaper strbuf_setlen() should be enough. After this change, memcpy
drops down to the 13th position [2] and is dominated by
read_index_from.

[1] before
+     15.74%   git  git                [.] blk_SHA1_Block
+     13.22%   git  [kernel.kallsyms]  [k] link_path_walk
+     10.91%   git  [kernel.kallsyms]  [k] __d_lookup
+      8.17%   git  [kernel.kallsyms]  [k] strncpy_from_user
+      4.75%   git  [kernel.kallsyms]  [k] memcmp
+      2.42%   git  libc-2.11.2.so     [.] memcpy

[2] after
+     16.30%   git  git                [.] blk_SHA1_Block
+     13.43%   git  [kernel.kallsyms]  [k] link_path_walk
+     11.45%   git  [kernel.kallsyms]  [k] __d_lookup
+      8.73%   git  [kernel.kallsyms]  [k] strncpy_from_user
+      5.14%   git  [kernel.kallsyms]  [k] memcmp
+      2.29%   git  [kernel.kallsyms]  [k] do_lookup
+      2.21%   git  libc-2.11.2.so     [.] 0x6daf6
+      1.98%   git  [kernel.kallsyms]  [k] _atomic_dec_and_lock
+      1.98%   git  [kernel.kallsyms]  [k] _raw_spin_lock
+      1.86%   git  [kernel.kallsyms]  [k] acl_permission_check
+      1.61%   git  [kernel.kallsyms]  [k] kmem_cache_free
+      1.59%   git  git                [.] unpack_trees
+      1.47%   git  libc-2.11.2.so     [.] memcpy

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 I was after something else when I noticed this. Seems like a simple
 and safe change.

 read-cache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/read-cache.c b/read-cache.c
index 827ae55..8c443aa 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -1354,7 +1354,7 @@ static unsigned long expand_name_field(struct strbuf *name, const char *cp_)
 
 	if (name->len < len)
 		die("malformed name field in the index");
-	strbuf_remove(name, name->len - len, len);
+	strbuf_setlen(name, name->len - len);
 	for (ep = cp; *ep; ep++)
 		; /* find the end */
 	strbuf_add(name, cp, ep - cp);
-- 
1.8.2.83.gc99314b