Expand description
Running loop iterations ahead.
Prefetch. When the awaited statements at the front of a for loop body (its prefix) only
touch per-iteration data, psync starts the prefixes of up to bound later iterations while the
rest of the body still runs in order:
for x in xs { let y = fetch(x).await?; out.push(y); }
// becomes
let each = over(&xs, async |x| { <prefix, exits recorded> });
let items = join_all_bounded(xs.into_iter().map(|x| each(x)).collect::<Vec<_>>(), bound).await;
for (exits.., bindings) in items { <re-raise exits>; let (x, y) = bindings; out.push(y); }Guards. if c { .. } around awaited work becomes a prefix binding
let g = if c { Some(<work>) } else { None };, and an early exit if c { ..; continue; }
followed by awaited work becomes let g = if c { None } else { Some(<work>) }; (with c
pure), so the work runs ahead exactly for the items that reach it.
Fission. When the body starts with statements that update loop-carried state without
awaiting (dedupe sets, map removals, permit reservations), those run first for every item in a
plain loop, and the remaining body is prefetched. Exits in that first loop that would end the
whole loop (return, ?, break to an outer loop) are deferred until the items before them
have been processed, which is where sequential code performs them.
Structsยง
- Loop
Scope ๐ - What a loopโs prefix may touch.
Constantsยง
- TOTAL_
METHODS ๐ - Methods whose call on a total receiver neither panics nor touches anything: what a file path is usually built with.
Functionsยง
- binding_
counts ๐ - Every name a pattern binds, with repetitions (
visitsees each binding once). - body_
effects ๐ - The effects of a run of statements.
- breaks ๐
- True when
bodycontains abreakthat would leave it (not one inside a nested loop). - describe ๐
- exit_
guard ๐ - The condition of
if c { ..; continue | break | return .. }(noelse) whencis pure. - flatten ๐
for a in xs { for b in f(a) { body } }asfor (a, b) in <pairs> { body }, when that is the same loop: the outer body is only the inner loop, neither loop is labeled,bodyhas nobreak(which would end only the inner loop), and the outer item is a plain binding (it is paired with every inner item, so it must beCopy, such as a reference; otherwise the generated code does not compile rather than changing meaning).- is_
local ๐ - substitute ๐
- Replace the names bound by
letsintokenswith their values, in parentheses. - target_
check ๐ - The runtime check that the items of a prefetched loop name different files.
- terminates ๐
- True when
bodymay end the loop it belongs to: a?orreturnanywhere outside closures and async blocks, abreakthat leaves it, or acontinueaimed at an outer loop. - total ๐
- An expression that can be evaluated once more, ahead of the loop, for every item: it has no effect and cannot panic (no indexing, no arithmetic, no unknown call).
- yields_
value ๐ - Whether a branch ends in a value (
if c { f().await } else { x }), which cannot be moved into a hoisted chain. Loops andifs withoutelseat the end are statements of type().