From 48befd67b565efcb218849644c9dc1f728e2f095 Mon Sep 17 00:00:00 2001
From: Todd Gamblin <tgamblin@llnl.gov>
Date: Mon, 16 Dec 2019 16:56:55 -0800
Subject: [PATCH] performance: memoize spack.architecture.get_platform()

`get_platform()` is pretty expensive and can be called many times in a
spack invocation.

- [x] memoize `get_platform()`
---
 lib/spack/spack/architecture.py    | 1 +
 lib/spack/spack/test/concretize.py | 7 +++++++
 2 files changed, 8 insertions(+)

diff --git a/lib/spack/spack/architecture.py b/lib/spack/spack/architecture.py
index 0e318bbccf..7552795cd2 100644
--- a/lib/spack/spack/architecture.py
+++ b/lib/spack/spack/architecture.py
@@ -436,6 +436,7 @@ def from_dict(d):
         return arch_for_spec(spec)
 
 
+@memoized
 def get_platform(platform_name):
     """Returns a platform object that corresponds to the given name."""
     platform_list = all_platforms()
diff --git a/lib/spack/spack/test/concretize.py b/lib/spack/spack/test/concretize.py
index e0774909f4..4b17e755ed 100644
--- a/lib/spack/spack/test/concretize.py
+++ b/lib/spack/spack/test/concretize.py
@@ -94,6 +94,10 @@ def current_host(request, monkeypatch):
     # preferred target via packages.yaml
     cpu, _, is_preference = request.param.partition('-')
     target = llnl.util.cpu.targets[cpu]
+
+    # this function is memoized, so clear its state for testing
+    spack.architecture.get_platform.cache.clear()
+
     if not is_preference:
         monkeypatch.setattr(llnl.util.cpu, 'host', lambda: target)
         monkeypatch.setattr(spack.platforms.test.Test, 'default', cpu)
@@ -104,6 +108,9 @@ def current_host(request, monkeypatch):
         with spack.config.override('packages:all', {'target': [cpu]}):
             yield target
 
+    # clear any test values fetched
+    spack.architecture.get_platform.cache.clear()
+
 
 @pytest.mark.usefixtures('config', 'mock_packages')
 class TestConcretize(object):
-- 
GitLab