From 325b821b5d6afbc6cb6fefca1c3c6e0efee0bb04 Mon Sep 17 00:00:00 2001 From: Albert Ziegenhagel Date: Thu, 22 Jun 2023 11:06:12 +0200 Subject: [PATCH] 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). --- fortls/parse_fortran.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fortls/parse_fortran.py b/fortls/parse_fortran.py index fa1d9dfe..1c34e649 100644 --- a/fortls/parse_fortran.py +++ b/fortls/parse_fortran.py @@ -2234,6 +2234,10 @@ def replace_vars(line: str): # Substitute (if any) read in preprocessor macros for def_tmp, value in defs_tmp.items(): + # Skip if the line does not contain the macro at all. This is supposed to + # spare the expensive regex-substitution in case we do not need it at all + if def_tmp not in line: + continue def_regex = def_regexes.get(def_tmp) if def_regex is None: def_regex = re.compile(rf"\b{def_tmp}\b")