cgen + memio: cgoutarena → memio.dynamic, grow → dynamicgrow (β-3)
Phase 0 last β-shape site. Two concerns in one commit because the refactor surfaced the rename: - selfhost/cmd/wcc/cgen.ww cgout buffer (cgoutbuf/cap/len + arena + cgout_grow + CGOUT_INIT_CAP) → memio.state + io.stream behind a one-shot lazy-init guard. cgout_enable drops its *arena param; memio.reset in cgout_flush keeps the buffer sticky across fns so the arena's amortisation survives — re-init per fn would abandon the buffer and re-grow from 0 via the 8→…→65536 ladder for every function (no io.close path → no os.free). - lib/memio/memio.ww private fn grow → dynamicgrow. Symmetric with dynamicwrite / dynamicclose; required because cstage bundles all imported modules into a flat TU and resolves private fns by unqualified name, so the new `import memio;` in wcc's bundle collided with selfhost/cmd/wcc/mem.ww's arena `grow`. Module-aware private-fn scoping in cstage is task #9. @test fn dynamicgrow in memiotest.ww (same package as memio.ww) renamed to dynamicgrowcases to free the name; new suffix mirrors the file's existing fixedwritecases / borrowedreadcases convention. Lazy-init guard cgoutinit. memio.dynamic runs once on first cgout_enable; subsequent enables just set cgoutmode. Mirrors lib/log/log.ww:124 ensureinit. Without it, ~14 mmap syscalls per fn and ~100 MiB+ cumulative leak on a typical bootstrap. io.write bare discard in emitbytes mirrors lib/log/log.ww:169 — memio.dynamicwrite never returns io.closed (memio.ww:166). Verified 132/132 incl. 995_self_rebuild byte-identity.
This commit is contained in:
@@ -166,7 +166,7 @@ fn fixedwrite(s: *io.stream, buf: []u8) (i32 | io.closed) = {
|
||||
fn dynamicwrite(s: *io.stream, buf: []u8) (i32 | io.closed) = {
|
||||
let m: *state = s.ctx: *state;
|
||||
let need: i32 = m.pos + buf.len;
|
||||
if (need > m.cap) { grow(m, need); };
|
||||
if (need > m.cap) { dynamicgrow(m, need); };
|
||||
let i: i32 = 0;
|
||||
for (i < buf.len) {
|
||||
m.ptr[m.pos + i] = buf[i];
|
||||
@@ -193,7 +193,14 @@ fn closenoop(s: *io.stream) (void | io.closed) = {
|
||||
|
||||
// Double-and-copy growth. Initial bump from 0 lands at 8 to amortise
|
||||
// small write bursts without a tail of reallocs.
|
||||
fn grow(m: *state, need: i32) void = {
|
||||
//
|
||||
// `dynamicgrow`, not Hare's bare `grow`: cstage bundles all imported
|
||||
// modules into a flat TU and resolves private fns by unqualified
|
||||
// name, so two `fn grow` decls (here + selfhost/cmd/wcc/mem.ww's
|
||||
// arena `grow`) collide. Module-prefixed name keeps the symmetry
|
||||
// with `dynamicwrite`/`dynamicclose` until task #9 (module-aware
|
||||
// private-fn scoping in cstage) lands.
|
||||
fn dynamicgrow(m: *state, need: i32) void = {
|
||||
let newcap: i32 = m.cap;
|
||||
if (newcap < 8) { newcap = 8; };
|
||||
for (newcap < need) { newcap *= 2; };
|
||||
|
||||
Reference in New Issue
Block a user