Skip to content

Commit ac243ac

Browse files
committed
README: benchmarks after the CPU, GPU, checker and build work; GC knobs
bendc is ahead on every time measured (runs, builds, checking) and behind on peak memory for allocation-heavy code and for Metal's baseline; the README says so. BEND_GC_MINOR, BEND_GC_SLACK_MB and BEND_GC_FACTOR tune when collections are major and how often they run (read at start: parsing numbers during a collection could wait on a lock a stopped thread holds).
1 parent da399fe commit ac243ac

4 files changed

Lines changed: 65 additions & 28 deletions

File tree

‎README.md‎

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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

‎rt/bendrt.h‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ static GcBlk *gc_partial[2][GC_NCLS];
148148
static _Atomic size_t gc_since; // bytes handed out since the last collection
149149
static size_t gc_limit = (size_t)256 << 20;
150150
static size_t gc_limit_min = (size_t)256 << 20;
151+
// (Read at start: libc's number parsing takes a lock a stopped thread may hold.)
152+
static double gc_minor_k = 2.0, gc_factor = 0.0;
153+
static size_t gc_slack = (size_t)64 << 20;
151154
static size_t gc_live_bytes;
152155
static size_t gc_major_live;
153156
static int gc_minor; // this collection keeps the marks of old objects
@@ -189,6 +192,9 @@ static void gc_init(void) {
189192
const char *s = getenv("BEND_GC_STATS");
190193
gc_stats = s && *s;
191194
gc_all_major = getenv("BEND_GC_MAJOR") != NULL;
195+
if (getenv("BEND_GC_MINOR")) gc_minor_k = atof(getenv("BEND_GC_MINOR"));
196+
if (getenv("BEND_GC_FACTOR")) gc_factor = atof(getenv("BEND_GC_FACTOR"));
197+
if (getenv("BEND_GC_SLACK_MB")) gc_slack = (size_t)atol(getenv("BEND_GC_SLACK_MB")) << 20;
192198
const char *m = getenv("BEND_GC_MIN_MB");
193199
if (m && atol(m) > 0) gc_limit = gc_limit_min = (size_t)atol(m) << 20;
194200
}
@@ -481,7 +487,7 @@ __attribute__((noinline)) static void gc_collect_locked(void) {
481487
// A major collection forgets every mark; a minor one keeps the old
482488
// objects' (the heap is written only while an object is built, and the
483489
// exceptions are reached from roots).
484-
gc_minor = !gc_all_major && gc_major_live > 0 && gc_live_bytes < 2 * gc_major_live + ((size_t)64 << 20);
490+
gc_minor = !gc_all_major && gc_major_live > 0 && gc_live_bytes < (size_t)(gc_minor_k * (double)gc_major_live) + gc_slack;
485491
if (!gc_minor) {
486492
for (uintptr_t bi = 0; bi < gc_top; bi++) {
487493
if (gc_kind[bi] == 1 || gc_kind[bi] == 2) memset(gc_blk(bi)->mark, 0, sizeof(gc_blk(bi)->mark));
@@ -500,7 +506,10 @@ __attribute__((noinline)) static void gc_collect_locked(void) {
500506
atomic_store(&gc_stopping, 0);
501507
while (atomic_load(&gc_inside) > 0) sched_yield();
502508
if (!gc_minor) gc_major_live = gc_live_bytes ? gc_live_bytes : 1;
503-
gc_limit = gc_limit_min;
509+
{
510+
size_t want = (size_t)(gc_factor * (double)gc_live_bytes);
511+
gc_limit = want > gc_limit_min ? want : gc_limit_min;
512+
}
504513
gc_since = 0;
505514
gc_count++;
506515
if (gc_stats) {

‎rt/bendrt_impl.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ GcBlk *gc_partial[2][GC_NCLS];
3939
_Atomic size_t gc_since; // bytes handed out since the last collection
4040
size_t gc_limit = (size_t)256 << 20;
4141
size_t gc_limit_min = (size_t)256 << 20;
42+
double gc_minor_k = 2.0, gc_factor = 0.0;
43+
size_t gc_slack = (size_t)64 << 20;
4244
size_t gc_live_bytes;
4345
size_t gc_major_live;
4446
int gc_minor; // this collection keeps the marks of old objects
@@ -76,6 +78,9 @@ void gc_init(void) {
7678
const char *s = getenv("BEND_GC_STATS");
7779
gc_stats = s && *s;
7880
gc_all_major = getenv("BEND_GC_MAJOR") != NULL;
81+
if (getenv("BEND_GC_MINOR")) gc_minor_k = atof(getenv("BEND_GC_MINOR"));
82+
if (getenv("BEND_GC_FACTOR")) gc_factor = atof(getenv("BEND_GC_FACTOR"));
83+
if (getenv("BEND_GC_SLACK_MB")) gc_slack = (size_t)atol(getenv("BEND_GC_SLACK_MB")) << 20;
7984
const char *m = getenv("BEND_GC_MIN_MB");
8085
if (m && atol(m) > 0) gc_limit = gc_limit_min = (size_t)atol(m) << 20;
8186
}
@@ -279,7 +284,7 @@ __attribute__((noinline)) void gc_collect_locked(void) {
279284
// A major collection forgets every mark; a minor one keeps the old
280285
// objects' (the heap is written only while an object is built, and the
281286
// exceptions are reached from roots).
282-
gc_minor = !gc_all_major && gc_major_live > 0 && gc_live_bytes < 2 * gc_major_live + ((size_t)64 << 20);
287+
gc_minor = !gc_all_major && gc_major_live > 0 && gc_live_bytes < (size_t)(gc_minor_k * (double)gc_major_live) + gc_slack;
283288
if (!gc_minor) {
284289
for (uintptr_t bi = 0; bi < gc_top; bi++) {
285290
if (gc_kind[bi] == 1 || gc_kind[bi] == 2) memset(gc_blk(bi)->mark, 0, sizeof(gc_blk(bi)->mark));
@@ -298,7 +303,10 @@ __attribute__((noinline)) void gc_collect_locked(void) {
298303
atomic_store(&gc_stopping, 0);
299304
while (atomic_load(&gc_inside) > 0) sched_yield();
300305
if (!gc_minor) gc_major_live = gc_live_bytes ? gc_live_bytes : 1;
301-
gc_limit = gc_limit_min;
306+
{
307+
size_t want = (size_t)(gc_factor * (double)gc_live_bytes);
308+
gc_limit = want > gc_limit_min ? want : gc_limit_min;
309+
}
302310
gc_since = 0;
303311
gc_count++;
304312
if (gc_stats) {

‎rt/bendrt_split.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ extern GcBlk *gc_partial[2][GC_NCLS];
145145
extern _Atomic size_t gc_since;
146146
extern size_t gc_limit;
147147
extern size_t gc_limit_min;
148+
// (Read at start: libc's number parsing takes a lock a stopped thread may hold.)
149+
extern double gc_minor_k;
150+
extern size_t gc_slack;
148151
extern size_t gc_live_bytes;
149152
extern size_t gc_major_live;
150153
extern int gc_minor;

0 commit comments

Comments
 (0)