Repository navigation
fix(interp): make deep function recursion linear in its depth - #883
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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,PWDand_many times, so at depth 100 there were about 56k layer visits forIFSalone.internal/shell/interp/vars.gonow:Setof a new key, ordeleteCurrentScopeVar). Value updates such asn=$((n+1))leave it alone. Caches only point at ancestors, which never change, and forks reset them. Each layer holds at most 64 names.currentTempScopeBindingskips plain function scopes, which hold no temp bindings.enableTempScope(). It replacesoverlay.tempScope = trueand clears that layer's caches. No layer sits above it yet.Get,visibleBindingWriteEnvandvisibleSecondsBindinguse the cached lookup. Behavior is unchanged.Numbers
n=0; f(){ n=$((n+1)); [ $n -lt N ] && f; }; f, measured locally:-raceQuadrupling 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-raceand about 5.5x without.TestFunctionScopeLookupsSeeBindingChangescovers bindings that appear and disappear in scopes between the reader and the owner. Its expected output is from pinned bash 5.3. It covers dynamicunsetof 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/bashpasses.go test -race ./...passes, apart fromscripts/bench-compare(fails the same way onmain; it runs the host's bash) and thecmd/gbashtail-follow tests (flaked under load and pass on rerun).make lintis clean.No SPEC change: this is internal to the interpreter.
Not covered (follow-up)
local/declarestill snapshot the whole environment on each call (declEvalEnvinrunner_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 alocalin 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.