From 9e7da09fe3ff43b704bc58c3509316f22a08627b Mon Sep 17 00:00:00 2001
From: Robert Izzard <r.izzard@surrey.ac.uk>
Date: Sat, 9 Oct 2021 17:17:13 +0100
Subject: [PATCH] memoize (with functools' lru_cache) calls to
 spacing_functions

---
 binarycpython/utils/spacing_functions.py | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/binarycpython/utils/spacing_functions.py b/binarycpython/utils/spacing_functions.py
index bfd5b805f..3eae711f1 100644
--- a/binarycpython/utils/spacing_functions.py
+++ b/binarycpython/utils/spacing_functions.py
@@ -14,7 +14,7 @@ import sys
 from binarycpython.utils.grid import Population
 
 
-
+@functools.lru_cache(maxsize=128)
 def const(
     min_bound: Union[int, float], max_bound: Union[int, float], steps: int
 ) -> list:
@@ -34,6 +34,7 @@ def const(
 
 
 ############################################################
+@functools.lru_cache(maxsize=128)
 def peak_normalized_gaussian_func(
     x: Union[int, float], mean: Union[int, float], sigma: Union[int, float]
 ) -> Union[int, float]:
@@ -58,6 +59,7 @@ def peak_normalized_gaussian_func(
 
 
 ############################################################
+@functools.lru_cache(maxsize=128)
 def gaussian_zoom(
     min_bound: Union[int, float],
     max_bound: Union[int, float],
@@ -102,6 +104,7 @@ def gaussian_zoom(
 
     return array
 
+@functools.lru_cache(maxsize=128)
 def const_dt(self,
              dt=1000.0,
              dlogt=0.1,
@@ -111,8 +114,8 @@ def const_dt(self,
              logspacing=False,
              tmin=3.0, # start at 3Myr
              tmax=None, # use max_evolution_time by default
-             mindm=None, # list of tuples
-             maxdm=[(0.07,1.0,0.1),(1.0,300.0,1.0)], # list of tuples
+             mindm=None, # tuple of tuples
+             maxdm=((0.07,1.0,0.1),(1.0,300.0,1.0)), # tuple of tuples
              fsample=1.0,
              factor=1.0,
              showtable=False):
@@ -128,7 +131,7 @@ def const_dt(self,
         logspacing: whether to use log-spaced time, in which case dt is actually d(log10(t))
         tmin: the minimum time to consider (Myr, default 3.0 Myr)
         tmax: the maximum time to consider (Myr, default None which means we use the grid option 'max_evolution_time')
-        mindm: a list of tuples containing a mass range and minimum mass spacing in that range. The default is [(0.07,1.0,0.1),(1.0,300.0,1.0)] allocated a minimum dm of 0.1Msun in the mass range 0.07 to 1.0 Msun and 1.0Msun in the range 1.0 to 300.0 Msun. Anything you set overrides this.
+        mindm: a tuple of tuples containing a mass range and minimum mass spacing in that range. The default is ((0.07,1.0,0.1),(1.0,300.0,1.0)) allocated a minimum dm of 0.1Msun in the mass range 0.07 to 1.0 Msun and 1.0Msun in the range 1.0 to 300.0 Msun. Anything you set overrides this.
         maxdm: a list of tuples similar to mindm but specifying a maximum mass spacing. (None)
         fsample: a global sampling (Shannon-like) factor (<1) to improve resolution (default 1.0, set to smaller to improve resolution)
         factor: all masses generated are multiplied by this after generation
@@ -145,7 +148,7 @@ def const_dt(self,
 
     # logarithmic spacing in time, generally suitable for Galactic
     # chemical evolution yield grids.
-    spacingfunc="const_dt(self,dlogt=0.1,nres=100,mmin=0.07,mmax=80.0,maxdm=[(0.07,1.0,0.1),(1.0,10.0,1.0),(10.0,80.0,2.0)],showtable=True,logspacing=True,fsample=1.0/4.0)"
+    spacingfunc="const_dt(self,dlogt=0.1,nres=100,mmin=0.07,mmax=80.0,maxdm=((0.07,1.0,0.1),(1.0,10.0,1.0),(10.0,80.0,2.0)),showtable=True,logspacing=True,fsample=1.0/4.0)"
 
     """
 
-- 
GitLab