1
0
forked from aniani/vim

patch 8.2.5036: using two counters for timeout check in NFA engine

Problem:    Using two counters for timeout check in NFA engine.
Solution:   Use only one counter.  Tune the counts based on guessing.
This commit is contained in:
Bram Moolenaar
2022-05-28 11:08:40 +01:00
parent e6f13b473c
commit 305abc6123
2 changed files with 19 additions and 21 deletions

View File

@@ -4053,7 +4053,6 @@ static int nfa_match;
#ifdef FEAT_RELTIME #ifdef FEAT_RELTIME
static proftime_T *nfa_time_limit; static proftime_T *nfa_time_limit;
static int *nfa_timed_out; static int *nfa_timed_out;
static int nfa_time_count;
#endif #endif
static void copy_sub(regsub_T *to, regsub_T *from); static void copy_sub(regsub_T *to, regsub_T *from);
@@ -5646,22 +5645,26 @@ find_match_text(colnr_T startcol, int regstart, char_u *match_text)
} }
#ifdef FEAT_RELTIME #ifdef FEAT_RELTIME
/*
* Check if we are past the time limit, if there is one.
* To reduce overhead, only check one in "count" times.
*/
static int static int
nfa_did_time_out() nfa_did_time_out(int count)
{ {
static int tm_count = 0; static int tm_count = 0;
// Check for timeout once in 800 times to avoid excessive overhead from // Check for timeout once in "count" times to avoid excessive overhead from
// reading the clock. The value has been picked to check about once per // reading the clock.
// msec on a modern CPU.
if (nfa_time_limit != NULL) if (nfa_time_limit != NULL)
{ {
if (tm_count == 800) if (tm_count >= count)
{ {
if (profile_passed_limit(nfa_time_limit)) if (profile_passed_limit(nfa_time_limit))
{ {
if (nfa_timed_out != NULL) if (nfa_timed_out != NULL)
*nfa_timed_out = TRUE; *nfa_timed_out = TRUE;
tm_count = 99999;
return TRUE; return TRUE;
} }
// Only reset the count when not timed out, so that when it did // Only reset the count when not timed out, so that when it did
@@ -5722,7 +5725,8 @@ nfa_regmatch(
if (got_int) if (got_int)
return FALSE; return FALSE;
#ifdef FEAT_RELTIME #ifdef FEAT_RELTIME
if (nfa_did_time_out()) // Check relatively often here, since this is the toplevel matching.
if (nfa_did_time_out(100))
return FALSE; return FALSE;
#endif #endif
@@ -5876,12 +5880,9 @@ nfa_regmatch(
if (got_int) if (got_int)
break; break;
#ifdef FEAT_RELTIME #ifdef FEAT_RELTIME
if (nfa_time_limit != NULL && ++nfa_time_count == 20) // do not check very often here, since this is a loop in a loop
{ if (nfa_did_time_out(2000))
nfa_time_count = 0; break;
if (nfa_did_time_out())
break;
}
#endif #endif
t = &thislist->t[listidx]; t = &thislist->t[listidx];
@@ -7126,13 +7127,9 @@ nextchar:
if (got_int) if (got_int)
break; break;
#ifdef FEAT_RELTIME #ifdef FEAT_RELTIME
// Check for timeout once in a twenty times to avoid overhead. // check regularly but not too often here
if (nfa_time_limit != NULL && ++nfa_time_count == 20) if (nfa_did_time_out(800))
{ break;
nfa_time_count = 0;
if (nfa_did_time_out())
break;
}
#endif #endif
} }
@@ -7178,7 +7175,6 @@ nfa_regtry(
#ifdef FEAT_RELTIME #ifdef FEAT_RELTIME
nfa_time_limit = tm; nfa_time_limit = tm;
nfa_timed_out = timed_out; nfa_timed_out = timed_out;
nfa_time_count = 0;
#endif #endif
#ifdef ENABLE_LOG #ifdef ENABLE_LOG

View File

@@ -734,6 +734,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
5036,
/**/ /**/
5035, 5035,
/**/ /**/