Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

The pipeline

The rest of the book is about what psync does. This part is about how. The code lives in two crates:

CrateRole
psync-macrosthe proc-macro entry points (graph, seq, fresh, independent, speculate, par); graph calls psync_graph::expand
psync-graphthe 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):

  1. Parses the options (bound, explain, off, strict, speculate, effects(..), yield_every, crate; see Attribute options). If off is set, or PSYNC_GRAPH=off, it only strips hint attributes and returns the function.
  2. Builds a Scope of mutability facts for the whole function. These are the mut bindings, the bindings that hold &mut values (including &mut self and &mut T parameters), and the #[psync::fresh] bindings with their initializers.
  3. Runs the Rewriter (rewrite.rs) over the body. It is a syn::visit_mut::VisitMut that handles blocks innermost first. It only rewrites code inside async fns, async blocks and async closures, and it skips nested items.
  4. 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.
  5. Inserts const __PSYNC_GRAPH: Option<&str> = option_env!("PSYNC_FACTS_STAMP"); at the top of the body. It marks the function for cargo psync’s driver, and makes cargo rebuild the function when its compiler facts change. It has no effect at run time.
  6. Strips the hints, and prints the report if explain was 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

ModuleWhat it decides
classify.rsnode or barrier; awaits, exits, effects; labels
access.rswhich variables and field paths a statement reads and writes
hoist.rshow to split a statement so its await becomes its own node
schedule.rsedges, chain fusion, levels, and the generated join
exits.rshow ?/return/break/continue are recorded and replayed
loops.rsloop prefetch, guard hoisting and inversion, fission, nested-loop flattening
rewrite.rsthe 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.

  1. The report shows n1 -> n3 (writes `cache`) for cache.peek(k) on let mut cache, and you know peek only reads.
  2. let n = api.count().await.len(); is one node, but you expected the len() to be split off.
  3. A loop’s prefix was not prefetched, and the report names a write that every iteration performs.
  4. An error came back from the wrong statement when two statements failed together.
  5. You want to see what psync does with a function without building your crate.
Answers
  1. access.rs, which decides that a method on a mut binding writes unless it looks read-only. peek is neither in the pure list nor has a read-only prefix (get, is_, len, …), so it counts as a write. Called through let c = &cache; it does not: a shared reference is not a mut binding. (c.peek(2) is still an ordinary statement, so it keeps its order with other ordinary statements.)
  2. 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.
  3. loops.rs, which takes the prefix and checks what the rest of the body writes.
  4. exits.rs, which records exits and replays them in source order. A wrong order there would break the guarantee that replay_correct in verify/Schedule.lean proves for the model.
  5. psync-expand FILE LINE, which runs psync_graph::expand outside the compiler. The proc macro is a thin wrapper around the same function.