Skip to main content

Module loops

Module loops 

Source
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ยง

LoopScope ๐Ÿ”’
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 (visit sees each binding once).
body_effects ๐Ÿ”’
The effects of a run of statements.
breaks ๐Ÿ”’
True when body contains a break that would leave it (not one inside a nested loop).
describe ๐Ÿ”’
exit_guard ๐Ÿ”’
The condition of if c { ..; continue | break | return .. } (no else) when c is pure.
flatten ๐Ÿ”’
for a in xs { for b in f(a) { body } } as for (a, b) in <pairs> { body }, when that is the same loop: the outer body is only the inner loop, neither loop is labeled, body has no break (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 be Copy, such as a reference; otherwise the generated code does not compile rather than changing meaning).
is_local ๐Ÿ”’
substitute ๐Ÿ”’
Replace the names bound by lets in tokens with their values, in parentheses.
target_check ๐Ÿ”’
The runtime check that the items of a prefetched loop name different files.
terminates ๐Ÿ”’
True when body may end the loop it belongs to: a ? or return anywhere outside closures and async blocks, a break that leaves it, or a continue aimed 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 and ifs without else at the end are statements of type ().