Skip to content

Commit

Permalink
Merge pull request #1032 from rizar/add_cache_to_persistent_load
Browse files Browse the repository at this point in the history
Add cache in _PersistentLoad
  • Loading branch information
dwf committed Mar 15, 2016
2 parents 0e1696d + 59c22e7 commit 83079b7
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions blocks/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,10 +563,16 @@ def __init__(self, tar_file):
if '_parameters' in tar_file.getnames():
self.parameters = numpy.load(
tar_file.extractfile(tar_file.getmember('_parameters')))
self._cache = {}

def __call__(self, id_):
components = _unmangle_parameter_name(id_)
return components[0](self.parameters[components[1]])
# As we empirically found out, this method can be called multiple
# times with the same id_. That's why we need a cache here to
# avoid creating the same object more than once.
if id_ not in self._cache:
components = _unmangle_parameter_name(id_)
self._cache[id_] = components[0](self.parameters[components[1]])
return self._cache[id_]


def _mangle_parameter_name(type_, name):
Expand Down

0 comments on commit 83079b7

Please sign in to comment.