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, speculate, par); 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,strict,speculate,effects(..),yield_every,crate; see Attribute options). 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. - With
yield_every = N, inserts a yield to the executor at the start of every Nth iteration of each loop (YieldInjector), so a long loop cannot hold back other tasks. - Inserts
const __PSYNC_GRAPH: Option<&str> = option_env!("PSYNC_FACTS_STAMP");at the top of the body. It marks the function forcargo psync’s driver, and makes cargo rebuild the function when its compiler facts change. It has no effect at run time. - 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 a block it cannot speed up. (The function as a whole still gains the
__PSYNC_GRAPH constant of step 5.)
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.
Exercises
For each question, name the module (or step) where you would look first.
- The report shows
n1 -> n3 (writes `cache`)forcache.peek(k)onlet mut cache, and you knowpeekonly reads. let n = api.count().await.len();is one node, but you expected thelen()to be split off.- A loop’s prefix was not prefetched, and the report names a write that every iteration performs.
- An error came back from the wrong statement when two statements failed together.
- You want to see what psync does with a function without building your crate.
Answers
access.rs, which decides that a method on amutbinding writes unless it looks read-only.peekis neither in the pure list nor has a read-only prefix (get,is_,len, …), so it counts as a write. Called throughlet c = &cache;it does not: a shared reference is not amutbinding. (c.peek(2)is still an ordinary statement, so it keeps its order with other ordinary statements.)hoist.rs. A binding whose value is itself awaited is already its own node, so there is nothing to split:len()runs right after the await in the same chain, and costs nothing.loops.rs, which takes the prefix and checks what the rest of the body writes.exits.rs, which records exits and replays them in source order. A wrong order there would break the guarantee thatreplay_correctinverify/Schedule.leanproves for the model.psync-expand FILE LINE, which runspsync_graph::expandoutside the compiler. The proc macro is a thin wrapper around the same function.