From 8197d724a6eac77766f6505683bd9cad2e82a039 Mon Sep 17 00:00:00 2001 From: Aravind Vasudevan Date: Thu, 30 Nov 2023 23:29:09 +0000 Subject: [PATCH] [detect_host_arch] Cache HostArch() function From my recent go/gclient-sync-no-ops analysis, I noticed that HostArch() is repeated called within get_builtin_vars() fn. The `platform.architecture()` call made in arm-based systems and some x64 is expensive and adds to gclient sync's runtime when repeatedly called. When caching HostArch(), I noticed 22.4% speed-up in incremental no-op gclient sync when run on arm-based machine. Change-Id: I962c4fb0879d2931268f5bec1678ef782c56e7f2 Bug: 5076224 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5076226 Commit-Queue: Aravind Vasudevan Reviewed-by: Josip Sokcevic --- detect_host_arch.py | 2 ++ tests/detect_host_arch_test.py | 1 + 2 files changed, 3 insertions(+) diff --git a/detect_host_arch.py b/detect_host_arch.py index 7669d9ec88..266b457bf7 100755 --- a/detect_host_arch.py +++ b/detect_host_arch.py @@ -4,10 +4,12 @@ # found in the LICENSE file. """Outputs host CPU architecture in format recognized by gyp.""" +from functools import lru_cache import platform import re +@lru_cache(maxsize=None) def HostArch(): """Returns the host architecture with a predictable string.""" host_arch = platform.machine().lower() diff --git a/tests/detect_host_arch_test.py b/tests/detect_host_arch_test.py index f3e8ebaa06..f5a8bda174 100644 --- a/tests/detect_host_arch_test.py +++ b/tests/detect_host_arch_test.py @@ -47,6 +47,7 @@ class DetectHostArchTest(unittest.TestCase): platform.machine.return_value = machine platform.processor.return_value = processor platform.architecture.return_value = arch + detect_host_arch.HostArch.cache_clear() self.assertEqual(expected, detect_host_arch.HostArch())