From ca42c7029f0a1e4c99bd6f173521662eed3b91f9 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Thu, 31 Aug 2023 15:25:31 -0400 Subject: [PATCH] msubprojects: Speedup subproject_dir extraction The interpreter takes significant amount of time to initialize everything in project() function. We only need to extract a string from AST, just like we do in handle_meson_version_from_ast(). --- mesonbuild/ast/introspection.py | 19 +++++++++++++++++++ mesonbuild/msubprojects.py | 8 +++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/mesonbuild/ast/introspection.py b/mesonbuild/ast/introspection.py index d66e73f3e..d5203822e 100644 --- a/mesonbuild/ast/introspection.py +++ b/mesonbuild/ast/introspection.py @@ -362,3 +362,22 @@ class IntrospectionInterpreter(AstInterpreter): self.sanity_check_ast() self.parse_project() self.run() + + def extract_subproject_dir(self) -> T.Optional[str]: + '''Fast path to extract subproject_dir kwarg. + This is faster than self.parse_project() which also initialize options + and also calls parse_project() on every subproject. + ''' + if not self.ast.lines: + return + project = self.ast.lines[0] + # first line is always project() + if not isinstance(project, FunctionNode): + return + for kw, val in project.args.kwargs.items(): + assert isinstance(kw, IdNode), 'for mypy' + if kw.value == 'subproject_dir': + # mypy does not understand "and isinstance" + if isinstance(val, StringNode): + return val.value + return None diff --git a/mesonbuild/msubprojects.py b/mesonbuild/msubprojects.py index 3ecfba18f..8f3231c11 100755 --- a/mesonbuild/msubprojects.py +++ b/mesonbuild/msubprojects.py @@ -14,7 +14,7 @@ import tarfile import zipfile from . import mlog -from .ast import IntrospectionInterpreter, AstIDGenerator +from .ast import IntrospectionInterpreter from .mesonlib import quiet_git, GitException, Popen_safe, MesonException, windows_proof_rmtree from .wrap.wrap import (Resolver, WrapException, ALL_TYPES, PackageDefinition, parse_patch_url, update_wrap_file, get_releases) @@ -691,11 +691,9 @@ def run(options: 'Arguments') -> int: mlog.error('Directory', mlog.bold(source_dir), 'does not seem to be a Meson source directory.') return 1 with mlog.no_logging(): - intr = IntrospectionInterpreter(source_dir, '', 'none', visitors = [AstIDGenerator()]) + intr = IntrospectionInterpreter(source_dir, '', 'none') intr.load_root_meson_file() - intr.sanity_check_ast() - intr.parse_project() - subproject_dir = intr.subproject_dir + subproject_dir = intr.extract_subproject_dir() or 'subprojects' if not os.path.isdir(os.path.join(source_dir, subproject_dir)): mlog.log('Directory', mlog.bold(source_dir), 'does not seem to have subprojects.') return 0