From dc2e4516c54b83575b07aad211c53d1c4d55eebd Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner <inform@tiker.net> Date: Fri, 28 Nov 2014 13:38:51 -0600 Subject: [PATCH] Add CachingMapperMixin --- pymbolic/mapper/__init__.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pymbolic/mapper/__init__.py b/pymbolic/mapper/__init__.py index 8970a74..8aa66ca 100644 --- a/pymbolic/mapper/__init__.py +++ b/pymbolic/mapper/__init__.py @@ -721,7 +721,23 @@ class CallbackMapper(RecursiveMapper): # }}} -# {{{ cse caching mixin +# {{{ caching mixins + +class CachingMapperMixin(object): + def __init__(self): + super(CachingMapperMixin, self).__init__() + self.result_cache = {} + + def rec(self, expr): + try: + return self.result_cache[expr] + except KeyError: + result = super(CachingMapperMixin, self).rec(expr) + self.result_cache[expr] = result + return result + + __call__ = rec + class CSECachingMapperMixin(object): """A :term:`mix-in` that helps -- GitLab