You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildstream.apache.org by gi...@apache.org on 2020/12/29 13:06:30 UTC

[buildstream] 03/05: _platform/platform.py: Cache result from 'get_host_arch'

This is an automated email from the ASF dual-hosted git repository.

github-bot pushed a commit to branch bschubert/optimize-element-init
in repository https://gitbox.apache.org/repos/asf/buildstream.git

commit e4c86c563aeab2f2a0a8cb8d29d1a4645c045254
Author: Benjamin Schubert <co...@benschubert.me>
AuthorDate: Thu Oct 10 16:05:23 2019 +0100

    _platform/platform.py: Cache result from 'get_host_arch'
    
    'get_host_arch' is an expensive method and is called twice per element.
    Caching gives us some performance benefits.
---
 src/buildstream/_platform/platform.py | 16 +++++++++++-----
 tests/format/optionarch.py            |  8 ++++++++
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/src/buildstream/_platform/platform.py b/src/buildstream/_platform/platform.py
index 11c9217..ca5fb75 100644
--- a/src/buildstream/_platform/platform.py
+++ b/src/buildstream/_platform/platform.py
@@ -30,6 +30,9 @@ from .. import utils
 
 
 class Platform():
+
+    _host_arch = None  # cache to not recompute the host arch every time
+
     # Platform()
     #
     # A class to manage platform-specific details. Currently holds the
@@ -167,11 +170,14 @@ class Platform():
     #
     # Returns:
     #    (string): String representing the architecture
-    @staticmethod
-    def get_host_arch():
-        # get the hardware identifier from uname
-        uname_machine = platform.uname().machine
-        return Platform.canonicalize_arch(uname_machine)
+    @classmethod
+    def get_host_arch(cls):
+        if cls._host_arch is None:
+            # get the hardware identifier from uname
+            uname_machine = platform.uname().machine
+            cls._host_arch = Platform.canonicalize_arch(uname_machine)
+
+        return cls._host_arch
 
     # does_multiprocessing_start_require_pickling():
     #
diff --git a/tests/format/optionarch.py b/tests/format/optionarch.py
index f347e27..4451a22 100644
--- a/tests/format/optionarch.py
+++ b/tests/format/optionarch.py
@@ -7,6 +7,7 @@ import pytest
 
 from buildstream import _yaml
 from buildstream._exceptions import ErrorDomain, LoadErrorReason
+from buildstream._platform import Platform
 from buildstream.testing.runcli import cli  # pylint: disable=unused-import
 
 from tests.testutils import override_platform_uname
@@ -15,6 +16,13 @@ from tests.testutils import override_platform_uname
 DATA_DIR = os.path.dirname(os.path.realpath(__file__))
 
 
+@pytest.fixture(autouse=True)
+def reset_arch():
+    # The platform currently caches the host arch. We need to clean that up here.
+    Platform._host_arch = None
+    yield
+    Platform._host_arch = None
+
 @pytest.mark.datafiles(DATA_DIR)
 @pytest.mark.parametrize("machine,value,expected", [
     # Test explicitly provided arches