✓
Passing This code compiles and runs correctly.
Code
// BULK APPEND — counted `for` whose `! each` body is a single insert
// into an observation-free store lowers to one hoisted capacity check,
// indexed column writes at `base + j`, and a single `len += n` after
// the loop. The per-row path's `len += 1` is a loop-carried store that
// blocks the optimizer; measured ~294us -> ~89us on a 100k-row fill by
// hand-editing the emitted loop before the lowering existed.
//
// `marks` qualifies on every clause — no handles, no watch, no rule,
// no lifecycle arms, no `| row`/`| full` on the insert. Its `| done`
// arm carries a real body (the notes write): the bulk block hands off
// to it through the same `__koru_continue` marker the template's own
// `{{ continuations["done"].continue }}` renders to, so post-loop work
// still runs once, after the fill.
//
// `kept` declines twice over — the insert binds `| row k` (handle
// machinery stays) and an armed insert inside `! each` is not the bare
// single-insert shape. Its rows land one event call at a time, with
// the row arm firing per insert. One program, both halves of the
// recognizer's decision.
import std/io
import std/store
import std/control
std/store:new(marks, capacity: 8) { on: i64 }
std/store:new(kept, capacity: 8) { hp: i64 }
std/store:new(notes) { n: 0[i64] }
for(0..5)
! each i |> std/store:insert(marks) { on: @intCast(i * 2) }
| done |> std/store:stored { notes.n: 5 }
for(0..3)
! each i |> std/store:insert(kept) { hp: @intCast(i * 10) }
| row k |> std/io:print.ln("row {{ k:d }}")
std/store:query(marks)
! query e |> std/io:print.ln("mark {{ e.on:d }}")
std/store:query(kept)
! query e |> std/io:print.ln("kept {{ e.hp:d }}")
std/io:print.ln("notes {{ notes.n:d }}")
Actual
row 33554432
row 33554433
row 33554434
mark 0
mark 2
mark 4
mark 6
mark 8
kept 0
kept 10
kept 20
notes 5
Expected output
✓ Zig✓ JavaScriptrow 33554432
row 33554433
row 33554434
mark 0
mark 2
mark 4
mark 6
mark 8
kept 0
kept 10
kept 20
notes 5
Flows
flow ~new click a branch to expand · @labels scroll to their anchor
new (marks, capacity: 8, source: on: i64)
flow ~new click a branch to expand · @labels scroll to their anchor
new (kept, capacity: 8, source: hp: i64)
flow ~new click a branch to expand · @labels scroll to their anchor
new (notes, source: n: 0[i64])
flow ~for click a branch to expand · @labels scroll to their anchor
for (0..5)
flow ~for click a branch to expand · @labels scroll to their anchor
for (0..3)
flow ~query click a branch to expand · @labels scroll to their anchor
query (marks)
flow ~query click a branch to expand · @labels scroll to their anchor
query (kept)
flow ~print.ln click a branch to expand · @labels scroll to their anchor
print.ln (expr: "notes {{ notes.n:d }}")
Test Configuration
MUST_RUN LANGUAGES: zig js
Post-validation Script:
#!/bin/bash
# Emitted-shape oracle for counted-for bulk append. `marks` is
# observation-free (no handles, watch, rules, lifecycle, or armed
# insert), so its `for(0..5) ! each i |> insert` lowers to a hoisted
# capacity check + `col[base + j]` writes + one `len += n` — and the
# per-row insert event is never CALLED (its proc still emits; the site
# transformed before the rewrite consumed the arm). `kept` binds
# `| row k`, so it keeps handles AND stays per-row: the
# `__store_inserth_kept` call must still sit inside a per-item `for`.
set -u
if [ ! -f output_emitted.zig ]; then
echo "FAIL: no output_emitted.zig"
exit 1
fi
# --- the bulk block is there, in the probed shape ---
if ! grep -q '__koru_bulk_base = __koru_store_marks.len' output_emitted.zig; then
echo "FAIL: no bulk base captured for marks"
exit 1
fi
if ! grep -q 'for (0..__koru_bulk_n)' output_emitted.zig; then
echo "FAIL: no bulk counted loop emitted"
exit 1
fi
if ! grep -q '__koru_store_marks.on\[__koru_bulk_base + __koru_bulk_j\]' output_emitted.zig; then
echo "FAIL: no indexed column write at base + j"
exit 1
fi
if ! grep -q '__koru_store_marks.len += __koru_bulk_n' output_emitted.zig; then
echo "FAIL: no single len += n after the loop"
exit 1
fi
if ! grep -q '__koru_bulk_n > 8 - __koru_store_marks.len' output_emitted.zig; then
echo "FAIL: capacity check was not hoisted out of the loop"
exit 1
fi
# --- the per-row insert event is dead code: emitted, never called ---
if grep -q '__store_insert_marks_event.handler(' output_emitted.zig; then
echo "FAIL: marks insert still called per row"
exit 1
fi
# --- the done arm's body still runs, once, after the fill ---
if ! grep -q '__store_write_notes_event.handler' output_emitted.zig; then
echo "FAIL: done arm body dropped — the continue splice did not fire"
exit 1
fi
# --- kept declined: per-row handle-returning insert inside the loop ---
if ! grep -q '__store_inserth_kept_event.handler(' output_emitted.zig; then
echo "FAIL: kept lost its per-row handle insert"
exit 1
fi
KEPT_LOOP=$(awk '/FLOW: .*for\(\)$/,/^ }$/' output_emitted.zig | grep -c 'inserth_kept')
if [ "$KEPT_LOOP" -lt 1 ]; then
echo "FAIL: kept's insert is not inside a for flow"
exit 1
fi
# --- JS target: never bulk — the event call stays per row ---
if [ -f output_emitted.js ]; then
if grep -q '__koru_bulk' output_emitted.js; then
echo "FAIL: JS lane emitted bulk append text"
exit 1
fi
if ! grep -q '__store_insert_marks_event.handler' output_emitted.js; then
echo "FAIL: JS lost the per-row insert call for marks"
exit 1
fi
fi
echo "PASS: counted-for insert lowers to bulk append; observed store stays per-row"
exit 0