[PATCH v2 17/25] docs: kdoc_re: don't recompile NextMatch regex every time

Mauro Carvalho Chehab posted 25 patches 1 week, 3 days ago
There is a newer version of this series
[PATCH v2 17/25] docs: kdoc_re: don't recompile NextMatch regex every time
Posted by Mauro Carvalho Chehab 1 week, 3 days ago
Store delimiters and its regex-compiled version as const vars.

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
---
 tools/lib/python/kdoc/kdoc_re.py | 35 ++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 13 deletions(-)

diff --git a/tools/lib/python/kdoc/kdoc_re.py b/tools/lib/python/kdoc/kdoc_re.py
index f082f82bad67..b6e11ee0be21 100644
--- a/tools/lib/python/kdoc/kdoc_re.py
+++ b/tools/lib/python/kdoc/kdoc_re.py
@@ -81,6 +81,13 @@ class KernRe:
         self.last_match = self.regex.search(string)
         return self.last_match
 
+    def finditer(self,  string):
+        """
+        Alias to re.finditer.
+        """
+
+        return self.regex.finditer(string)
+
     def findall(self, string):
         """
         Alias to re.findall.
@@ -116,6 +123,16 @@ class KernRe:
 
         return self.last_match.groups()
 
+#: Nested delimited pairs (brackets and parenthesis)
+DELIMITER_PAIRS = {
+    '{': '}',
+    '(': ')',
+    '[': ']',
+}
+
+#: compiled delimiters
+RE_DELIM = KernRe(r'[\{\}\[\]\(\)]')
+
 
 class NestedMatch:
     """
@@ -165,14 +182,6 @@ class NestedMatch:
     #
     #   FOO(arg1, arg2, arg3)
 
-    DELIMITER_PAIRS = {
-        '{': '}',
-        '(': ')',
-        '[': ']',
-    }
-
-    RE_DELIM = re.compile(r'[\{\}\[\]\(\)]')
-
     def _search(self, regex, line):
         """
         Finds paired blocks for a regex that ends with a delimiter.
@@ -202,13 +211,13 @@ class NestedMatch:
             escape = False
 
             d = line[offset - 1]
-            if d not in self.DELIMITER_PAIRS:
+            if d not in DELIMITER_PAIRS:
                 continue
 
-            end = self.DELIMITER_PAIRS[d]
+            end = DELIMITER_PAIRS[d]
             stack.append(end)
 
-            for match in self.RE_DELIM.finditer(line[offset:]):
+            for match in RE_DELIM.finditer(line[offset:]):
                 pos = match.start() + offset
 
                 d = line[pos]
@@ -229,8 +238,8 @@ class NestedMatch:
                     string_char = d
                     continue
 
-                if d in self.DELIMITER_PAIRS:
-                    end = self.DELIMITER_PAIRS[d]
+                if d in DELIMITER_PAIRS:
+                    end = DELIMITER_PAIRS[d]
 
                     stack.append(end)
                     continue
-- 
2.52.0
RE: [Intel-wired-lan] [PATCH v2 17/25] docs: kdoc_re: don't recompile NextMatch regex every time
Posted by Loktionov, Aleksandr 1 week, 3 days ago

> -----Original Message-----
> From: Intel-wired-lan <intel-wired-lan-bounces@osuosl.org> On Behalf
> Of Mauro Carvalho Chehab
> Sent: Wednesday, January 28, 2026 5:50 PM
> To: Jonathan Corbet <corbet@lwn.net>; Linux Doc Mailing List <linux-
> doc@vger.kernel.org>
> Cc: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>;
> bpf@vger.kernel.org; intel-wired-lan@lists.osuosl.org; linux-
> kernel@vger.kernel.org; netdev@vger.kernel.org; Peter Zijlstra
> <peterz@infradead.org>; Randy Dunlap <rdunlap@infradead.org>; Stephen
> Rothwell <sfr@canb.auug.org.au>
> Subject: [Intel-wired-lan] [PATCH v2 17/25] docs: kdoc_re: don't
> recompile NextMatch regex every time
> 
> Store delimiters and its regex-compiled version as const vars.
> 
> Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
> ---
>  tools/lib/python/kdoc/kdoc_re.py | 35 ++++++++++++++++++++-----------
> -
>  1 file changed, 22 insertions(+), 13 deletions(-)
> 
> diff --git a/tools/lib/python/kdoc/kdoc_re.py
> b/tools/lib/python/kdoc/kdoc_re.py
> index f082f82bad67..b6e11ee0be21 100644
> --- a/tools/lib/python/kdoc/kdoc_re.py
> +++ b/tools/lib/python/kdoc/kdoc_re.py
> @@ -81,6 +81,13 @@ class KernRe:
>          self.last_match = self.regex.search(string)
>          return self.last_match
> 
> +    def finditer(self,  string):
> +        """
> +        Alias to re.finditer.
> +        """
> +
> +        return self.regex.finditer(string)
> +
>      def findall(self, string):
>          """
>          Alias to re.findall.
> @@ -116,6 +123,16 @@ class KernRe:
> 
>          return self.last_match.groups()
> 
> +#: Nested delimited pairs (brackets and parenthesis) DELIMITER_PAIRS
> =
> +{
> +    '{': '}',
> +    '(': ')',
> +    '[': ']',
> +}
> +
> +#: compiled delimiters
> +RE_DELIM = KernRe(r'[\{\}\[\]\(\)]')
> +
> 
>  class NestedMatch:
>      """
> @@ -165,14 +182,6 @@ class NestedMatch:
>      #
>      #   FOO(arg1, arg2, arg3)
> 
> -    DELIMITER_PAIRS = {
> -        '{': '}',
> -        '(': ')',
> -        '[': ']',
> -    }
> -
> -    RE_DELIM = re.compile(r'[\{\}\[\]\(\)]')
> -
>      def _search(self, regex, line):
>          """
>          Finds paired blocks for a regex that ends with a delimiter.
> @@ -202,13 +211,13 @@ class NestedMatch:
>              escape = False
> 
>              d = line[offset - 1]
> -            if d not in self.DELIMITER_PAIRS:
> +            if d not in DELIMITER_PAIRS:
>                  continue
> 
> -            end = self.DELIMITER_PAIRS[d]
> +            end = DELIMITER_PAIRS[d]
>              stack.append(end)
> 
> -            for match in self.RE_DELIM.finditer(line[offset:]):
> +            for match in RE_DELIM.finditer(line[offset:]):
>                  pos = match.start() + offset
> 
>                  d = line[pos]
> @@ -229,8 +238,8 @@ class NestedMatch:
>                      string_char = d
>                      continue
> 
> -                if d in self.DELIMITER_PAIRS:
> -                    end = self.DELIMITER_PAIRS[d]
> +                if d in DELIMITER_PAIRS:
> +                    end = DELIMITER_PAIRS[d]
> 
>                      stack.append(end)
>                      continue
> --
> 2.52.0
Reviewed-by: Aleksandr Loktionov <aleksandr.loktionov@intel.com>