From 3e7a70396c4fba999fc9c1d50c06aeb883b4116f Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Wed, 28 Nov 2012 00:00:00 -0600 Subject: [PATCH] top: give inspect search algorithm a significant boost The Inspect find algorithm has always been challenging given the possibility that 'rows' might contain binary data. Be that as it may, two small changes have proven to dramatically improve the performance of such scans. The first involves the case wherein if no match on the 'substring' portion of a row was found, then a pointer representing the substring was increased by the length of the search string, not the better/longer substring. Thus, portions of the substring were always rescanned! The second performance boost was achieved in this way: pre-scanning each raw row for just the first character in the search string now determines if a full match is even possible. Therefore, repeated unproductive strstr calls on individual substrings within that row will be avoided. In a nutshell, 1 'if' with '}' did the trick! (now that we know a '.' + 2 spaces is squeezed to one) (everything's perfectly justified, but it's just luck) Signed-off-by: Jim Warner --- top/top.c | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/top/top.c b/top/top.c index cd5785b3..d68172d9 100644 --- a/top/top.c +++ b/top/top.c @@ -2471,11 +2471,8 @@ static void insp_do_pipe (char *fmts, int pid) { * ( preceeding control chars would consume an unknown amount ) */ static void insp_find (int ch, int *col, int *row) { #define reDUX (found) ? N_txt(WORD_another_txt) : "" - #define begFS (int)(fnd - Insp_p[i]) static char str[SCREENMAX]; static int found; - char *fnd, *p; - int i, x, ccur = *col; if ((ch == '&' || ch == 'n') && !str[0]) { show_msg(N_txt(FIND_no_next_txt)); @@ -2486,29 +2483,33 @@ static void insp_find (int ch, int *col, int *row) { found = 0; } if (str[0]) { + int i, xx, yy; INSP_BUSY; - for (i = *row; i < Insp_nl; ) { - fnd = NULL; // because our glob might - for (x = ccur +1; x < INSP_RLEN(i); x++) { // be raw binary data, we - if (!*(p = Insp_p[i] + x)) // could encounter a '\0' - continue; // in what we view as the - if ((fnd = STRSTR(p, str))) // 'row' -- so we'll have - break; // to search it in chunks - x += strlen(str); // ... - } // and, account for maybe - if (fnd && fnd < Insp_p[i +1]) { // overrunning that 'row' - found = 1; - *row = i; - *col = begFS; - return; + for (xx = *col, yy = *row; yy < Insp_nl; ) { + // let's skip this entire row, if there's no chance of a match + if (memchr(Insp_p[yy], str[0], INSP_RLEN(yy))) { + char *p, *fnd = NULL; + for (i = xx; i < INSP_RLEN(yy); i++) { + if (!*(p = Insp_p[yy] + i)) // skip any empty strings + continue; + if ((fnd = STRSTR(p, str))) // with binary data, each + break; // row may have '\0'. so + i += strlen(p); // our scans must be done + } // in chunks, and we must + if (fnd && fnd < Insp_p[yy + 1]) { // guard against overrun! + found = 1; + if (xx == *col) { ++xx; continue; } // matched where we were! + *row = yy; // ( tried to fool top? ) + *col = (int)(fnd - Insp_p[yy]); + return; + } } - ++i; - ccur = 0; + xx = 0; + ++yy; } show_msg(fmtmk(N_fmt(FIND_no_find_fmt), reDUX, str)); } #undef reDUX - #undef begFS } // end: insp_find