From 5dd3413b717a470955ad0c6603d6d129ce2f5590 Mon Sep 17 00:00:00 2001 From: Nirbheek Chauhan Date: Mon, 19 Dec 2022 18:45:39 +0530 Subject: [PATCH] meson: Cache os.path.realpath in CLikeCompilerArgs Profiling showed that we were spending 25s inside os.path.realpath() on Windows while generating compile lines for build.ninja, inside NinjaBackend.generate() The real path for these will not (should not) change during a single meson invocation, so cache all these. Brings build.ninja generation from 73s to 47s on my machine. --- mesonbuild/compilers/mixins/clike.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/mesonbuild/compilers/mixins/clike.py b/mesonbuild/compilers/mixins/clike.py index 8736c6d97..4449aa7d1 100644 --- a/mesonbuild/compilers/mixins/clike.py +++ b/mesonbuild/compilers/mixins/clike.py @@ -101,7 +101,7 @@ class CLikeCompilerArgs(arglist.CompilerArgs): # Remove system/default include paths added with -isystem default_dirs = self.compiler.get_default_include_dirs() if default_dirs: - real_default_dirs = [os.path.realpath(i) for i in default_dirs] + real_default_dirs = [self._cached_realpath(i) for i in default_dirs] bad_idx_list = [] # type: T.List[int] for i, each in enumerate(new): if not each.startswith('-isystem'): @@ -110,16 +110,21 @@ class CLikeCompilerArgs(arglist.CompilerArgs): # Remove the -isystem and the path if the path is a default path if (each == '-isystem' and i < (len(new) - 1) and - os.path.realpath(new[i + 1]) in real_default_dirs): + self._cached_realpath(new[i + 1]) in real_default_dirs): bad_idx_list += [i, i + 1] - elif each.startswith('-isystem=') and os.path.realpath(each[9:]) in real_default_dirs: + elif each.startswith('-isystem=') and self._cached_realpath(each[9:]) in real_default_dirs: bad_idx_list += [i] - elif os.path.realpath(each[8:]) in real_default_dirs: + elif self._cached_realpath(each[8:]) in real_default_dirs: bad_idx_list += [i] for i in reversed(bad_idx_list): new.pop(i) return self.compiler.unix_args_to_native(new._container) + @staticmethod + @functools.lru_cache(maxsize=None) + def _cached_realpath(arg: str) -> str: + return os.path.realpath(arg) + def __repr__(self) -> str: self.flush_pre_post() return f'CLikeCompilerArgs({self.compiler!r}, {self._container!r})'