From 615cbbc94d5a5438eeb108cd9892abf349959671 Mon Sep 17 00:00:00 2001
From: Marc Bonnici <marc.bonnici@arm.com>
Date: Fri, 26 Jun 2020 11:12:01 +0100
Subject: [PATCH] fw/target_info: Prevent multiple parses of the
 target_info_cache

Instead of parsing the target_info_cache multiple times,allow for
it to be read it once and passed as a paramter to the coresponding
methods.
---
 wa/framework/target/info.py    | 10 ++++++----
 wa/framework/target/manager.py | 10 ++++++----
 2 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/wa/framework/target/info.py b/wa/framework/target/info.py
index 5e9f1ba7..7bf4ddc3 100644
--- a/wa/framework/target/info.py
+++ b/wa/framework/target/info.py
@@ -292,8 +292,9 @@ def write_target_info_cache(cache):
         write_pod(cache, settings.target_info_cache_file)
 
 
-def get_target_info_from_cache(system_id):
-    cache = read_target_info_cache()
+def get_target_info_from_cache(system_id, cache=None):
+    if cache is None:
+        cache = read_target_info_cache()
     pod = cache.get(system_id, None)
 
     if not pod:
@@ -307,8 +308,9 @@ def get_target_info_from_cache(system_id):
     return TargetInfo.from_pod(pod)
 
 
-def cache_target_info(target_info, overwrite=False):
-    cache = read_target_info_cache()
+def cache_target_info(target_info, overwrite=False, cache=None):
+    if cache is None:
+        cache = read_target_info_cache()
     if target_info.system_id in cache and not overwrite:
         raise ValueError('TargetInfo for {} is already in cache.'.format(target_info.system_id))
     cache[target_info.system_id] = target_info.to_pod()
diff --git a/wa/framework/target/manager.py b/wa/framework/target/manager.py
index 114a5890..bc756aa3 100644
--- a/wa/framework/target/manager.py
+++ b/wa/framework/target/manager.py
@@ -24,7 +24,8 @@ from wa.framework.plugin import Parameter
 from wa.framework.target.descriptor import (get_target_description,
                                             instantiate_target,
                                             instantiate_assistant)
-from wa.framework.target.info import get_target_info, get_target_info_from_cache, cache_target_info
+from wa.framework.target.info import (get_target_info, get_target_info_from_cache,
+                                      cache_target_info, read_target_info_cache)
 from wa.framework.target.runtime_parameter_manager import RuntimeParameterManager
 from wa.utils.types import module_name_set
 
@@ -92,18 +93,19 @@ class TargetManager(object):
 
     @memoized
     def get_target_info(self):
-        info = get_target_info_from_cache(self.target.system_id)
+        cache = read_target_info_cache()
+        info = get_target_info_from_cache(self.target.system_id, cache=cache)
 
         if info is None:
             info = get_target_info(self.target)
-            cache_target_info(info)
+            cache_target_info(info, cache=cache)
         else:
             # If module configuration has changed form when the target info
             # was previously cached, it is possible additional info will be
             # available, so should re-generate the cache.
             if module_name_set(info.modules) != module_name_set(self.target.modules):
                 info = get_target_info(self.target)
-                cache_target_info(info, overwrite=True)
+                cache_target_info(info, overwrite=True, cache=cache)
 
         return info