From 711cebfcf314d177e6af75d567b35395cc517003 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Mon, 4 Nov 2024 01:35:40 -0600 Subject: [PATCH] scripts: Simplified memoization of stack.py's limit calculation While a decorator does a good job of separating concerns here, it's a bit overkill for a single function. --- scripts/stack.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/scripts/stack.py b/scripts/stack.py index 8a2a41d5..48f01146 100755 --- a/scripts/stack.py +++ b/scripts/stack.py @@ -234,21 +234,14 @@ def collect(ci_paths, *, callgraph_[source] = (s_file, s_function, frame, targets) callgraph = callgraph_ - # memoize via only the first argument - def cache1(f): - def f_(a, *args, **kwargs): - if a in f_.cache: - return f_.cache[a] - r = f(a, *args, **kwargs) - f_.cache[a] = r - return r - f_.cache = {} - return f_ - # find maximum stack size recursively, this requires also detecting cycles # (in case of recursion) - @cache1 def find_limit(source, seen=set()): + if not hasattr(find_limit, 'cache'): + find_limit.cache = {} + if source in find_limit.cache: + return find_limit.cache[source] + if source not in callgraph: return 0 _, _, frame, targets = callgraph[source] @@ -261,6 +254,7 @@ def collect(ci_paths, *, limit_ = find_limit(target, seen | {target}) limit = max(limit, limit_) + find_limit.cache[source] = frame + limit return frame + limit def find_children(targets):