Everything is async until proven otherwise

In May I wrote about continuation passing style (CPS) in YAFL. I said the next post would explain how async works in practice. This is that post.

The implementation is no longer textbook CPS. It is closer to C#’s Task and ValueTask. A callback still resumes a suspended function, but a call now returns either its result or a task, instead of passing a continuation everywhere.

Why async

YAFL is a functional language aiming for implicit parallelism. You write ordinary linear code, and the compiler and runtime work out what can run at the same time. This needs a fixed number of worker threads, one per core, and IO that never blocks one of them.

The usual answer is async/await. In C# it spreads through the call graph: a function that calls an async function must be async, and so must its callers, until most of the code carries the keyword.

Async by default

YAFL starts from the opposite assumption. Every function is assumed to be able to suspend, unless the compiler can prove it cannot.

There are no async or await keywords in YAFL source. A pass called sync inference works out which functions can never suspend. It uses an optimistic flow analysis over functions and over the places function values are stored. A function proven never to suspend, and not recursive, keeps the ordinary C stack calling convention and returns its result directly. Leaf functions usually qualify.

All other functions are lowered by the async pass.

A function returns a value or a task

A function that may suspend is compiled into two C functions:

  1. The hot path. It keeps the function’s name and runs on the C stack.
  2. A state machine, named foo$async. It runs when the function resumes after suspending.

A function that may suspend returns either its real result or a tagged pointer to a task that will produce the result later. After every call that may suspend, the hot path checks which one it got. The check is marked as unlikely.

// Simplified sketch, not literal compiler output.
result_or_task foo(int x) {
result_or_task r = bar(x);
if (UNLIKELY(IS_TASK(r)))
goto suspend; // bar() has not finished
return use(r); // common case: plain C, no allocation
suspend:
state = new_state(); // heap frame holding the live locals
save(state, x);
task = new_task();
run_when_complete(r, foo$async, state);
return TAG_TASK(task); // tell my caller this function is suspended too
}
void foo$async(state, completed_task) {
switch (state->idx) { // which call was this waiting on?
case 0: /* reload locals, carry on */ ...
}
}

When no call suspends, a call costs one predictable branch. There is no allocation and no heap frame, so the garbage collector has nothing extra to scan. When a call does suspend, the live locals are saved into a heap state object, the task is registered with a callback, and control returns to the worker’s event loop.

The cost therefore scales with the number of suspensions taken, not the number of operations executed. A tight pipeline loop that never waits runs at hot-path speed.

Measured effect of sync inference

Originally every call paid the full async protocol, whether or not the callee could suspend. Inlining hid some of that cost. Calls to functions proven synchronous are now plain C calls. Measured on the -O3 build of the compiler compiling itself:

  • Generated code size (.text) went from 12.6 MB to 6.2 MB.
  • Self-compile wall time fell by about 13%.
  • Peak memory was unchanged.

The inlining thresholds were not changed.

Two optimisations that did not help

Folding single-caller functions into their callers. This merged suspending helpers into their callers and produced state frames of 137 fields. Every field that is live across a suspension is heap traffic and something the GC has to scan. On the compiler’s own build it gave no speedup at the thresholds tried, and it added about 5.5 MB of generated C. I removed it. Inlining async code needs a limit on the merged frame size, not only a size heuristic.

Removing write barriers. I added a pass that proves when a store needs no GC write barrier. It removed 14,828 of 25,217 barrier sites, a 58.8% reduction. A controlled A/B test showed no CPU improvement: 384.9s against 398.8s, with a 22s spread within the baseline runs. The barrier is a predictable branch on a flag that is usually false, so the number of barriers in the binary does not determine the cost. The 58.8% is a reduction in code sites, not a speedup.

Next

This is groundwork for implicit parallelism. Every call now has a cheap, uniform way to suspend. The next step is for the compiler to decide when to run things in parallel. Any tuple construction, and so any function call’s arguments, is a candidate for parallel work, weighed by a cost estimate. That work is in progress.

The code is at github.com/supersoftcafe/yafl. The design write-up is in docs/architecture.md.