Skip to content

Export stdlib dynamic-call thunks from stdlib.o instead of rebuilding them in every program - #335

Merged
blacktrs merged 2 commits into
mainfrom
perf/dynf-stdlib-tables
Oct 18, 2026
Merged

blacktrs merged 2 commits into
mainfrom
perf/dynf-stdlib-tables

Conversation

@blacktrs

Copy link
Copy Markdown
Contributor

Why

CI got slow: the AOT suite on linux-amd64 went from 19 min (Sep 23) to 91 min (Oct 16). Per-case compile cost roughly doubled while the case count grew ~1.9x. About 87% of a case's compile time is clang -O2 -c over the whole module, and the IR it is handed kept growing (2.6 MB on v0.11 → 6.5 MB on main for fwrite_park_overwrite).

The growth is dynamic-call-by-name machinery. Every dynamic-name call site ($cb(...), callable) built a table of every nameable function that fits its arity, plus one linkonce_odr thunk per function and arity. With the stdlib ~5x larger, one site expanded to ~970 thunks. The sites that trigger it are mostly in the prelude Async runtime (Scheduler, Fiber::__mcRun, TaskGroup, Semaphore, pcntl_signal_dispatch), so any program touching streams or async paid for it.

What changes

The stdlib build now emits those thunks once and exports them from lib/manticore_stdlib.o:

  • external symbols __mc_sdynf<bit>_<fn>: argc 0..8, plus a bare spread over an untyped or mixed[] pack, built from the same dynfThunk / dynfSpreadThunk code as before;
  • the buildable ones are listed in a new additive .sig key, "dynf".

A program still builds its own candidate set and table exactly as before. Only a row's target changes: a stdlib import whose shape is exported points at a declared library thunk instead of a module copy. Prelude and user functions, by-ref inline arms, argc above 8 and spreads with a fixed prefix are built in the module as today. A .sig without dynf (old lib, dev-tree fallback, stdlib:false) builds everything in the module, so no link-time surprises.

Name resolution and coercion are untouched.

Measured

19 cases (every 60th of the Sep 23 case list), -O2 --no-analyze, same worktree before/after:

before after
IR total 54.9 MB 41.2 MB
compile wall 52.8 s 34.3 s
clang time on the 8 affected cases 44.6 s 26.5 s

Per affected case: async_select_reactor 6.5 → 4.0 MB, reflection_function 3.9 → 2.5 MB, curl_write_callback 3.6 → 2.2 MB, http_keepalive_outlives_idle_timer 12.4 → 9.4 MB. The 11 cases without thunks are unchanged; hello world is byte-identical. lib/manticore_stdlib.o grows 7.2 → 8.9 MB.

The win is smaller than a full removal because prelude thunks and by-ref inline arms keep large prelude bodies alive (usort / uksort ~195 KB each, array_filter ~112 KB). Moving prelude thunks into the library is not safe as is: their bodies are linkonce_odr and can be specialized per module. That is a separate follow-up.

Verification

Targeted groups on macOS, all green: dyn 101, callable 30, closure 93, spread 34, variadic 20, fiber 14, async 51, curl 11, http 33, reflection 56, array_ 268, func 30, stream 23. tests/aot/xfail.sh: 61 XFAIL, 0 XPASS. bin/build reaches a clean finish (pinned release builds the tree, the new compiler rebuilds stdlib.o, 0 CELLGUARD violations).

New case dynf_stdlib_by_name covers call by name with defaults, case-folded and \-prefixed names, an arity above the export cap, a user spread, a by-ref arm, a user function beside the stdlib fallback, an unknown name, and call_user_func(_array). It also passes on the baseline compiler; it guards behaviour, since this is a size refactor.

Not run: full AOT suite, difftest, fixpoint, Linux gate (arm64 / amd64 / alpine). Linux is the target, so the gate is required before merge.

Found on the way (not fixed here)

@blacktrs
blacktrs merged commit fe5476b into main Oct 18, 2026
6 of 8 checks passed
@blacktrs
blacktrs deleted the perf/dynf-stdlib-tables branch October 18, 2026 10:52
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