]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
scripts/lib/abi/abi_parser.py: optimize parse_abi() function
authorMauro Carvalho Chehab <mchehab+huawei@kernel.org>
Mon, 10 Feb 2025 10:18:02 +0000 (11:18 +0100)
committerJonathan Corbet <corbet@lwn.net>
Mon, 10 Feb 2025 18:19:56 +0000 (11:19 -0700)
Instead of using glob, use a recursive function to parse all files.

Such change reduces the total excecution time by 15% with my SSD disks.

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Link: https://lore.kernel.org/r/190dd358897017ed82c56f1e263192215ffbae43.1739182025.git.mchehab+huawei@kernel.org
scripts/lib/abi/abi_parser.py

index bea7f1a7616584f1b65f3b09e24cee759051f60a..6052a8aec443e37bd3f0b5694a68fe4e085fe52c 100644 (file)
@@ -12,7 +12,6 @@ import logging
 import os
 import re
 
-from glob import glob
 from pprint import pformat
 from random import randrange, seed
 
@@ -46,7 +45,11 @@ class AbiParser:
         self.file_refs = {}
         self.what_refs = {}
 
+        # Ignore files that contain such suffixes
+        self.ignore_suffixes = (".rej", ".org", ".orig", ".bak", "~")
+
         # Regular expressions used on parser
+        self.re_abi_dir = re.compile(r"(.*)" + ABI_DIR)
         self.re_tag = re.compile(r"(\S+)(:\s*)(.*)", re.I)
         self.re_valid = re.compile(self.TAGS)
         self.re_start_spc = re.compile(r"(\s*)(\S.*)")
@@ -322,26 +325,42 @@ class AbiParser:
                 for w in fdata.what:
                     self.add_symbol(what=w, fname=fname, xref=fdata.key)
 
-    def parse_abi(self):
-        """Parse documentation ABI"""
+    def _parse_abi(self, root=None):
+        """Internal function to parse documentation ABI recursively"""
 
-        ignore_suffixes = ("rej", "org", "orig", "bak", "~")
-        re_abi = re.compile(r".*" + ABI_DIR)
+        if not root:
+            root = self.directory
 
-        for fname in glob(os.path.join(self.directory, "**"), recursive=True):
-            if os.path.isdir(fname):
-                continue
+        with os.scandir(root) as obj:
+            for entry in obj:
+                name = os.path.join(root, entry.name)
 
-            basename = os.path.basename(fname)
+                if entry.is_dir():
+                    self.parse_abi(name)
+                    continue
 
-            if basename == "README":
-                continue
-            if basename.startswith(".") or basename.endswith(ignore_suffixes):
-                continue
+                if not entry.is_file():
+                    continue
+
+                basename = os.path.basename(name)
 
-            path = re_abi.sub("", os.path.dirname(fname))
+                if basename == "README":
+                    continue
+
+                if basename.startswith("."):
+                    continue
+
+                if basename.endswith(self.ignore_suffixes):
+                    continue
+
+                path = self.re_abi_dir.sub("", os.path.dirname(name))
+
+                self.parse_file(name, path, basename)
+
+    def parse_abi(self, root=None):
+        """Parse documentation ABI"""
 
-            self.parse_file(fname, path, basename)
+        self._parse_abi(root)
 
         if self.debug & AbiDebug.DUMP_ABI_STRUCTS:
             self.log.debug(pformat(self.data))