The pipeline
The rest of the book is about what psync does. This part is about how. The code lives in two crates:
| Crate | Role |
|---|---|
psync-macros | the proc-macro entry points (graph, seq, fresh, independent); graph calls psync_graph::expand |
psync-graph | the analysis and code generation, an ordinary library (also used by psync-expand) |
psync (src/graph_rt.rs, exported as psync::__private) | the small runtime the generated code calls |
The proc macro is a thin wrapper, so the whole analysis can be tested and run outside the compiler.
psync-expand does exactly that.
From attribute to code
psync_graph::expand(attr, item):
- Parses the options (
bound,explain,off,crate). Ifoffis set, orPSYNC_GRAPH=off, it only strips hint attributes and returns the function. - Builds a
Scopeof mutability facts for the whole function. These are themutbindings, the bindings that hold&mutvalues (including&mut selfand&mut Tparameters), and the#[psync::fresh]bindings with their initializers. - Runs the
Rewriter(rewrite.rs) over the body. It is asyn::visit_mut::VisitMutthat handles blocks innermost first. It only rewrites code insideasync fns, async blocks and async closures, and it skips nested items. - Strips the hints, and prints the report if
explainwas given.
For each block of async code, the rewriter does five steps:
statements
│ for/while loops ──► loops.rs: prefetch, guards, fission, flattening
▼
hoist_tail an awaited block value becomes `let t = ..; t`
▼
expose_awaits awaits in scrutinees, arguments, receivers and if-branches become bindings
▼
schedule_block classify each statement; split into regions at barriers
▼
schedule_region graph → chains → levels → generated joins
A block whose regions all stay sequential is emitted exactly as written. Hoisting is undone too. So psync never changes code it cannot speed up.
The modules
| Module | What it decides |
|---|---|
classify.rs | node or barrier; awaits, exits, effects; labels |
access.rs | which variables and field paths a statement reads and writes |
hoist.rs | how to split a statement so its await becomes its own node |
schedule.rs | edges, chain fusion, levels, and the generated join |
exits.rs | how ?/return/break/continue are recorded and replayed |
loops.rs | loop prefetch, guard hoisting and inversion, fission, nested-loop flattening |
rewrite.rs | the walk that ties it together |
The next chapters take them one at a time.