From a69213821c34579ec224fa9a145ccc94d6bfa324 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Tue, 25 Nov 2014 15:40:07 -0800 Subject: [PATCH] Cache immutable visible scopes in the runtime component Instead of recalculating/rewalking over the visible scopes of atoms just calculate the scope once and cache it (as it currently does not change at runtime) and return the cached tuple instead to avoid the needless recreation whenever a scope is requested for a given atom. Change-Id: I47d24054c63e8620d26e7ade4baa239295daed0a --- taskflow/engines/action_engine/runtime.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/taskflow/engines/action_engine/runtime.py b/taskflow/engines/action_engine/runtime.py index de092f5e..ec0b44d4 100644 --- a/taskflow/engines/action_engine/runtime.py +++ b/taskflow/engines/action_engine/runtime.py @@ -40,6 +40,7 @@ class Runtime(object): self._task_executor = task_executor self._storage = storage self._compilation = compilation + self._scopes = {} @property def compilation(self): @@ -68,17 +69,23 @@ class Runtime(object): @misc.cachedproperty def retry_action(self): return ra.RetryAction(self._storage, self._atom_notifier, - lambda atom: sc.ScopeWalker(self.compilation, - atom, - names_only=True)) + self._fetch_scopes_for) @misc.cachedproperty def task_action(self): return ta.TaskAction(self._storage, self._task_executor, - self._atom_notifier, - lambda atom: sc.ScopeWalker(self.compilation, - atom, - names_only=True)) + self._atom_notifier, self._fetch_scopes_for) + + def _fetch_scopes_for(self, atom): + """Fetches a tuple of the visible scopes for the given atom.""" + try: + return self._scopes[atom] + except KeyError: + walker = sc.ScopeWalker(self.compilation, atom, + names_only=True) + visible_to = tuple(walker) + self._scopes[atom] = visible_to + return visible_to # Various helper methods used by the runtime components; not for public # consumption...