Skip to content

Commit b38e545

Browse files
committed
Improve performance by limiting the search to newly read choices only
1 parent 902d0b4 commit b38e545

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

pick.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static const char *choice_description(const struct choice *);
7777
static const char *choice_string(const struct choice *);
7878
static void delete_between(char *, size_t, size_t, size_t);
7979
static char *eager_strpbrk(const char *, const char *);
80-
static int filter_choices(size_t);
80+
static int filter_choices(size_t, size_t);
8181
static size_t get_choices(int, ssize_t);
8282
static enum key get_key(const char **);
8383
static void handle_sigwinch(int);
@@ -328,7 +328,8 @@ selected_choice(void)
328328
struct pollfd fds[2];
329329
const char *buf;
330330
ssize_t insert;
331-
size_t cursor_position, i, j, length, nfds, xscroll;
331+
size_t choices_offset, cursor_position, i, j, length, nfds,
332+
xscroll;
332333
size_t choices_count = 0;
333334
size_t selection = 0;
334335
size_t yscroll = 0;
@@ -347,7 +348,7 @@ selected_choice(void)
347348
/* No timeout on first call to poll in order to render the UI fast. */
348349
timo = 0;
349350
for (;;) {
350-
dokey = doread = 0;
351+
choices_offset = dokey = doread = 0;
351352
toggle_sigwinch(1);
352353
nready = xpoll(fds, nfds, timo);
353354
if (nready == -1 && errno != EINTR)
@@ -379,8 +380,9 @@ selected_choice(void)
379380
if (query_length > 0) {
380381
dofilter = 1;
381382
choices_reset = 0;
382-
choices_count += choices.length -
383-
length;
383+
choices_offset = choices_count;
384+
choices_count +=
385+
choices.length - length;
384386
}
385387
} else {
386388
nfds--; /* EOF */
@@ -553,7 +555,9 @@ selected_choice(void)
553555
choices_count = choices.length;
554556
choices_reset = 0;
555557
if (dofilter) {
556-
if ((dochoices = filter_choices(choices_count)))
558+
dochoices = filter_choices(choices_offset,
559+
choices_count);
560+
if (dochoices)
557561
dofilter = selection = yscroll = 0;
558562
}
559563

@@ -585,20 +589,21 @@ selected_choice(void)
585589
}
586590

587591
/*
588-
* Filter the first nchoices number of choices using the current query and
589-
* regularly check for new user input in order to abort filtering. This
590-
* improves the performance when the cardinality of the choices is large.
592+
* Filter nchoices - offset number of choices starting at offset using the
593+
* current query.
594+
* In addition, regularly check for new user input and abort filtering since any
595+
* previous matches could be invalidated by the new query.
591596
* Returns non-zero if the filtering was not aborted.
592597
*/
593598
int
594-
filter_choices(size_t nchoices)
599+
filter_choices(size_t offset, size_t nchoices)
595600
{
596601
struct pollfd pfd;
597602
struct choice *c;
598603
size_t i, match_length;
599604
int nready;
600605

601-
for (i = 0; i < nchoices; i++) {
606+
for (i = offset; i < nchoices; i++) {
602607
c = &choices.v[i];
603608
if (min_match(choice_string(c), 0,
604609
&c->match_start, &c->match_end) == INT_MAX) {

0 commit comments

Comments
 (0)