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

[PATCH 2/2] ls-files: move only kept cache entries in prune_cache()

From
René Scharfe <l.s.r@web.de>
Date
Feb 10, 2017, 20:03 UTC
Message-ID
<de84d0f4-cfce-1e73-6d5d-ad18df507d32@web.de>
In-Reply-To
<f480bd26-f74e-9088-844d-26cde0843baa@web.de>

prune_cache() first identifies those entries at the start of the sorted array that can be discarded. Then it moves the rest of the entries up. Last it identifies the unwanted trailing entries among the moved ones and cuts them off.

Change the order: Identify both start *and* end of the range to keep first and then move only those entries to the top. The resulting code is slightly shorter and a bit more efficient.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
The performance impact is probably only measurable with a *really* big
index.
 builtin/ls-files.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index 18105ec7ea..1c0f057d02 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -379,10 +379,7 @@ static void prune_cache(const char *prefix, size_t prefixlen)
 	pos = cache_name_pos(prefix, prefixlen);
 	if (pos < 0)
 		pos = -pos-1;
-	memmove(active_cache, active_cache + pos,
-		(active_nr - pos) * sizeof(struct cache_entry *));
-	active_nr -= pos;
-	first = 0;
+	first = pos;
 	last = active_nr;
 	while (last > first) {
 		int next = (last + first) >> 1;
@@ -393,7 +390,9 @@ static void prune_cache(const char *prefix, size_t prefixlen)
 		}
 		last = next;
 	}
-	active_nr = last;
+	memmove(active_cache, active_cache + pos,
+		(last - pos) * sizeof(struct cache_entry *));
+	active_nr = last - pos;
 }
 
 /*
-- 
2.11.1
Previous: René ScharfeNext: Brandon Williams
Message 2 of 3 in “ls-files: pass prefix length explicitly to prune_cache()”
  1. 1/2 ls-files: pass prefix length explicitly to prune_cache()René Scharfe, Feb 10, 2017
  2. 2/2 ls-files: move only kept cache entries in prune_cache()René Scharfe, Feb 10, 2017
  3. Brandon WilliamsFeb 10, 2017

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.