Basic

Await compiles to a state machine

A function that can pause in the middle cannot use the call stack. The compiler replaces the stack with a struct that says which line we are on, and reading that struct is how async is debugged.

What you will see

One line. A task over the limit is a function that should be split.

About 1 min of reading

Try It

Count states and estimate size

1

Count the await points

printf 'await\nawait\nawait\nawait\n' > /tmp/s.txt; wc -l < /tmp/s.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
4. Four awaits is at least five states, and the states are the size.
2

Estimate the task size

awk 'BEGIN {printf "%d\n", 5*16 + 64}'

Before you run it, say out loud what it will print. Then open the answer.

What it prints
144. A rough size per task, which is the number that decides the pool.
3

Write the size limit

printf 'max_task_bytes 256\n' > /tmp/lim.txt; cat /tmp/lim.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One line. A task over the limit is a function that should be split.

Data Flow

An async function to a machine
1 The function is parsed for awaits
↓
2 Each await becomes a state
↓
3 Locals used later are stored in the machine
↓
4 The machine is allocated when spawned
Why this works
Why a task has a size

The task is allocated on a heap when it is spawned. A large machine means a large allocation per task, and the count is large.

The debuggable form

The state number is the debuggable form. A stack trace of an async function is a state in a machine, not a line.

Examples

The stack becomes a field

Each await point is a state. The state is stored in a value that can be moved to a heap.

Locals live in the machine

A local that is used after an await is stored in the machine. That is why the borrow checker talks about the whole function.

The machine is why size matters

A function with many awaits has a large machine. The size of the machine is the size of the task.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← A future is a recipe, not a result
Previous Lesson
A future is a recipe, not a result
Next Lesson
The executor is the loop
The executor is the loop →