summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <daniel.silverstone@codethink.co.uk>2014-03-28 15:01:05 +0000
committerDaniel Silverstone <daniel.silverstone@codethink.co.uk>2014-03-28 15:01:05 +0000
commit8db794a8e9565fbc9dda993fc7347436a53dddd4 (patch)
tree657a6891ad531e8f71784847b42b3c2e625d7604
parentf56259c2ce62c301f0e0f9dd7c85001b45fc8f5c (diff)
parent6d9eee86f1ea75c0f2666dc4282d8b10fee63e7b (diff)
downloadmorph-8db794a8e9565fbc9dda993fc7347436a53dddd4.tar.gz
Merge branch 'baserock/danielsilverstone/memoise-cache-keys-better'
Reviewed-By: Lars Wirzenius <lars.wirzenius@codethink.co.uk>
-rw-r--r--morphlib/cachekeycomputer.py33
-rw-r--r--morphlib/cachekeycomputer_tests.py13
2 files changed, 36 insertions, 10 deletions
diff --git a/morphlib/cachekeycomputer.py b/morphlib/cachekeycomputer.py
index 3efe1cbb..ca374436 100644
--- a/morphlib/cachekeycomputer.py
+++ b/morphlib/cachekeycomputer.py
@@ -25,6 +25,7 @@ class CacheKeyComputer(object):
def __init__(self, build_env):
self._build_env = build_env
self._calculated = {}
+ self._hashed = {}
def _filterenv(self, env):
keys = ["LOGNAME", "MORPH_ARCH", "TARGET", "TARGET_STAGE1",
@@ -32,11 +33,18 @@ class CacheKeyComputer(object):
return dict([(k, env[k]) for k in keys])
def compute_key(self, artifact):
- logging.debug('computing cache key for artifact %s from source '
- 'repo %s, sha1 %s, filename %s' %
- (artifact.name, artifact.source.repo_name,
- artifact.source.sha1, artifact.source.filename))
- return self._hash_id(self.get_cache_id(artifact))
+ try:
+ ret = self._hashed[artifact]
+ logging.debug('returning cached key for artifact %s from source ',
+ (artifact.name, artifact.source.repo_name,
+ artifact.source.sha1, artifact.source.filename))
+ return ret
+ except KeyError:
+ logging.debug('computing cache key for artifact %s from source ',
+ (artifact.name, artifact.source.repo_name,
+ artifact.source.sha1, artifact.source.filename))
+ self._hashed[artifact] = self._hash_id(self.get_cache_id(artifact))
+ return self._hashed[artifact]
def _hash_id(self, cache_id):
sha = hashlib.sha256()
@@ -66,13 +74,18 @@ class CacheKeyComputer(object):
self._hash_thing(sha, item)
def get_cache_id(self, artifact):
- logging.debug('computing cache id for artifact %s from source '
- 'repo %s, sha1 %s, filename %s' %
- (artifact.name, artifact.source.repo_name,
- artifact.source.sha1, artifact.source.filename))
try:
- return self._calculated[artifact]
+ ret = self._calculated[artifact]
+ logging.debug('returning cached id for artifact %s from source '
+ 'repo %s, sha1 %s, filename %s' %
+ (artifact.name, artifact.source.repo_name,
+ artifact.source.sha1, artifact.source.filename))
+ return ret
except KeyError:
+ logging.debug('computing cache id for artifact %s from source '
+ 'repo %s, sha1 %s, filename %s' %
+ (artifact.name, artifact.source.repo_name,
+ artifact.source.sha1, artifact.source.filename))
cacheid = self._calculate(artifact)
self._calculated[artifact] = cacheid
return cacheid
diff --git a/morphlib/cachekeycomputer_tests.py b/morphlib/cachekeycomputer_tests.py
index 4e73e905..dd10307f 100644
--- a/morphlib/cachekeycomputer_tests.py
+++ b/morphlib/cachekeycomputer_tests.py
@@ -148,6 +148,19 @@ class CacheKeyComputerTests(unittest.TestCase):
validchars = '0123456789abcdef'
return len(s) == 64 and all([c in validchars for c in s])
+ def test_compute_twice_same_key(self):
+ artifact = self._find_artifact('system-rootfs')
+ self.assertEqual(self.ckc.compute_key(artifact),
+ self.ckc.compute_key(artifact))
+
+ def test_compute_twice_same_id(self):
+ artifact = self._find_artifact('system-rootfs')
+ id1 = self.ckc.get_cache_id(artifact)
+ id2 = self.ckc.get_cache_id(artifact)
+ hash1 = self.ckc._hash_id(id1)
+ hash2 = self.ckc._hash_id(id2)
+ self.assertEqual(hash1, hash2)
+
def test_compute_key_returns_sha256(self):
artifact = self._find_artifact('system-rootfs')
self.assertTrue(self._valid_sha256(