Mercurial > vim
diff src/search.c @ 29071:b90bca860b5a v8.2.5057
patch 8.2.5057: using gettimeofday() for timeout is very inefficient
Commit: https://github.com/vim/vim/commit/6574577cacd393ab7591fc776ea060eebc939e55
Author: Paul Ollis <paul@cleversheep.org>
Date: Sun Jun 5 16:55:54 2022 +0100
patch 8.2.5057: using gettimeofday() for timeout is very inefficient
Problem: Using gettimeofday() for timeout is very inefficient.
Solution: Set a platform dependent timer. (Paul Ollis, closes https://github.com/vim/vim/issues/10505)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sun, 05 Jun 2022 18:00:08 +0200 |
parents | 485619e7f836 |
children | d1e263ecf634 |
line wrap: on
line diff
--- a/src/search.c +++ b/src/search.c @@ -658,19 +658,8 @@ searchit( int break_loop = FALSE; #endif linenr_T stop_lnum = 0; // stop after this line number when != 0 -#ifdef FEAT_RELTIME - proftime_T *tm = NULL; // timeout limit or NULL - int *timed_out = NULL; // set when timed out or NULL -#endif - - if (extra_arg != NULL) - { - stop_lnum = extra_arg->sa_stop_lnum; -#ifdef FEAT_RELTIME - tm = extra_arg->sa_tm; - timed_out = &extra_arg->sa_timed_out; -#endif - } + int unused_timeout_flag = FALSE; + int *timed_out = &unused_timeout_flag; // set when timed out. if (search_regcomp(pat, RE_SEARCH, pat_use, (options & (SEARCH_HIS + SEARCH_KEEP)), ®match) == FAIL) @@ -680,6 +669,18 @@ searchit( return FAIL; } + if (extra_arg != NULL) + { + stop_lnum = extra_arg->sa_stop_lnum; +#ifdef FEAT_RELTIME + if (extra_arg->sa_tm > 0) + { + init_regexp_timeout(extra_arg->sa_tm); + timed_out = &extra_arg->sa_timed_out; + } +#endif + } + /* * find the string */ @@ -753,11 +754,9 @@ searchit( if (stop_lnum != 0 && (dir == FORWARD ? lnum > stop_lnum : lnum < stop_lnum)) break; -#ifdef FEAT_RELTIME - // Stop after passing the "tm" time limit. - if (tm != NULL && profile_passed_limit(tm)) + // Stop after passing the time limit. + if (*timed_out) break; -#endif /* * Look for a match somewhere in line "lnum". @@ -765,22 +764,12 @@ searchit( col = at_first_line && (options & SEARCH_COL) ? pos->col : (colnr_T)0; nmatched = vim_regexec_multi(®match, win, buf, - lnum, col, -#ifdef FEAT_RELTIME - tm, timed_out -#else - NULL, NULL -#endif - ); + lnum, col, timed_out); // vim_regexec_multi() may clear "regprog" if (regmatch.regprog == NULL) break; // Abort searching on an error (e.g., out of stack). - if (called_emsg > called_emsg_before -#ifdef FEAT_RELTIME - || (timed_out != NULL && *timed_out) -#endif - ) + if (called_emsg > called_emsg_before || *timed_out) break; if (nmatched > 0) { @@ -863,13 +852,7 @@ searchit( if (ptr[matchcol] == NUL || (nmatched = vim_regexec_multi(®match, win, buf, lnum + matchpos.lnum, - matchcol, -#ifdef FEAT_RELTIME - tm, timed_out -#else - NULL, NULL -#endif - )) == 0) + matchcol, timed_out)) == 0) { match_ok = FALSE; break; @@ -974,21 +957,13 @@ searchit( if (ptr[matchcol] == NUL || (nmatched = vim_regexec_multi(®match, win, buf, lnum + matchpos.lnum, - matchcol, -#ifdef FEAT_RELTIME - tm, timed_out -#else - NULL, NULL -#endif - )) == 0) + matchcol, timed_out)) == 0) { -#ifdef FEAT_RELTIME // If the search timed out, we did find a match // but it might be the wrong one, so that's not // OK. - if (timed_out != NULL && *timed_out) + if (*timed_out) match_ok = FALSE; -#endif break; } // vim_regexec_multi() may clear "regprog" @@ -1097,10 +1072,7 @@ searchit( * twice. */ if (!p_ws || stop_lnum != 0 || got_int - || called_emsg > called_emsg_before -#ifdef FEAT_RELTIME - || (timed_out != NULL && *timed_out) -#endif + || called_emsg > called_emsg_before || *timed_out #ifdef FEAT_SEARCH_EXTRA || break_loop #endif @@ -1124,10 +1096,7 @@ searchit( if (extra_arg != NULL) extra_arg->sa_wrapped = TRUE; } - if (got_int || called_emsg > called_emsg_before -#ifdef FEAT_RELTIME - || (timed_out != NULL && *timed_out) -#endif + if (got_int || called_emsg > called_emsg_before || *timed_out #ifdef FEAT_SEARCH_EXTRA || break_loop #endif @@ -1136,6 +1105,9 @@ searchit( } while (--count > 0 && found); // stop after count matches or no match +# ifdef FEAT_RELTIME + disable_regexp_timeout(); +# endif vim_regfree(regmatch.regprog); if (!found) // did not find it @@ -2915,7 +2887,7 @@ is_zero_width(char_u *pattern, int move, { regmatch.startpos[0].col++; nmatched = vim_regexec_multi(®match, curwin, curbuf, - pos.lnum, regmatch.startpos[0].col, NULL, NULL); + pos.lnum, regmatch.startpos[0].col, NULL); if (nmatched != 0) break; } while (regmatch.regprog != NULL