On this page

0283. Shared runtime state for split C units

Context

Research #7 measured a dashboard gcc -O2 build at 7.46 s with one generated C unit and 2.48 s with eight. Design Critic #15 asked to split units only for optimized builds and caching, not the TinyCC dev loop. A survey of the tree found dozens of mutable file-scope static variables in runtime/ and plugins/*/native/ headers (stdout buffering, fuel, arenas, cli argv, agent handles, and similar). If each compiled unit includes those headers, it gets a private copy, so output, traps, fuel and plugin state diverge. The splitter is unsound until that state has exactly one definition. Maintainer approved the frozen-file edit this record lists.

Decision

  1. Visibility. Mutable file-scope state in runtime and plugin native headers is declared extern and defined in exactly one translation unit. runtime/trt_state.h provides TRT_STATE / TRT_STATE_INIT. The unit that should hold the definitions compiles without TRT_NO_STATE_DEFS; tenon_rt.h then sets TRT_DEFINE_STATE and the headers emit definitions. Other units (once they exist) compile with -DTRT_NO_STATE_DEFS and see only declarations. Immutable static const tables may stay static.
  2. Not in this slice. split_units, TENON_C_UNITS, parallel compile and per-unit caching stay later M20.5 work. They apply only to BuildMode::Release with CcSpeed::Normal (never the TinyCC dev loop).
  3. Frozen files this record authorizes, file-scope mutable state only: plugins/cli/native/trt_cli.h, trt_net.h, trt_io.h; plugins/agent/native/trt_agent.h, trt_ws_api.h, and the probe-serial static in trt_ws.h. Function-local static in those inline headers is lifted to file scope so it also has one definition. Runtime headers and non-frozen plugin headers follow the same policy in a later non-frozen slice; they are not blocked on further owner approval.

Alternatives considered

Consequences

Single-TU programs keep the same observable behavior: one definition of each object still lives in the generated main.c. Split optimized builds become possible after the remaining headers and the splitter land. A later unit that forgets -DTRT_NO_STATE_DEFS will fail at link time with duplicate symbols, which is the desired failure.