Skip to content

Commit 6437e5b

Browse files
committed
Improve performance by limiting the search to newly read choices only
1 parent 44dddce commit 6437e5b

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

pick.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static const char *choice_description(const struct choice *);
7878
static const char *choice_string(const struct choice *);
7979
static void delete_between(char *, size_t, size_t, size_t);
8080
static char *eager_strpbrk(const char *, const char *);
81-
static int filter_choices(size_t);
81+
static int filter_choices(size_t, size_t);
8282
static size_t get_choices(int, ssize_t);
8383
static enum key get_key(const char **);
8484
static void handle_sigwinch(int);
@@ -329,7 +329,8 @@ selected_choice(void)
329329
struct pollfd fds[2];
330330
const char *buf;
331331
ssize_t insert;
332-
size_t cursor_position, i, j, length, nfds, xscroll;
332+
size_t choices_offset, cursor_position, i, j, length, nfds,
333+
xscroll;
333334
size_t choices_count = 0;
334335
size_t selection = 0;
335336
size_t yscroll = 0;
@@ -373,15 +374,17 @@ selected_choice(void)
373374
dokey = 1;
374375
}
375376

377+
choices_offset = 0;
376378
length = choices.length;
377379
if (doread) {
378380
insert = query_length > 0 ? (ssize_t)choices_count : -1;
379381
if (get_choices(fds[1].fd, insert)) {
380382
if (query_length > 0) {
381383
dofilter = 1;
382384
choices_reset = 0;
383-
choices_count += choices.length -
384-
length;
385+
choices_offset = choices_count;
386+
choices_count +=
387+
choices.length - length;
385388
}
386389
} else {
387390
nfds--; /* EOF */
@@ -554,7 +557,9 @@ selected_choice(void)
554557
choices_count = choices.length;
555558
choices_reset = 0;
556559
if (dofilter) {
557-
if ((dochoices = filter_choices(choices_count)))
560+
dochoices = filter_choices(choices_offset,
561+
choices_count);
562+
if (dochoices)
558563
dofilter = selection = yscroll = 0;
559564
}
560565

@@ -586,20 +591,21 @@ selected_choice(void)
586591
}
587592

588593
/*
589-
* Filter the first nchoices number of choices using the current query and
590-
* regularly check for new user input in order to abort filtering. This
591-
* improves the performance when the cardinality of the choices is large.
594+
* Filter nchoices - offset number of choices starting at offset using the
595+
* current query.
596+
* In addition, regularly check for new user input and abort filtering since any
597+
* previous matches could be invalidated by the new query.
592598
* Returns non-zero if the filtering was not aborted.
593599
*/
594600
int
595-
filter_choices(size_t nchoices)
601+
filter_choices(size_t offset, size_t nchoices)
596602
{
597603
struct pollfd pfd;
598604
struct choice *c;
599605
size_t i, match_length;
600606
int nready;
601607

602-
for (i = 0; i < nchoices; i++) {
608+
for (i = offset; i < nchoices; i++) {
603609
c = &choices.v[i];
604610
if (min_match(choice_string(c), 0,
605611
&c->match_start, &c->match_end) == INT_MAX) {

0 commit comments

Comments
 (0)