Skip to content

Commit 325b821

Browse files
skip trying to substitute macros into lines that do not contain them
Currently, we try to substitute all macros into all lines by calling `subn` on the compiled regex, which results in an unmodified line if the line did not contain the macro at all. This call to `subn` is still quite expensive (runtime-wise). To spare the cost, we now first check whether the line actually contains the macro text and skip the substitution check otherwise. Please note that a line containing the macro text does not necessarily mean that a substitution will take place, since the simple containment is less strict than the regex (which checks that the macro is an actual token separated from the sounding context, as in `FOO` being in `FOO_BAR` but it should not be substituted).
1 parent 1f54125 commit 325b821

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

fortls/parse_fortran.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,6 +2234,10 @@ def replace_vars(line: str):
22342234

22352235
# Substitute (if any) read in preprocessor macros
22362236
for def_tmp, value in defs_tmp.items():
2237+
# Skip if the line does not contain the macro at all. This is supposed to
2238+
# spare the expensive regex-substitution in case we do not need it at all
2239+
if def_tmp not in line:
2240+
continue
22372241
def_regex = def_regexes.get(def_tmp)
22382242
if def_regex is None:
22392243
def_regex = re.compile(rf"\b{def_tmp}\b")

0 commit comments

Comments
 (0)