Expand description
Dependency analysis and code generation behind #[psync::graph].
This is Phase I step 2 of the psync proposal: a source-level pass that finds the awaits of an
async function which do not depend on each other and runs them concurrently. It sees tokens
only, so it is deliberately conservative, and the Rust compiler still type- and borrow-checks
everything it generates (two branches that both need &mut x fail to compile; they never race).
§Pipeline
For every block of async code, innermost first (rewrite.rs):
- Classify each statement (
classify.rs) as a movable node or a barrier that stays in place. Nodes are bindings, awaits, and ordinary statements (effects, kept in order with each other). Barriers are block values, lock acquisitions, sleeps, and code psync cannot rewrite (exits hidden in macros,yield). - Expose awaits hidden inside larger statements by hoisting them into bindings, in
evaluation order (
hoist.rs). - Graph each run of nodes between barriers (
schedule.rs): an edge where one node reads a binding another defines, where their accesses to a variable or field conflict (access.rs), where their effects on files, services, the network, processes or the environment conflict (effects.rs), before a write that an earlier exit could skip, and between ordinary statements. - Schedule: single-predecessor runs fuse into chains; the chains of each level run concurrently, and their bindings are handed back in program order.
- Loops (
loops.rs): iterations’ awaited prefixes run up toboundahead while the rest of each body runs in order; loop-carried state is split into a first pass (fission); work behind an early-exit guard runs ahead when the guard would not exit.
§What stays the same
Results, bindings and early exits. A ?, return, break or continue inside a node stops
that node’s chain and is performed after the join in program order (exits.rs), so the
function exits from the same place with the same value. Recognized writes (fs::write,
api.delete, s3.put_object, processes) never start before an earlier exit, and operations on
one file or service keep their order (effects.rs). What can still differ is unclassified
awaited work that sequential code would have skipped after an exit: by adding the attribute,
the programmer asserts that such calls tolerate running speculatively; explain lists them and
strict treats them as writes. Ordinary statements never move past each other, and locks,
permits, transactions, sleeps and order-sensitive calls (send, write, recv, …) keep
their order.
§Hints
Where tokens cannot show what the programmer knows:
#[psync::seq]on a statement keeps it in place.#[psync::fresh]on aletmakes each concurrent branch evaluate the initializer again (let pool = &mut context.pool();gives every query its own connection).#[psync::independent]on a loop asserts that iterations do not depend on each other (needed forwhileloops, whose condition psync cannot reason about); on a statement, that its effects commute with those around it.#[psync::speculate]on a statement or loop asserts that its writes may run although sequential code would have exited first (idempotent uploads, cache fills).
Function options: bound = N, explain, off, strict (unclassified awaited calls are
writes), speculate, effects(read(..), write(..), ordered(..)) (extra verbs), crate = path.
#[psync::graph(off)] compiles the function as written, for comparisons; building with the
environment variable PSYNC_GRAPH=off does the same for every annotated function.
Modules§
- access 🔒
- Token-level read/write analysis.
- classify 🔒
- Statement classification: which statements become graph nodes, and what each one does.
- effects 🔒
- Effects on the world outside the function: files, the network, other services, processes and the environment.
- exits 🔒
- Deferring early exits.
- facts 🔒
- Facts from the compiler: what each call in an annotated function really does.
- gates 🔒
- Runtime checks of computed targets in straight-line code: file paths, HTTP URLs, Redis keys.
- hoist 🔒
- Exposing awaits that sit inside larger statements.
- loops 🔒
- Running loop iterations ahead.
- predict
- What psync’s schedule would make of a measured run: a what-if prediction.
- profile 🔒
#[psync::graph(profile)]: compile the function as written, timing every statement that awaits, forpsync-predict(seepsync::profile).- rewrite 🔒
- The rewriting pass.
- schedule 🔒
- Building the dependency graph of a region and generating its concurrent schedule.
Structs§
- Cert
Node - One node as a certificate describes it: the places it may read and write, each a path of segments. Two nodes are independent when neither writes a place the other reads or writes, and places overlap when one is a prefix of the other.
- Declarations 🔒
- Collects
let mutbindings, bindings to&mutexpressions, and#[psync::fresh]bindings anywhere in the function. - Effect
Config - Function-wide settings from
#[psync::graph(..)]. - Loop
Report - One
forloop and whether its iterations were prefetched. - Node
Report - One node of a region.
- Options
- Options from
#[psync::graph(bound = N, explain, off, crate = path)]. - Region
Report - The graph and schedule of one region.
- Report
- What the analysis found, for
explainand external tooling (psync-expand). - Yield
Injector 🔒 yield_every = N: at the start of every loop body in async code, yield to the executor on everyN-th iteration. A cooperative executor cannot interrupt a task that does not await; these yields are preemption points the compiler inserts, so a long loop no longer holds back higher-priority tasks. They change timing only, never results.
Enums§
- Baseline
- Earlier tools’ rules, applied to the same code so their speedups can be measured against psync’s (book: What is new, and what is not).
- Calls
#[psync::graph(calls = ..)]: a function-wide mark for the calls psync cannot classify, and whether the function’s statements commute.- Verb
- What a function or method name says about the operation, for tools outside the macro
(
cargo psync checkclassifies calls into other crates with the same lists).
Functions§
- expand
- Expand
#[psync::graph(attr)] item. - explain
- Human-readable summary of a report (what
explainprints). - fs_
reads - True for file-system functions that only read (
read_to_string,metadata,open, …). - transform_
fn - Rewrite every async block in
function(its body if it isasync, and any async blocks or closures inside), returning what was found. - verb_of
- The verb a name starts with, if psync knows it.