Skip to content
Snippets Groups Projects
Commit 5d9a7dd6 authored by Dominic Kempf's avatar Dominic Kempf
Browse files

More adjustments to the decorator magic

parent 720966e9
No related branches found
No related tags found
No related merge requests found
......@@ -26,12 +26,12 @@ class UFL2LoopyDataCache(dict):
def __init__(self):
self.counter = 0
def register(self, cachekey, content, preamble):
self[cachekey] = (content, self.counter, preamble)
def register(self, cachekey, content):
self[cachekey] = (content, self.counter)
self.counter = self.counter + 1
def extract_preambles(self):
return [(p[1], p[0]) for p in self.values() if p[2]]
return [(p[1], p[0]) for p in self.values() if p[0].generate_preambel]
# have one such cache on the module level. It is easier than handing around an instance of it.
......@@ -72,21 +72,23 @@ class _RegisteredFunction(object):
""" The data structure for a function that accesses UFL2LoopyDataCache """
def __init__(self, func,
cache_key_generator=lambda *a : freeze(a),
generate_preamble=True
generate_preamble=False,
return_content=False
):
self.func = func
self.cache_key_generator = cache_key_generator
self.generate_preamble = generate_preamble
self.return_content = return_content
def __call__(self, *args):
cache_key = (self.func, self.cache_key_generator(*args))
cache_key = (self, self.cache_key_generator(*args))
if cache_key in _cache:
if not self.generate_preamble:
if self.return_content:
return _cache[cache_key][0]
else:
content = self.func(*args)
_cache.register(cache_key, content, self.generate_preamble)
if not self.generate_preamble:
_cache.register(cache_key, content)
if self.return_content:
return content
......@@ -112,6 +114,9 @@ def _dune_decorator_factory(**factory_kwargs):
This is usually not the case, when you generate something
like symbol names. Those are inserted into other snippets,
but not on their own right.
return_content : bool
Whether the content of the cache should be returned on subsequent function calls.
If the cache is only used for requirements tracking, this is not needed.
"""
def _dec(*args, **kwargs):
# Modify the kwargs according to the factorys kwargs
......@@ -127,7 +132,7 @@ def _dune_decorator_factory(**factory_kwargs):
# A decorator for a dune preambel
dune_preambel = _dune_decorator_factory()
dune_preambel = _dune_decorator_factory(generate_preamble=True)
# A decorator for a dune symbol name
dune_symbol = _dune_decorator_factory(generate_preamble=False)
dune_symbol = _dune_decorator_factory(return_content=True)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment