Repository navigation
Description
Summary
Every natively compiled Bend program that does any IO carries about 1 MiB of
zero bytes. A program that only prints hello compiles to 1,089,008
bytes. With a small change to the runtime C emitted by bend2/comp.ts it
is 40,504 bytes, with identical behaviour.
Reproduce
import Base
def main() -> IO(Unit):
IO.print("hello\n")$ bend hello.bend -o hello && stat -c %s hello
1089008
$ nm -S hello | grep io_eff_rows
000000000000fb60 0000000000100000 d io_eff_rows
A program with no IO (def main() -> U32: 42) is 27 KB. The megabyte
appears as soon as an effect is registered.
Cause
bend2/comp.ts:5464 (at c97ebd5) emits
static IoEff io_eff_rows[1 << 16];That is 65,536 × 16 bytes = 1 MiB. Effects are registered into it by
__attribute__((constructor)) functions that store constants
(io_eff(CID_IO_PRINT, io_print_run, 0)). At -O2/-O3, clang evaluates
those constructors at compile time. Because the stored values are known, it
turns the table from zero-initialized .bss into an initialized 1 MiB
object in .data.rel.ro, most of it zeros, written into the file. gcc does
not do this: the process-spawning drill below, built from the same emitted C with
gcc -O2, keeps the table in .bss and is 93,264 bytes.
It costs file size only. The hello binary has just 19 relative relocations, and the zero pages
are never touched, so startup and resident memory are unaffected. But it
applies to essentially every compiled IO program, and it is the first thing
someone sees when they compile hello world and check the size.
Suggested fix
Allocate the table instead of declaring it as a static array. A calloc in
a constructor that runs before the registering ones cannot be evaluated at
compile time, so nothing is baked into the binary. Lookups and the existing
run == NULL checks are unchanged.
-static IoEff io_eff_rows[1 << 16];
+static IoEff* io_eff_rows;
+
+// Allocated, not a static array: clang evaluates the effect-registering
+// constructors at compile time and would otherwise store all 1 MiB of the
+// mostly-empty table in the binary. Priority 101 runs before them.
+static void __attribute__((constructor(101))) io_eff_init(void) {
+ io_eff_rows = calloc(1 << 16, sizeof(IoEff));
+ if (io_eff_rows == NULL) {
+ fprintf(stderr, "host allocation failed\n");
+ exit(1);
+ }
+}Measured on the emitted C, patched by hand and built with
clang-19 -std=c11 -O3 … -lpthread -lm (Bend's own flags):
| program | before | after | output |
|---|---|---|---|
print hello |
1,089,008 B | 40,504 B | identical |
| a process-spawning drill (7 effects) | 1,126,208 B | 77,736 B | identical, exit 0 |
The error branch could use err_fail if it is declared before this point
in the emitted C; I used fprintf/exit so the snippet stands alone.
Sizing the table to the number of registered effects would also work, but
it touches more code.
Environment: Bend 2.0.32, Ubuntu clang 19.1.7, Linux x86-64.
Activity
fix(runtime): keep bend-lint and GPU-less builds working; restore the…