From 154b968608ed765a5cd303badf04369b2dc220d8 Mon Sep 17 00:00:00 2001 From: Gertjan van Zwieten Date: Fri, 15 Mar 2024 22:05:25 +0100 Subject: [PATCH] make _dependencies_sans_invariants non-recursive --- nutils/evaluable.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/nutils/evaluable.py b/nutils/evaluable.py index 3492d09b8..f18c54833 100644 --- a/nutils/evaluable.py +++ b/nutils/evaluable.py @@ -4520,23 +4520,24 @@ def _isunique(array): def _dependencies_sans_invariants(func, arg): invariants = [] dependencies = [] - _populate_dependencies_sans_invariants(func, arg, invariants, dependencies, {arg}) + cache = {arg} + stack = [func] + while stack: + func_ = stack.pop() + if func_ is None: + dependencies.append(stack.pop()) + elif func_ not in cache: + cache.add(func_) + if arg in func_.arguments: + stack.append(func_) + stack.append(None) + stack.extend(func_._Evaluable__args) + else: + invariants.append(func_) assert (dependencies or invariants or [arg])[-1] == func return tuple(invariants), tuple(dependencies) -def _populate_dependencies_sans_invariants(func, arg, invariants, dependencies, cache): - if func in cache: - return - cache.add(func) - if arg in func.arguments: - for child in func._Evaluable__args: - _populate_dependencies_sans_invariants(child, arg, invariants, dependencies, cache) - dependencies.append(func) - else: - invariants.append(func) - - class _Stats: def __init__(self, ncalls: int = 0, time: int = 0) -> None: