Skip to main content

Crate psync_graph

Crate psync_graph 

Source
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):

  1. 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).
  2. Expose awaits hidden inside larger statements by hoisting them into bindings, in evaluation order (hoist.rs).
  3. 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.
  4. Schedule: single-predecessor runs fuse into chains; the chains of each level run concurrently, and their bindings are handed back in program order.
  5. Loops (loops.rs): iterations’ awaited prefixes run up to bound ahead 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 a let makes 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 for while loops, 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, for psync-predict (see psync::profile).
rewrite 🔒
The rewriting pass.
schedule 🔒
Building the dependency graph of a region and generating its concurrent schedule.

Structs§

CertNode
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 mut bindings, bindings to &mut expressions, and #[psync::fresh] bindings anywhere in the function.
EffectConfig
Function-wide settings from #[psync::graph(..)].
LoopReport
One for loop and whether its iterations were prefetched.
NodeReport
One node of a region.
Options
Options from #[psync::graph(bound = N, explain, off, crate = path)].
RegionReport
The graph and schedule of one region.
Report
What the analysis found, for explain and external tooling (psync-expand).
YieldInjector 🔒
yield_every = N: at the start of every loop body in async code, yield to the executor on every N-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 check classifies calls into other crates with the same lists).

Functions§

expand
Expand #[psync::graph(attr)] item.
explain
Human-readable summary of a report (what explain prints).
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 is async, and any async blocks or closures inside), returning what was found.
verb_of
The verb a name starts with, if psync knows it.