From 34b43a9f5bfa49c5b19f1a237c7c384e05f57cf8 Mon Sep 17 00:00:00 2001 From: Antoine Wacheux Date: Wed, 13 Dec 2017 08:55:43 +0000 Subject: plugins/sources/local.py: Cache the local element's unique key This avoid multiple file system traversal when the key is requested multiple times. --- buildstream/plugins/sources/local.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/buildstream/plugins/sources/local.py b/buildstream/plugins/sources/local.py index d513b897e..e2a769e1c 100644 --- a/buildstream/plugins/sources/local.py +++ b/buildstream/plugins/sources/local.py @@ -41,6 +41,12 @@ from buildstream import utils class LocalSource(Source): + def __init__(self, context, project, meta): + super().__init__(context, project, meta) + + # Cached unique key to avoid multiple file system traversal if the unique key is requested multiple times. + self.__unique_key = None + def configure(self, node): self.node_validate(node, ['path'] + Source.COMMON_CONFIG_KEYS) @@ -53,16 +59,18 @@ class LocalSource(Source): raise SourceError("Specified path '{}' does not exist".format(self.path)) def get_unique_key(self): - # Get a list of tuples of the the project relative paths and fullpaths - if os.path.isdir(self.fullpath): - filelist = utils.list_relative_paths(self.fullpath) - filelist = [(relpath, os.path.join(self.fullpath, relpath)) for relpath in filelist] - else: - filelist = [(self.path, self.fullpath)] - - # Return a list of (relative filename, sha256 digest) tuples, a sorted list - # has already been returned by list_relative_paths() - return [(relpath, unique_key(fullpath)) for relpath, fullpath in filelist] + if self.__unique_key is None: + # Get a list of tuples of the the project relative paths and fullpaths + if os.path.isdir(self.fullpath): + filelist = utils.list_relative_paths(self.fullpath) + filelist = [(relpath, os.path.join(self.fullpath, relpath)) for relpath in filelist] + else: + filelist = [(self.path, self.fullpath)] + + # Return a list of (relative filename, sha256 digest) tuples, a sorted list + # has already been returned by list_relative_paths() + self.__unique_key = [(relpath, unique_key(fullpath)) for relpath, fullpath in filelist] + return self.__unique_key def get_consistency(self): return Consistency.CACHED -- cgit v1.2.1