git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 1/2] ls-files: pass prefix length explicitly to prune_cache()
@ 2017-02-10 19:42 René Scharfe
  2017-02-10 20:03 ` [PATCH 2/2] ls-files: move only kept cache entries in prune_cache() René Scharfe
  0 siblings, 1 reply; 3+ messages in thread
From: René Scharfe @ 2017-02-10 19:42 UTC (permalink / raw)
  To: Git List; +Cc: Duy Nguyen, Brandon Williams, Junio C Hamano

The function prune_cache() relies on the fact that it is only called on
max_prefix and sneakily uses the matching global variable max_prefix_len
directly.  Tighten its interface by passing both the string and its
length as parameters.  While at it move the NULL check into the function
to collect all cache-pruning related logic in one place.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
Not urgent, of course.

 builtin/ls-files.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index 1592290815..18105ec7ea 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -369,11 +369,14 @@ static void show_files(struct dir_struct *dir)
 /*
  * Prune the index to only contain stuff starting with "prefix"
  */
-static void prune_cache(const char *prefix)
+static void prune_cache(const char *prefix, size_t prefixlen)
 {
-	int pos = cache_name_pos(prefix, max_prefix_len);
+	int pos;
 	unsigned int first, last;
 
+	if (!prefix)
+		return;
+	pos = cache_name_pos(prefix, prefixlen);
 	if (pos < 0)
 		pos = -pos-1;
 	memmove(active_cache, active_cache + pos,
@@ -384,7 +387,7 @@ static void prune_cache(const char *prefix)
 	while (last > first) {
 		int next = (last + first) >> 1;
 		const struct cache_entry *ce = active_cache[next];
-		if (!strncmp(ce->name, prefix, max_prefix_len)) {
+		if (!strncmp(ce->name, prefix, prefixlen)) {
 			first = next+1;
 			continue;
 		}
@@ -641,8 +644,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 	      show_killed || show_modified || show_resolve_undo))
 		show_cached = 1;
 
-	if (max_prefix)
-		prune_cache(max_prefix);
+	prune_cache(max_prefix, max_prefix_len);
 	if (with_tree) {
 		/*
 		 * Basic sanity check; show-stages and show-unmerged
-- 
2.11.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] ls-files: move only kept cache entries in prune_cache()
  2017-02-10 19:42 [PATCH 1/2] ls-files: pass prefix length explicitly to prune_cache() René Scharfe
@ 2017-02-10 20:03 ` René Scharfe
  2017-02-10 22:51   ` Brandon Williams
  0 siblings, 1 reply; 3+ messages in thread
From: René Scharfe @ 2017-02-10 20:03 UTC (permalink / raw)
  To: Git List; +Cc: Duy Nguyen, Brandon Williams, Junio C Hamano

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 2/2] ls-files: move only kept cache entries in prune_cache()
  2017-02-10 20:03 ` [PATCH 2/2] ls-files: move only kept cache entries in prune_cache() René Scharfe
@ 2017-02-10 22:51   ` Brandon Williams
  0 siblings, 0 replies; 3+ messages in thread
From: Brandon Williams @ 2017-02-10 22:51 UTC (permalink / raw)
  To: René Scharfe; +Cc: Git List, Duy Nguyen, Junio C Hamano

On 02/10, René Scharfe wrote:
> 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.

Well there's been a lot of talk recently about *really* big indexes, so
I'm sure someone out there will be happy :)

> 
>  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
> 

Both these patches look good to me.

-- 
Brandon Williams

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-02-10 22:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-10 19:42 [PATCH 1/2] ls-files: pass prefix length explicitly to prune_cache() René Scharfe
2017-02-10 20:03 ` [PATCH 2/2] ls-files: move only kept cache entries in prune_cache() René Scharfe
2017-02-10 22:51   ` Brandon Williams

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).