✓
Passing This code compiles and runs correctly.
Code
// BULK INSERT WITH HANDLES — a counted `for` insert into a store that
// carries generational handles still lowers to the batched fill. The
// emitted loop mints the SAME slot sequence the per-row path would —
// freelist pops in LIFO order (`free[frem-1-j]`), then bump allocations
// (`next+k`) — so refilled rows carry bit-identical handles and the
// freelist drains in the same order. The only per-row cost that cannot
// be batched is a reactive enter, and that is positional: a rule fires
// for insert sites BELOW its declaration (`__site_line > qs.line`), so
// sites declared ABOVE every rule are provably safe to batch.
//
// `units` carries handles because `snap` names them with `[id]`.
// `fill` and `refill` sit above every rule's declaration — both bulk.
// `tallyfill` sits below `counter`, so it stays per-row and the rule
// fires on each of its inserts: tally lands at 2, proving the reactive
// enters the batch path would have skipped still happen.
//
// The freelist-order pin: cull takes slot 1 (hp 10), cull2 takes slot 3
// (hp 30) — freelist [1, 3]. `refill` pops LIFO: hp 100 mints slot 3,
// hp 200 mints slot 1. `snap` then records each live row's packed handle
// beside its hp; the printed pairs prove which slot each refilled row
// took (generation 1 sits in the high bits of both refilled handles).
import std/io
import std/store
import std/control
std/store:new(units, capacity: 64) { hp: i64 }
std/store:new(seen, capacity: 16) { h: 0[i64], hp: 0[i64] }
std/store:new(tally) { n: 0[i64] }
tor fill {}
fill = for(0..4)
! each i |> std/store:insert(units) { hp: @intCast(i * 10) }
tor refill {}
refill = for(0..2)
! each i |> std/store:insert(units) { hp: @intCast(100 + i * 100) }
tor counter {}
counter = std/store:rule(units)
! row e when e.hp == 999 |> std/store:stored { tally.n: tally.n + 1 }
tor tallyfill {}
tallyfill = for(0..2)
! each _ |> std/store:insert(units) { hp: 999 }
tor cull {}
cull = std/store:rule(units)
! row e when e.hp == 10 |> std/store:take(units[e])
| item _ |> _
tor cull2 {}
cull2 = std/store:rule(units)
! row e when e.hp == 30 |> std/store:take(units[e])
| item _ |> _
tor snap {}
snap = std/store:rule(units)
! row { [row]e, [id]h } |> std/store:insert(seen) { h, hp: e.hp }
| full |> _
fill()
tallyfill()
cull()
cull2()
refill()
snap()
std/io:print.ln("tally {{ tally.n:d }}")
std/io:print.ln("units {{ units.len:d }}")
std/store:query(seen)
! query s |> std/io:print.ln("h {{ s.h:d }} hp {{ s.hp:d }}")
Actual
tally 2
units 6
h 16777216 hp 0
h 16777221 hp 999
h 16777218 hp 20
h 16777220 hp 999
h 4311744515 hp 100
h 4311744513 hp 200
Expected output
tally 2
units 6
h 16777216 hp 0
h 16777221 hp 999
h 16777218 hp 20
h 16777220 hp 999
h 4311744515 hp 100
h 4311744513 hp 200
Flows
flow ~new click a branch to expand · @labels scroll to their anchor
new (units, capacity: 64, source: hp: i64)
flow ~new click a branch to expand · @labels scroll to their anchor
new (seen, capacity: 16, source: h: 0[i64], hp: 0[i64])
flow ~new click a branch to expand · @labels scroll to their anchor
new (tally, source: n: 0[i64])
subflow ~fill click a branch to expand · @labels scroll to their anchor
for (0..4)
subflow ~refill click a branch to expand · @labels scroll to their anchor
for (0..2)
subflow ~counter click a branch to expand · @labels scroll to their anchor
rule (units)
subflow ~tallyfill click a branch to expand · @labels scroll to their anchor
for (0..2)
subflow ~cull click a branch to expand · @labels scroll to their anchor
rule (units)
subflow ~cull2 click a branch to expand · @labels scroll to their anchor
rule (units)
subflow ~snap click a branch to expand · @labels scroll to their anchor
rule (units)
flow ~fill click a branch to expand · @labels scroll to their anchor
fill
flow ~tallyfill click a branch to expand · @labels scroll to their anchor
tallyfill
flow ~cull click a branch to expand · @labels scroll to their anchor
cull
flow ~cull2 click a branch to expand · @labels scroll to their anchor
cull2
flow ~refill click a branch to expand · @labels scroll to their anchor
refill
flow ~snap click a branch to expand · @labels scroll to their anchor
snap
flow ~print.ln click a branch to expand · @labels scroll to their anchor
print.ln (expr: "tally {{ tally.n:d }}")
flow ~print.ln click a branch to expand · @labels scroll to their anchor
print.ln (expr: "units {{ units.len:d }}")
flow ~query click a branch to expand · @labels scroll to their anchor
query (seen)
Test Configuration
MUST_RUN LANGUAGES: zig
Post-validation Script:
#!/bin/bash
# Emitted-shape oracle for bulk insert with handles. `units` carries
# generational handles (`snap` names them via `[id]`), so its counted
# `for` fills lower to the bulk block WITH slot minting: LIFO freelist
# pops (`free[frem-1-j]`), bump-fresh for the rest, both map writes,
# and the free_len/next writeback after the loop. `tallyfill` sits below
# the `counter` rule's declaration, so it declines — the per-row insert
# call and its reactive `__site_line` gate must still be in the emit.
set -u
if [ ! -f output_emitted.zig ]; then
echo "FAIL: no output_emitted.zig"
exit 1
fi
# --- the bulk block mints handles: freelist-pop-then-bump ---
if ! grep -q '__koru_bulk_frem = __koru_store_units.__koru_hslot_free_len' output_emitted.zig; then
echo "FAIL: no freelist length captured for units"
exit 1
fi
if ! grep -q '__koru_bulk_fresh = __koru_store_units.__koru_hslot_next' output_emitted.zig; then
echo "FAIL: no bump-alloc cursor captured for units"
exit 1
fi
if ! grep -q '__koru_store_units.__koru_hslot_free\[__koru_bulk_frem - 1 - __koru_bulk_j\]' output_emitted.zig; then
echo "FAIL: freelist pops are not LIFO-ordered"
exit 1
fi
if ! grep -q '__koru_store_units.__koru_row_hslot\[__koru_bulk_base + __koru_bulk_j\] = __koru_bulk_slot' output_emitted.zig; then
echo "FAIL: row->slot map write missing"
exit 1
fi
if ! grep -q '__koru_store_units.__koru_hslot_row\[__koru_bulk_slot\] = __koru_bulk_base + __koru_bulk_j' output_emitted.zig; then
echo "FAIL: slot->row map write missing"
exit 1
fi
if ! grep -q '__koru_store_units.__koru_hslot_free_len = __koru_bulk_frem -| __koru_bulk_n' output_emitted.zig; then
echo "FAIL: freelist writeback missing"
exit 1
fi
if ! grep -q '__koru_store_units.__koru_hslot_next = __koru_bulk_fresh' output_emitted.zig; then
echo "FAIL: bump-alloc writeback missing"
exit 1
fi
# --- tallyfill declined: per-row insert call + reactive enter gate ---
if ! grep -q '__store_insert_units_event.handler(' output_emitted.zig; then
echo "FAIL: declined site lost its per-row insert call"
exit 1
fi
if ! grep -q 'if (__site_line >' output_emitted.zig; then
echo "FAIL: declined site lost its reactive enter gate"
exit 1
fi
echo "PASS: counted-for insert into a handle store bulks with exact mint order; below-rule site stays per-row"
exit 0