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
This commit is contained in:
Joshua Harlow
2014-11-25 15:40:07 -08:00
parent 96a387f66f
commit a69213821c

View File

@@ -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...