Skip to content

· Mehdi Berel · Internals · 3 min read

Engineering notes: green threads, a real allocator, and integers that mean what they say

Three boring-sounding changes that took real work: tasks that switch stacks on both native backends, an allocator that actually frees things, and integer arithmetic checked at the declared width on every backend.

Some releases add a headline feature. Session 36 was the other kind: the release where a pile of “works, mostly” turned into “works, provably”. Three of them are worth writing down because each killed a whole class of future bug reports.

Green threads that actually switch stacks

stdlib/task.fd gives cooperative green threads: many tasks on one OS thread, each with its own stack:

use task;

fn worker(n: i64) -> i64 {
    println(f"worker {n} starts");
    task_yield();
    println(f"worker {n} resumes");
    return n * 10;
}

On the native and C paths this is real stack switching (fibers on Windows, ucontext on Linux), so tens of thousands of tasks are cheap. On the interpreter there is one call stack, so each task runs to its end when started; the semantics stay the same, the concurrency does not pretend to be something it is not.

On top of tasks, async fn is no longer sugar over an effect annotation. An async fn compiles to a state machine and returns Future<T>: it runs until it first waits, and await, task_sleep, task_wait_read, and task_wait_write suspend it. There is still no function-coloring split: calling an async fn just hands you a Future value.

The honest remainder: spawn and spawn scope still run on OS threads. Routing the spawn syntax onto tasks is a planned change, and the syntax and semantics above do not change when it lands.

An allocator that frees, and panics when it should

Two numbers from this session: 544 MB → 6 MB on the dropped-Vec benchmark (an IR pass frees Vec locals that provably never escape), and 2.3 GB → 4 MB peak on a 30-million-allocation string workload (temporary and non-escaping local strings are freed the same way). String reclamation was the last entry in our “known limitations” list, and it is gone.

Just as important: p_free now panics on a double free and on a pointer that is not a live block (on the interpreter and both compiled backends). It used to ignore both. That one check immediately exposed a real leak: shared slots on the C path came from plain malloc, so rc.fd never freed its counters.

One integer meaning

Before this session, lowering carried i64 for every integer operation regardless of the declared type, the root cause of most interpreter-vs-backend disagreements. Now add, sub, mul, and neg are checked at the declared width on all three paths, u64 is genuinely unsigned, division by zero always panics, and @wrapping opts into wraparound deliberately. var i = 0 is an i64; a wider value or a float into a narrower slot needs an explicit as.

The companion rule set also landed: raw pointers, raw allocation, and C calls require an unsafe { } block or an @unsafe fn, and a thread may not write a global without a lock.

None of this is glamorous. All of it is the difference between a compiler you evaluate and a compiler you can run a service on.

  • concurrency
  • memory
  • backends
Share:
MB

Mehdi Berel

Engineering notes from the Fid compiler. Everything measured on the real toolchain: nothing on this blog is aspirational.

Back to Blog

Related Posts

View All Posts »

4 min read

The compiler that emits itself, bit-for-bit

fid emit compiles the entire 104K-line self-hosted compiler straight to a Windows executable (no assembler, no linker, no C compiler) and the output is byte-identical to the emitter that produced it. Here is why that matters and how it got there.

4 min read

Effect handlers, resumed: what landed after Fid 1.0

Fid 1.0 brings a self-hosted compiler and a zero-dependency toolchain. The weeks since closed the last open pillar (resumable effect handlers) and brought structured concurrency, a verified bootstrap fixpoint, and a public roadmap to 2.0.