Skip to content

Commit

Permalink
evaluable.compile: cache const steps
Browse files Browse the repository at this point in the history
  • Loading branch information
joostvanzwieten committed Mar 4, 2024
1 parent 839e02b commit 5abc0bc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions nutils/evaluable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down

0 comments on commit 5abc0bc

Please sign in to comment.