Skip to content

fix(interp): make deep function recursion linear in its depth - #883

Merged
ewhauser merged 3 commits into
mainfrom
claude/elastic-wilbur-20402e
Oct 20, 2026
Merged

ewhauser merged 3 commits into
mainfrom
claude/elastic-wilbur-20402e

Conversation

@ewhauser

Copy link
Copy Markdown
Owner

What changed

Recursing through shell functions cost O(depth²). Every function call adds an environ layer, and every variable lookup walked the chain one layer at a time. The same went for writes passing through function scopes and for the OPTIND/SECONDS/temp-binding searches. A command in a recursive function reads globals such as IFS, GLOBIGNORE, PWD and _ many times, so at depth 100 there were about 56k layer visits for IFS alone.

internal/shell/interp/vars.go now:

  • Caches lookups per layer. Each layer remembers, per name, which layer further down binds it (or the non-overlay environ the chain continues into). An entry stays valid while the shared epoch's per-name generation is unchanged. The generation bumps only when a layer gains or loses a binding of that name (a Set of a new key, or deleteCurrentScopeVar). Value updates such as n=$((n+1)) leave it alone. Caches only point at ancestors, which never change, and forks reset them. Each layer holds at most 64 names.
  • Jumps writes to their target. A write that passes through function or temp scopes goes straight to the nearer of the layer that binds the name and the first layer that does not pass writes on (cached per layer).
  • Skips function scopes in temp-binding searches. currentTempScopeBinding skips plain function scopes, which hold no temp bindings.
  • Clears caches on enableTempScope(). It replaces overlay.tempScope = true and clears that layer's caches. No layer sits above it yet.

Get, visibleBindingWriteEnv and visibleSecondsBinding use the cached lookup. Behavior is unchanged.

Numbers

n=0; f(){ n=$((n+1)); [ $n -lt N ] && f; }; f, measured locally:

depth before after
250 72 ms ~15 ms
1000 913 ms ~70 ms
2000 3.5 s ~160 ms
1000, -race 1.15 s ~0.2 s

Quadrupling the depth now costs about 4–5x, down from about 14x. What remains above 4x is mostly the Go GC scanning the deeper goroutine stack.

Tests

  • TestFunctionDepthDefaultAllowsDeepRecursion (the ratio test from test(runtime): deflake function depth recursion test #881) now covers global writes and prefix temp bindings (y=$n f). It allows at most 10x for 4x the depth, down from 32x. It logs about 4.5x with -race and about 5.5x without.
  • New TestFunctionScopeLookupsSeeBindingChanges covers bindings that appear and disappear in scopes between the reader and the owner. Its expected output is from pinned bash 5.3. It covers dynamic unset of a caller's local, subshells forked past the 32-layer collapse, prefix temp bindings, declare -g, and locals declared after a recursive call.
  • make conformance-test CONFORMANCE_RUN=TestConformance/bash passes. go test -race ./... passes, apart from scripts/bench-compare (fails the same way on main; it runs the host's bash) and the cmd/gbash tail-follow tests (flaked under load and pass on rerun). make lint is clean.

No SPEC change: this is internal to the interpreter.

Not covered (follow-up)

local/declare still snapshot the whole environment on each call (declEvalEnv in runner_decl.go). Past 32 layers that fork collapses the chain, which costs time proportional to the bindings in the upper scopes. So recursion that declares a local in every frame is still mildly superlinear: about 5.6x for 4x the depth, or about 7x with -race, down from about 12x before this change. Fixing that needs a lazy or persistent snapshot.

TestFunctionDepthDefaultAllowsDeepRecursion takes under a second
locally but about ten seconds in CI, where the race detector runs it
alongside the rest of the package, so its 10s bound failed twice on
PR #877. 25s still catches the O(depth^2) writes it guards against,
which took over ten seconds even without the race detector.
Every function call adds an environ layer, and variable lookups, writes
that pass through function scopes, and the OPTIND/SECONDS/temp-binding
searches all walked the chain one layer at a time. Each command in a
recursive function reads globals such as IFS and PWD many times, so
recursing to depth d cost O(d^2): depth 1000 took 0.9s, or about 10s
under -race in CI.

Each layer now caches, per name, which layer further down binds it. The
cache stays valid until some layer sharing the epoch gains or loses a
binding of that name, which value updates do not do. Pass-through writes
jump straight to the nearer of that owner and the first layer that does
not pass writes on, and temp-binding lookups skip plain function scopes.

Depth 2000 now takes about 0.16s instead of 3.5s, and quadrupling the
depth costs about 4-5x instead of 14x.
…-20402e

# Conflicts:
#	internal/runtime/nesting_limits_test.go
@ewhauser
ewhauser enabled auto-merge (squash) October 20, 2026 04:38
@ewhauser
ewhauser merged commit 25d4d05 into main Oct 20, 2026
17 checks passed
@ewhauser
ewhauser deleted the claude/elastic-wilbur-20402e branch October 20, 2026 04:45
@ewhauser ewhauser mentioned this pull request Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant