@@ -300,33 +300,50 @@ tail calls become loops. Effects are requests answered by an event loop with the
300300
301301## Benchmarks
302302
303- ` bench/run.sh ` builds each program in [ ` bench/ ` ] ( bench ) with bendc and with the official ` bend `
304- (2.0.25), checks that both print the same thing, and reports the best of three runs. Sizes come from
305- the command line, so neither compiler can compute the answer at compile time. On an Apple M4 Pro
306- (12 cores, 24 GB, macOS 27):
307-
308- | program | what it does | bendc | official bend |
309- | ---| ---| ---| ---|
310- | ` forks 28 ` | a parallel let at every level of a 2^28-leaf tree, CPU threads | 0.38s | 0.10s |
311- | ` forks_gpu 28 ` | the same as a ` ! ` -call, on the GPU | ** 0.07s ** | 0.09s |
312- | ` leaves 14 ` | 16384 leaves of a 200,000-step ` F32 ` loop, CPU threads | ** 0.77s ** | 0.98s |
313- | ` leaves_gpu 14 ` | the same as a ` ! ` -call, on the GPU | 0.08s | 0.07s |
314- | ` sort 1000000 ` | build, merge sort and sum a million ` U32 ` s (one thread, allocation-heavy) | ** 0.36s ** | 0.63s |
303+ ` bench/run.sh ` builds each program in [ ` bench/ ` ] ( bench ) with bendc ( ` bendc -o ` ) and with the official
304+ ` bend ` (2.0.25, ` bend -o ` ), checks that both print the same thing, and reports the best of three runs
305+ with its peak memory. Sizes come from the command line, so neither compiler can compute the answer at
306+ compile time. On an Apple M4 Pro (12 cores, 24 GB, macOS 27):
307+
308+ | program | what it does | bendc | official bend | bendc memory | official memory |
309+ | ---| ---| ---| ---| --- | --- |
310+ | ` forks 28 ` | a parallel let at every level of a 2^28-leaf tree, CPU threads | ** 0.04s ** | 0.10s | 3 MB | 3 MB |
311+ | ` forks_gpu 28 ` | the same as a ` ! ` -call, on the GPU | ** 0.05s ** | 0.09s | 20 MB | 14 MB |
312+ | ` leaves 14 ` | 16384 leaves of a 200,000-step ` F32 ` loop, CPU threads | ** 0.74s ** | 0.98s | 3 MB | 3 MB |
313+ | ` leaves_gpu 14 ` | the same as a ` ! ` -call, on the GPU | ** 0.06s ** | 0.07s | 20 MB | 14 MB |
314+ | ` sort 1000000 ` | build, merge sort and sum a million ` U32 ` s (one thread, allocation-heavy) | ** 0.35s ** | 0.53s | 481 MB | 32 MB |
315315
316316| task | bendc | official bend |
317317| ---| ---| ---|
318- | type-check ` bendc.bend ` (15,000 lines with ` check.bend ` ) | 4.3s | 0.9s |
319- | build ` bendc.bend ` into a binary | 8.1s (1.1s to C, 7.0s in clang) | 70s, 9.9 GB |
320- | build the bootstrap's stage0 (` boot.bend ` ) | n/a | 16s, 3.8 GB |
321-
322- bendc is faster on sequential, allocation-heavy code (its collector and native ` Nat ` /` U32 ` ), on
323- numeric loops on the CPU, and on fork-heavy code on the GPU (at 2^30 leaves: 0.145s against
324- 0.178s). Its fixed GPU cost (starting Metal, loading the cached kernel) is 0.04s, against 0.06s.
325- The official runtime is well ahead on fork-heavy code on the CPU: it turns the levels below its fork
326- frontier into native loops, where bendc still pays for a closure, a deque push and a join per fork.
327- The official checker is about 4x faster than bendc's port of it. The official compiler's build
328- numbers are for a whole compiler; bendc keeps them down by avoiding the patterns it expands (see
329- [ Bootstrapping] ( #bootstrapping ) ).
318+ | build ` forks.bend ` (source to binary) | ** 0.17s** | 0.27s |
319+ | build ` forks_gpu.bend ` | ** 0.27s** | 0.48s |
320+ | build ` leaves.bend ` | ** 0.16s** | 0.27s |
321+ | build ` leaves_gpu.bend ` | ** 0.26s** | 0.48s |
322+ | build ` sort.bend ` | ** 0.18s** | 0.28s |
323+ | type-check ` bendc.bend ` (16,000 lines with ` check.bend ` ) | ** 0.78s** , 501 MB | 0.96s, 996 MB |
324+ | build ` bendc.bend ` into a binary | ** 8.1s** | 70s, 9.9 GB |
325+
326+ Where the time goes:
327+
328+ - ** Forks on the CPU.** A def that is flat but for its parallel lets and calls to itself gets a
329+ sequential clone, ` S_name ` . Each thread tracks its fork depth; past log2(threads) + 6 levels a
330+ parallel let runs its values in order through the clone: no closures, deque pushes or joins.
331+ - ** Forks and loops on the GPU.** See [ The GPU backend] ( #the-gpu-backend ) : seq defs and looping flat
332+ defs run in their own small kernel (` bend_kq ` ), a whole SIMD group at a time.
333+ - ** Builds.** The runtime is compiled once (` build/bendrt.o ` , from ` rt/bendrt_impl.c ` ), so a program
334+ compiles only its own code; bendc's own front end takes about 0.1s for these programs.
335+ - ** Checking.** Declarations are checked in parallel on the CPU threads, against the book as it stands
336+ before each; the allocator hands out partly free blocks by their bitmaps; ` Map.bit ` is native.
337+
338+ Where bendc loses is ** peak memory** in two cases. On the GPU, Metal itself costs about 16 MB before a
339+ program does anything (the official runtime's 14 MB is less than a bare Metal program here). On
340+ allocation-heavy code such as ` sort ` , the official runtime counts references: it frees a list cell
341+ the moment it dies and reuses it in place, so sorting a million-element list stays near one list's
342+ size. bendc's collector is a tracing one: garbage waits for the next collection (every 256 MB of
343+ allocation), and a million-deep recursion (` merge ` builds its result as ` x <> merge(..) ` ) keeps a
344+ deep stack. Collecting more often (` BEND_GC_MIN_MB ` , ` BEND_GC_FACTOR ` , ` BEND_GC_MINOR ` ) trades speed
345+ for memory: 341 MB at 0.45s for ` sort ` . Closing the rest needs reference counting, or destination-
346+ passing for recursions like ` merge ` , which bendc does not do yet.
330347
331348## Writing a compiler under Bend's rules
332349
0 commit comments