Commit 79188fa0 authored by Médéric Boquien's avatar Médéric Boquien

Make the cache faster by avoiding a serialisation with marshel. Rather let's...

Make the cache faster by avoiding a serialisation with marshel. Rather let's use tuples of of the values of the parameters. This works because they are stored in ordered dictionaries.
parent f2537758
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
### Optimised ### Optimised
- The estimation of the physical properties is made a bit faster when all the models are valid. (Médéric Boquien) - The estimation of the physical properties is made a bit faster when all the models are valid. (Médéric Boquien)
- The access to the SED and module caches has been made faster and simpler. This results in a speedup of ~6% in the computation of the models. (Médéric Boquien)
## 0.12.1 (2018-02-27) ## 0.12.1 (2018-02-27)
### Fixed ### Fixed
......
...@@ -3,8 +3,6 @@ ...@@ -3,8 +3,6 @@
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt # Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
# Author: Yannick Roehlly # Author: Yannick Roehlly
import marshal
from ..sed import SED from ..sed import SED
from .. import sed_modules from .. import sed_modules
...@@ -62,9 +60,10 @@ class SedWarehouse(object): ...@@ -62,9 +60,10 @@ class SedWarehouse(object):
if name in self.nocache: if name in self.nocache:
module = sed_modules.get_module(name, **kwargs) module = sed_modules.get_module(name, **kwargs)
else: else:
# Marshal a tuple (name, parameters) to be used as a key for # Use the name of the module and the values of the parameters as a
# storing the module in the cache. # key to the cache. This works because the parameters are stored in
module_key = marshal.dumps((name, kwargs)) # ordered dictionaries.
module_key = (name, tuple(kwargs.values()))
if module_key in self.module_cache: if module_key in self.module_cache:
module = self.module_cache[module_key] module = self.module_cache[module_key]
else: else:
...@@ -92,7 +91,7 @@ class SedWarehouse(object): ...@@ -92,7 +91,7 @@ class SedWarehouse(object):
""" """
if n_modules_max > -1: if n_modules_max > -1:
for k in list(self.sed_cache.keys()): for k in list(self.sed_cache.keys()):
if len(marshal.loads(k)[0]) > n_modules_max: if len(k) > n_modules_max:
del self.sed_cache[k] del self.sed_cache[k]
def get_sed(self, module_list, parameter_list): def get_sed(self, module_list, parameter_list):
...@@ -120,9 +119,10 @@ class SedWarehouse(object): ...@@ -120,9 +119,10 @@ class SedWarehouse(object):
module_list = list(module_list) module_list = list(module_list)
parameter_list = list(parameter_list) parameter_list = list(parameter_list)
# Marshal a tuple (module_list, parameter_list) to be used as a key # Use the values of the parameters of all the modules as the key for
# for storing the SED in the cache. # the cache. This works because the parameters are stored in ordered
key = marshal.dumps((module_list, parameter_list)) # dictionaries.
key = tuple(tuple(par.values()) for par in parameter_list)
sed = self.sed_cache.get(key) sed = self.sed_cache.get(key)
if sed is None: if sed is None:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment