Issue #1489: A list of all-zero elements is zeroed memory, not a fill: List::zeroed from the emitter, and a block’s untouched pages

Open and answer this issue on GitHub.

performance, blocked, future · opened 2026-11-09 · updated 2026-11-19 · 4 comments

Today

Since the switch to the block types (#490), vec[v; n] lowers to task_block::List::from_elem, which writes v into every element. Rust’s std Vec recognises an all-zero element (an internal specialization only std may use) and asks for zeroed memory instead (alloc_zeroed, calloc): fresh pages from the operating system are zero already, so nothing is written.

Measured on benches/brc/fast-unsafe.nika (vec[Slot{..}; 65536] of zeros, 3.6 MB): 101.9 -> 103.8 Ir/row (callgrind, 200k rows), the whole rise one memset, paid once per run; Table::run is identical to the instruction. callgrind overstates it: with calloc the kernel zeroes the pages on first touch, which callgrind does not count; what remains is one extra pass over the memory.

from_elem cannot test an element’s bytes for zero itself: padding bytes are uninitialised and reading them is undefined behaviour.

To build

  1. task_block::List::zeroed(n) (unsafe, for an element type whose all-zero bytes are a valid value): asks the Place allocator for zeroed memory. Outside a block that is alloc_zeroed (calloc), as Vec gets.
  2. The emitter writes List::zeroed(n) where vec[v; n] has a literal v whose every field is a numeric zero or false (recursively, no default field left out, no field of a type that may not be all zeros); otherwise from_elem as today.
  3. In a task’s block (with #490 step 5’s placement): the block remembers how far it has ever been handed out; a zeroed cut past that mark is memory the operating system gave and nobody wrote, and is not zeroed again. Before the mark it is zeroed.
  4. Tests in tests/compiler/: a zero-literal table and a non-zero one print the same as today; benches/brc/fast-unsafe.nika back to 101.9 Ir/row or below (callgrind, docs/measuring.md).

Done when

A list of all-zero elements is made without writing its memory outside a block, and in a block only where the block reuses memory.

Comments

keywan-ghadami, 2026-11-19

Brief queued: issue-1489


agent: builder model: opus max_turns: 200 budget_usd: 30 effort: medium —

Issue #1489

Your work order is issue #1489, inlined below with its comments. There is no brief written around it: plan your own steps from its To build list, build them in order, commit after each finished step, and end when its Done when holds (or at the limits of builder.md, reporting what is left). The facts in your prompt are Core of docs/builder-facts.md; more on a topic: nikaia run scripts/syntax.nika -- KEYWORD, a probe: nikaia run scripts/probe.nika -- SNIPPET.

Measure the issue against docs/issues.md while you work: what it lacked, what was wrong in it. That answer goes into the report’s Issue: field. A finding, a changed plan or a finished item also goes into the issue as a comment (gh api repos/Nikaia-Foundation/Nikaia/issues/1489/comments -F body=@file, ending with the Claude Code footer), rule I13.

Rules (CLAUDE.md; shared checkout)

Report (at most 15 lines, to exactly /home/user/Nikaia/docs/briefs/performance/issue-1489.md.report.md)

Fields: Result, Files, Commit, Body (optional), Fixes, Pushed, Tests, Gate, Deviation, Found, Needs, Changelog, Issue, Owner (a question for the owner), Comment (#N <text>, what to say on another issue; the driver posts it, one line each), Friction, Disk freed, and last Attribution: with your commit trailer lines below it.


Generated by Claude Code

keywan-ghadami, 2026-11-19

Run: issue-1489, Result: blocked

Tokens: input 54, cache read 1276779, cache write 55265, output 15189 Cost: 1.04 USD, time 3 min 15 s, turns 27

Report

Result: blocked Files: none Commit: none Body: nothing built or committed; lowering checked on the current binary: let mut slots: Vec[Slot] = [] + slots.resize(65536, Slot { a: 0, b: 0 }) lowers to nikaia_std::block::list(Place::Block) + resize, and [Slot { a: 0, b: 0 }; 1000] lowers to vec[Slot { a: 0, b: 0 }; 1000] (from_elem); benches/brc/fast-unsafe.nika’s table is the resize form (fn table), not vec[v; n] Pushed: none Tests: none run (nothing changed) Gate: green Deviation: none Found: Step 2 has the emitter write a call to an unsafe List::zeroed, but ADR-218 D4 lets emitted code hold unsafe only around foreign calls and nikaia_std::proven operations, and ADR-378 D2/D3 lets it hold unsafe only around a proven operation, when the switches are on, listed in D3’s test; nikaia_std::proven is the proven-index crate, which does not depend on task-block’s List. Probe: struct Slot { a: u64, b: i64 } / let t = [Slot { a: 0, b: 0 }; 1000] lowers today (nikaia lower p.nika) to let t = vec[Slot { a: 0, b: 0 }; 1000];, and the only unsafe in the file is unsafe { nikaia_std::proven::add::<i64>(..) }. No decision yet on whether “every field a literal zero” is a proved operation under ADR-378 (a proven:: entry, only when the switches are on), on which crate holds it (proven-index taking an allocator hook, or task-block reached through proven), or on a safe route instead (an AllZero trait sealed in task-block: primitives only, so user structs such as brc’s Slot would need an unsafe impl in emitted code) Found: Step 3’s premise does not hold: in crates/unsafe/task-block/src/lib.rs, segment() takes a segment from the thread’s SPAREs (another task’s memory, already written) or from System.alloc(MEMORY) (malloc of 64 KiB with page alignment, which may hand back reused heap memory), and Block::whole takes System.alloc(run_layout(..)). So no page of a block is known to be zero, and with System.alloc_zeroed at page alignment Rust’s std does an aligned alloc plus write_bytes, which is the same memset. Skipping the zeroing in a block needs segments from mmap (a libc dependency task-block lacks) or some other zero source. No decision yet: until then, a zeroed cut in a block can only be the default allocate plus write_bytes Needs: supervisor decision Changelog: none Issue: broke I6: the issue orders an unsafe call written by the emitter, a kind of unsafe ADR-218 D4 and ADR-378 D2/D3 do not admit, and it neither cites nor proposes a decision for it; broke I4a: step 3 says a block’s untouched memory is zero pages from the operating system, but segment()/Block::whole in task-block/src/lib.rs use System.alloc (malloc) or reused spares; broke I11: the issue names neither crates/unsafe/task-block/README.md (ADR-218 lists every unsafe there) nor the emitter files; the site to change is emit_stmt.nika’s resize fusion as well as emit_core_places.nika emit_core_list_repeat, since the bench uses [] + resize, not vec[v; n] Owner: none Comment: none Friction: none Disk freed: none (target/nikaia-project-tests 1.8G; / 7.5G free of 252G; no cache entries created) Attribution: Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com Claude-Session: https://claude.ai/code/session_01UVxJDukWgopUzVU2M1TjK7

Debrief

  1. Most cost: working out whether the emitter may write unsafe at all. That meant reading task-block/src/lib.rs (where segments come from, Place), the unsafe_code notes in nikaia-std/lib.rs, ADR-218 D4/D5 and ADR-378 D2/D3. It took about 15 turns, and I reached the conflict only after I had started planning the build.
  2. Looked up that the brief could have handed me: ADR-218 D4 and ADR-378, which limit the emitter’s unsafe. The two places that emit vec[v; n] (emit_core_places.nika emit_core_list_repeat, and the resize merge in emit_stmt.nika). How segment() and whole get their memory. That the benchmark’s table is [] + resize, not vec[v; n].
  3. Checks: one lowering probe (nikaia lower p.nika), which settled both emit sites and is worth keeping, and the disk check. None were wasted, and I did no build. The binary was older than HEAD, which did not matter for a lowering probe.
  4. One change: the brief’s rule list should add: “An issue that has the emitter write unsafe, or adds an unsafe API, is checked against ADR-218 D4 and ADR-378 D2/D3 before planning.” That one line would have ended the run in about 5 turns.
  5. What the issue lacked:
    • broke I6: no ADR covers the unsafe call the emitter is ordered to write, and the issue doesn’t say “no decision yet”.
    • wrong I4a: step 3 says a block’s untouched memory is zero pages from the operating system, but segments come from malloc or reused spares. My report tagged this broke I4a; wrong is the correct tag.
    • broke I11: the issue names no files: not task-block/README.md (the list of every unsafe), and not the emitter’s resize merge.
    • knowledge: Lowering | Rust's std System.alloc_zeroed at page alignment does an aligned alloc plus memset, so only mmap gives untouched zero pages.
  6. Traps for the next builder:
    • Fact: Lowering Generated code may hold unsafe only around foreign calls and nikaia_std::proven operations (ADR-218 D4, ADR-378 D2/D3), so a new unsafe API for the emitter needs a decision first.
    • Fact: Lowering [] followed by resize(n, v) is merged by emit_stmt.nika into vec[v; n] or List::from_elem_in, a second emit site beside emit_core_list_repeat.

Generated by Claude Code

keywan-ghadami, 2026-11-19

The report of the run of issue-1489 (#1489) judged this issue against docs/issues.md; the author fixes the issue by the rule it names (How a run judges its issue):


Generated by Claude Code

keywan-ghadami, 2026-11-19

Driver: not queued, this issue waits for #2179, #2180, which is still open (#1819). Set ready again when it is closed.


Generated by Claude Code

Back to Open Issues