From 89d2a20e561878f3fee11005369c1d56b9a08c38 Mon Sep 17 00:00:00 2001 From: nicm Date: Wed, 1 Apr 2020 07:35:10 +0000 Subject: Performance improvements for regex searching, most notably: - Use the grid data directly instead of copying it. - Special case the most typical one byte character cells and use memcmp for multiple bytes instead of a handrolled loop. - Hoist regcomp out of the loop into the calling functions. GitHub issue 2143. Also a man page from from jmc@. --- window-copy.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) (limited to 'window-copy.c') diff --git a/window-copy.c b/window-copy.c index 8c226a92..57228273 100644 --- a/window-copy.c +++ b/window-copy.c @@ -2523,9 +2523,8 @@ window_copy_cstrtocellpos(struct grid *gd, u_int ncells, u_int *ppx, u_int *ppy, cells[cell].d = window_copy_cellstring(gl, px, &cells[cell].dlen); cell++; - px++; - if (px == gd->sx) { - px = 0; + px = (px + 1) % gd->sx; + if (px == 0) { pywrap++; gl = grid_peek_line(gd, pywrap); } @@ -2714,27 +2713,23 @@ window_copy_search(struct window_mode_entry *wme, int direction, int regex) struct screen *s = data->backing, ss; struct screen_write_ctx ctx; struct grid *gd = s->grid; - const char *str = data->searchstr; u_int fx, fy, endline; int wrapflag, cis, found; - if (regex && str[strcspn(str, "^$*+()?[].\\")] == '\0') - regex = 0; - free(wp->searchstr); - wp->searchstr = xstrdup(str); + wp->searchstr = xstrdup(data->searchstr); wp->searchregex = regex; fx = data->cx; fy = screen_hsize(data->backing) - data->oy + data->cy; - screen_init(&ss, screen_write_strlen("%s", str), 1, 0); + screen_init(&ss, screen_write_strlen("%s", data->searchstr), 1, 0); screen_write_start(&ctx, NULL, &ss); - screen_write_nputs(&ctx, -1, &grid_default_cell, "%s", str); + screen_write_nputs(&ctx, -1, &grid_default_cell, "%s", data->searchstr); screen_write_stop(&ctx); wrapflag = options_get_number(wp->window->options, "wrap-search"); - cis = window_copy_is_lowercase(str); + cis = window_copy_is_lowercase(data->searchstr); if (direction) { window_copy_move_right(s, &fx, &fy, wrapflag); -- cgit From c129ed3233d8303f3ae1fc14a9728e8d231c3911 Mon Sep 17 00:00:00 2001 From: nicm Date: Wed, 1 Apr 2020 07:52:07 +0000 Subject: Use a comparison to check for wrap and avoid an expensive modulus. --- window-copy.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'window-copy.c') diff --git a/window-copy.c b/window-copy.c index 57228273..4a3ee9ea 100644 --- a/window-copy.c +++ b/window-copy.c @@ -2523,8 +2523,9 @@ window_copy_cstrtocellpos(struct grid *gd, u_int ncells, u_int *ppx, u_int *ppy, cells[cell].d = window_copy_cellstring(gl, px, &cells[cell].dlen); cell++; - px = (px + 1) % gd->sx; - if (px == 0) { + px++; + if (px == gd->sx) { + px = 0; pywrap++; gl = grid_peek_line(gd, pywrap); } -- cgit From cd30633d1092366dc5cc44adca1cd3675de9cf39 Mon Sep 17 00:00:00 2001 From: nicm Date: Wed, 1 Apr 2020 08:07:05 +0000 Subject: Do not go down the regex search path (which is expensive because we need to convert the grid data into a string for regexec and reverse it to find the grid position) if the search string does not contain any regex special characters. --- window-copy.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'window-copy.c') diff --git a/window-copy.c b/window-copy.c index 4a3ee9ea..8c226a92 100644 --- a/window-copy.c +++ b/window-copy.c @@ -2714,23 +2714,27 @@ window_copy_search(struct window_mode_entry *wme, int direction, int regex) struct screen *s = data->backing, ss; struct screen_write_ctx ctx; struct grid *gd = s->grid; + const char *str = data->searchstr; u_int fx, fy, endline; int wrapflag, cis, found; + if (regex && str[strcspn(str, "^$*+()?[].\\")] == '\0') + regex = 0; + free(wp->searchstr); - wp->searchstr = xstrdup(data->searchstr); + wp->searchstr = xstrdup(str); wp->searchregex = regex; fx = data->cx; fy = screen_hsize(data->backing) - data->oy + data->cy; - screen_init(&ss, screen_write_strlen("%s", data->searchstr), 1, 0); + screen_init(&ss, screen_write_strlen("%s", str), 1, 0); screen_write_start(&ctx, NULL, &ss); - screen_write_nputs(&ctx, -1, &grid_default_cell, "%s", data->searchstr); + screen_write_nputs(&ctx, -1, &grid_default_cell, "%s", str); screen_write_stop(&ctx); wrapflag = options_get_number(wp->window->options, "wrap-search"); - cis = window_copy_is_lowercase(data->searchstr); + cis = window_copy_is_lowercase(str); if (direction) { window_copy_move_right(s, &fx, &fy, wrapflag); -- cgit From 567b27e10a07e9ab8266e629edd348a1d432a873 Mon Sep 17 00:00:00 2001 From: nicm Date: Wed, 1 Apr 2020 09:36:37 +0000 Subject: Add a 10 second timeout to prevent searches taking too much time, from Anindya Mukherjee. --- window-copy.c | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) (limited to 'window-copy.c') diff --git a/window-copy.c b/window-copy.c index 8c226a92..bc0cd849 100644 --- a/window-copy.c +++ b/window-copy.c @@ -261,6 +261,9 @@ struct window_copy_mode_data { int searchy; int searcho; + int timeout; /* search has timed out */ +#define WINDOW_COPY_SEARCH_TIMEOUT 10 + int jumptype; char jumpchar; @@ -316,6 +319,7 @@ window_copy_common_init(struct window_mode_entry *wme) } data->searchmark = NULL; data->searchx = data->searchy = data->searcho = -1; + data->timeout = 0; data->jumptype = WINDOW_COPY_OFF; data->jumpchar = '\0'; @@ -680,8 +684,8 @@ window_copy_resize(struct window_mode_entry *wme, u_int sx, u_int sy) window_copy_write_lines(wme, &ctx, 0, screen_size_y(s) - 1); screen_write_stop(&ctx); - if (search) - window_copy_search_marks(wme, NULL, 1); + if (search && !data->timeout) + window_copy_search_marks(wme, NULL, data->searchregex); data->searchx = data->cx; data->searchy = data->cy; data->searcho = data->oy; @@ -1800,6 +1804,7 @@ window_copy_cmd_search_backward(struct window_copy_cmd_state *cs) if (data->searchstr != NULL) { data->searchtype = WINDOW_COPY_SEARCHUP; data->searchregex = 1; + data->timeout = 0; for (; np != 0; np--) window_copy_search_up(wme, 1); } @@ -1819,6 +1824,7 @@ window_copy_cmd_search_backward_text(struct window_copy_cmd_state *cs) if (data->searchstr != NULL) { data->searchtype = WINDOW_COPY_SEARCHUP; data->searchregex = 0; + data->timeout = 0; for (; np != 0; np--) window_copy_search_up(wme, 0); } @@ -1838,6 +1844,7 @@ window_copy_cmd_search_forward(struct window_copy_cmd_state *cs) if (data->searchstr != NULL) { data->searchtype = WINDOW_COPY_SEARCHDOWN; data->searchregex = 1; + data->timeout = 0; for (; np != 0; np--) window_copy_search_down(wme, 1); } @@ -1857,6 +1864,7 @@ window_copy_cmd_search_forward_text(struct window_copy_cmd_state *cs) if (data->searchstr != NULL) { data->searchtype = WINDOW_COPY_SEARCHDOWN; data->searchregex = 0; + data->timeout = 0; for (; np != 0; np--) window_copy_search_down(wme, 0); } @@ -1873,6 +1881,8 @@ window_copy_cmd_search_backward_incremental(struct window_copy_cmd_state *cs) char prefix; enum window_copy_cmd_action action = WINDOW_COPY_CMD_NOTHING; + data->timeout = 0; + prefix = *argument++; if (data->searchx == -1 || data->searchy == -1) { data->searchx = data->cx; @@ -1924,6 +1934,8 @@ window_copy_cmd_search_forward_incremental(struct window_copy_cmd_state *cs) char prefix; enum window_copy_cmd_action action = WINDOW_COPY_CMD_NOTHING; + data->timeout = 0; + prefix = *argument++; if (data->searchx == -1 || data->searchy == -1) { data->searchx = data->cx; @@ -2721,6 +2733,9 @@ window_copy_search(struct window_mode_entry *wme, int direction, int regex) if (regex && str[strcspn(str, "^$*+()?[].\\")] == '\0') regex = 0; + if (data->timeout) + return (0); + free(wp->searchstr); wp->searchstr = xstrdup(str); wp->searchregex = regex; @@ -2768,6 +2783,7 @@ window_copy_search_marks(struct window_mode_entry *wme, struct screen *ssp, u_int ssize = 1; char *sbuf; regex_t reg; + time_t tstart, t; if (ssp == NULL) { width = screen_write_strlen("%s", data->searchstr); @@ -2797,6 +2813,7 @@ window_copy_search_marks(struct window_mode_entry *wme, struct screen *ssp, return (0); } } + time(&tstart); for (py = 0; py < gd->hsize + gd->sy; py++) { px = 0; for (;;) { @@ -2822,11 +2839,21 @@ window_copy_search_marks(struct window_mode_entry *wme, struct screen *ssp, px++; } + + time(&t); + if (t - tstart > WINDOW_COPY_SEARCH_TIMEOUT) { + data->timeout = 1; + break; + } } if (regex) { free(sbuf); regfree(®); } + if (data->timeout) { + window_copy_clear_marks(wme); + return (1); + } if (which != -1) data->searchthis = 1 + nfound - which; @@ -2836,7 +2863,7 @@ window_copy_search_marks(struct window_mode_entry *wme, struct screen *ssp, if (ssp == &ss) screen_free(&ss); - return (nfound); + return (1); } static void @@ -2895,8 +2922,15 @@ window_copy_write_line(struct window_mode_entry *wme, if (py == 0 && s->rupper < s->rlower && !data->hide_position) { if (data->searchmark == NULL) { - size = xsnprintf(hdr, sizeof hdr, - "[%u/%u]", data->oy, screen_hsize(data->backing)); + if (data->timeout) { + size = xsnprintf(hdr, sizeof hdr, + "(timed out) [%u/%u]", data->oy, + screen_hsize(data->backing)); + } else { + size = xsnprintf(hdr, sizeof hdr, + "[%u/%u]", data->oy, + screen_hsize(data->backing)); + } } else { if (data->searchthis == -1) { size = xsnprintf(hdr, sizeof hdr, -- cgit