From 5abc0bcf40361af176b11e4be4100dfcca7d4390 Mon Sep 17 00:00:00 2001 From: Joost van Zwieten Date: Mon, 4 Mar 2024 16:03:29 +0100 Subject: [PATCH] evaluable.compile: cache const steps --- nutils/evaluable.py | 11 +++++++++++ tests/test_topology.py | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/nutils/evaluable.py b/nutils/evaluable.py index fc3ecfe37..322acb76b 100644 --- a/nutils/evaluable.py +++ b/nutils/evaluable.py @@ -5273,9 +5273,16 @@ def _compile_tuple(funcs): if not graphviz: serialized = tuple(zip((op.evalf for op in ordereddeps[1:]), deptree[1:])) + nconsts = builtins.sum(itertools.takewhile(lambda v: v, map(operator.attrgetter('isconstant'), ordereddeps[1:]))) + cache = None def eval(**evalargs): + nonlocal serialized, cache values = [evalargs] + if nconsts and cache: + assert len(cache) == nconsts + values.extend(cache) + log.debug(f'using {nconsts} cached values') try: values.extend(evalf(*[values[i] for i in indices]) for evalf, indices in serialized) except KeyboardInterrupt: @@ -5284,6 +5291,10 @@ def eval(**evalargs): log.error(funcs._format_stack(values, e)) raise else: + if nconsts and not cache: + log.debug(f'caching {nconsts} values') + cache = values[1:nconsts + 1] + serialized = serialized[nconsts:] return values[-1] else: diff --git a/tests/test_topology.py b/tests/test_topology.py index 4ebcbf59e..69c8dbd72 100644 --- a/tests/test_topology.py +++ b/tests/test_topology.py @@ -985,7 +985,7 @@ def test_detect_linear(self): target = numpy.array([(.2, .3)]) with self.assertLogs('nutils', level='DEBUG') as cm: self.domain.locate(self.geom, target, eps=1e-15, tol=1e-12, arguments=dict(scale=.123)) - self.assertRegex(cm.output[0], 'locate detected linear geometry') + self.assertTrue(any(line.startswith('DEBUG:nutils:locate detected linear geometry') for line in cm.output)) for etype in 'square', 'triangle', 'mixed':