Skip to content

Commit a3b3c9c

Browse files
committed
Merge branch 'rs/ls-files-partial-optim'
"ls-files" run with pathspec has been micro-optimized to avoid having to memmove(3) unnecessary bytes. * rs/ls-files-partial-optim: ls-files: move only kept cache entries in prune_cache() ls-files: pass prefix length explicitly to prune_cache()
2 parents 0078a75 + 96f6d3f commit a3b3c9c

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

builtin/ls-files.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -369,28 +369,30 @@ static void show_files(struct dir_struct *dir)
369369
/*
370370
* Prune the index to only contain stuff starting with "prefix"
371371
*/
372-
static void prune_cache(const char *prefix)
372+
static void prune_cache(const char *prefix, size_t prefixlen)
373373
{
374-
int pos = cache_name_pos(prefix, max_prefix_len);
374+
int pos;
375375
unsigned int first, last;
376376

377+
if (!prefix)
378+
return;
379+
pos = cache_name_pos(prefix, prefixlen);
377380
if (pos < 0)
378381
pos = -pos-1;
379-
memmove(active_cache, active_cache + pos,
380-
(active_nr - pos) * sizeof(struct cache_entry *));
381-
active_nr -= pos;
382-
first = 0;
382+
first = pos;
383383
last = active_nr;
384384
while (last > first) {
385385
int next = (last + first) >> 1;
386386
const struct cache_entry *ce = active_cache[next];
387-
if (!strncmp(ce->name, prefix, max_prefix_len)) {
387+
if (!strncmp(ce->name, prefix, prefixlen)) {
388388
first = next+1;
389389
continue;
390390
}
391391
last = next;
392392
}
393-
active_nr = last;
393+
memmove(active_cache, active_cache + pos,
394+
(last - pos) * sizeof(struct cache_entry *));
395+
active_nr = last - pos;
394396
}
395397

396398
/*
@@ -641,8 +643,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
641643
show_killed || show_modified || show_resolve_undo))
642644
show_cached = 1;
643645

644-
if (max_prefix)
645-
prune_cache(max_prefix);
646+
prune_cache(max_prefix, max_prefix_len);
646647
if (with_tree) {
647648
/*
648649
* Basic sanity check; show-stages and show-unmerged

0 commit comments

Comments
 (0)