From 1c878fcda085de502fe618bb4a022c14dacbd47a Mon Sep 17 00:00:00 2001 From: Adam Simpkins Date: Fri, 19 Jul 2019 15:23:50 -0700 Subject: [PATCH] cache results of path_search() Summary: getdeps currently ends up calling `path_search()` repeatedly searching for various C/C++ compilers in $PATH. It ends up doing this multiple times for each dependency as it computes the project hashes. This updates the `path_search()` function to cache its results so that we don't keep performing the same searches on the file system over and over again. Reviewed By: chadaustin Differential Revision: D16354625 fbshipit-source-id: 116293bd2f636632517d26436b2332e6c10624f1 --- build/fbcode_builder/getdeps/envfuncs.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/build/fbcode_builder/getdeps/envfuncs.py b/build/fbcode_builder/getdeps/envfuncs.py index 737132e48..568f36cd1 100644 --- a/build/fbcode_builder/getdeps/envfuncs.py +++ b/build/fbcode_builder/getdeps/envfuncs.py @@ -150,6 +150,10 @@ def add_flag(env, name, flag, append=True): env.set(name, " ".join(val)) +_path_search_cache = {} +_not_found = object() + + def path_search(env, exename, defval=None): """ Search for exename in the PATH specified in env. exename is eg: `ninja` and this function knows to append a .exe @@ -161,6 +165,18 @@ def path_search(env, exename, defval=None): if path is None: return defval + # The project hash computation code searches for C++ compilers (g++, clang, etc) + # repeatedly. Cache the result so we don't end up searching for these over and over + # again. + cache_key = (path, exename) + result = _path_search_cache.get(cache_key, _not_found) + if result is _not_found: + result = _perform_path_search(path, exename) + _path_search_cache[cache_key] = result + return result + + +def _perform_path_search(path, exename): is_win = sys.platform.startswith("win") if is_win: exename = "%s.exe" % exename