Skip to content

fix(declare): render bare-listing function views lazily (Refs #3230) - #3982

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3230-lazy-function-views
Nov 20, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3230-lazy-function-views

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3230 — UNVERIFIED-BY-CAPTAIN

Root cause (bisect-adjudicated)

#3230's "post-#3177 pathological slowdown" is not #3177. Automatic bisect (good 9ef7380e = #3177 itself, bad 81a5bbf7, oracle = bats per-test wall time) lands on 35058553 (PR #3530 for #3487, bare declare/set function-definition printing). That commit threaded a pre-rendered &[FunctionSetView] through the set/declare funnel; Executor::function_set_views() runs full ast_print rendering of every function definition, and five call sites (execute_set + four declare/local routes) materialized it eagerly on every dispatch — though only the bare-listing arms consume it. bats loads ~150 library functions and calls local constantly, so every local paid O(total definition text).

Measured on X299 (release): bats per-test 35/37 ms → 221/216 ms (~6.2×); 9-test file wall 17 s → 43 s; scaling probe (150 functions × 12-line bodies, 2000 local calls) 0 s → 33 s.

Fix

Match GNU's cost shape (only the print-all arm renders definitions; builtins/set.def, variables.c print_func_list): the funnel now takes a lazy FunctionSetViewProvider (&dyn Fn() -> Vec<FunctionSetView>), materialized only inside the listing arms; rendering moved to Executor::render_function_set_views(&functions, &function_def_infos). 7 source files +71/−25, plus new pin file.

What this PR deletes

The eager materialization at all five dispatch sites; no fallback or dual path remains.

Evidence (lane, X299 release)

  • All three measurements back to pre-regression values: 37/37 ms per-test; 17 s wall, 9/9 pass; scaling probe 1 s.
  • 558-line listcmp battery (bare declare/set/typeset, -F/-f/-p/-a, POSIX mode, in-function bare local, declare --) byte-identical master vs fixed except 5 self-referential BASH=<exe path> lines.
  • [bug] bare declare omits the function definitions GNU prints #3487 pins 9/9; set/declare lib tests 16/16; rustfmt clean.
  • New pin tests/issue3230_declare_family_lazy_views_regressions.rs: scaling sentinel per issue1119 convention (local-loop cost must not grow with function-table size; 20-vs-160 ratio bound 3×, 120 s absolute backstop). Its failure on the regression code is derived from the measurements above rather than separately re-run on a master build — disclosed here for the captain's verification.

PR #3530 (for #3487) threaded pre-rendered FunctionSetViews through
the set/declare funnels, and the executor materialized them at every
dispatch: Executor::function_set_views() rendered every function's
listing text via ast_print on each set/declare/typeset/local call,
although only the bare-listing arm ever prints them. In a
function-heavy shell (bats loads ~150 library functions and calls
`local` pervasively) that is an O(total definition text) tax per
call: on X299 (release) 2,000 local-carrying calls under 150
twelve-line functions took ~33 s, and bats' junit gather reported
221 ms/test against ~35 ms before the regression (bisect over
9ef7380..81a5bbf: first bad 3505855, the #3530 commit itself;
#3177 is clean).

GNU renders function definitions only inside
print_all_shell_variables' print_func_list arm
(builtins/set.def:506-527; variables.c:1079-1090), reached solely
from the no-operand listing. Mirror that cost shape: the funnels now
take a lazy FunctionSetViewProvider and realize the views only in
the listing arm (set with no args; declare's plain arm). The render
itself (Executor::render_function_set_views over the functions and
function_def_infos maps) is unchanged.

What this change deletes: the eager per-dispatch full-table render
at five executor call sites (execute_set and the four declare/local
routes in declare_local.rs).

Verification (X299, release build of this branch):
- probe (150 functions x 12 lines, local-loop x2000): 33 s -> 1 s
- bats junit suite, direct form: 221/216 ms per test -> 37/37 ms
  (pre-#3177: 34/36 ms; #3177 itself: 35/37 ms)
- bats junit outer form: 43 s -> 17 s wall, 9/9 ok (#3177: 17 s)
- listing outputs (bare declare/set/typeset, -F/-f/-p/-a forms,
  POSIX mode, bare local, declare --) byte-identical to master
  except the self-referential BASH=<exe path> lines
- new pin tests/issue3230_declare_family_lazy_views_regressions.rs
  (scaling-ratio sentinel + listing-arm checks): 2/2 green;
  issue3487 pin 9/9 green; builtins::set/declare lib tests 16/16

UNVERIFIED-BY-CAPTAIN
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 20, 2026 10:35
@caomengxuan666
caomengxuan666 merged commit 4dea9e8 into master Nov 20, 2026
10 of 12 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/3230-lazy-function-views branch November 20, 2026 10:35
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