From 02e8d4e4ffcdd6ee919e19692d591da8e18a565d Mon Sep 17 00:00:00 2001 From: Bram Moolenaar Date: Fri, 27 May 2022 15:35:28 +0100 Subject: patch 8.2.5028: syntax regexp matching can be slow Problem: Syntax regexp matching can be slow. Solution: Adjust the counters for checking the timeout to check about once per msec. (closes #10487, closes #2712) --- src/regexp_nfa.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'src/regexp_nfa.c') diff --git a/src/regexp_nfa.c b/src/regexp_nfa.c index 511b90483..1e7cd9b46 100644 --- a/src/regexp_nfa.c +++ b/src/regexp_nfa.c @@ -5649,11 +5649,27 @@ find_match_text(colnr_T startcol, int regstart, char_u *match_text) static int nfa_did_time_out() { - if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit)) + static int tm_count = 0; + + // Check for timeout once in 800 times to avoid excessive overhead from + // reading the clock. The value has been picked to check about once per + // msec on a modern CPU. + if (nfa_time_limit != NULL) { - if (nfa_timed_out != NULL) - *nfa_timed_out = TRUE; - return TRUE; + if (tm_count == 800) + { + if (profile_passed_limit(nfa_time_limit)) + { + if (nfa_timed_out != NULL) + *nfa_timed_out = TRUE; + return TRUE; + } + // Only reset the count when not timed out, so that when it did + // timeout it keeps timing out until the time limit is changed. + tm_count = 0; + } + else + ++tm_count; } return FALSE; } -- cgit v1.2.1