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; };
|
||||
|
||||
@@ -127,11 +127,14 @@ fn putstr(s: str, into: []u8, off: i32) i32 = {
|
||||
match (c) { case void => {}; case io.closed => fail(); };
|
||||
};
|
||||
|
||||
// ---- dynamicgrow: every cap doubling exercised --------------------------
|
||||
// ---- dynamicgrowcases: every cap doubling exercised ---------------------
|
||||
|
||||
// Drive grow 0 → 8 → 16 → 32 by writing sized chunks. Verify
|
||||
// accumulated `pos` after each step.
|
||||
@test fn dynamicgrow() void = {
|
||||
// accumulated `pos` after each step. Suffix `cases` mirrors
|
||||
// `fixedwritecases` / `borrowedreadcases`; bare `dynamicgrow`
|
||||
// would collide with memio.ww's private `dynamicgrow` in the same
|
||||
// package.
|
||||
@test fn dynamicgrowcases() void = {
|
||||
let mem: memio.state;
|
||||
let s: io.stream;
|
||||
memio.dynamic(&mem, &s);
|
||||
@@ -352,7 +355,7 @@ fn putstr(s: str, into: []u8, off: i32) i32 = {
|
||||
export fn main() i32 = {
|
||||
signalled = 1; fixedread();
|
||||
signalled = 2; fixedwritecases();
|
||||
signalled = 3; dynamicgrow();
|
||||
signalled = 3; dynamicgrowcases();
|
||||
signalled = 4; dynamicreset();
|
||||
signalled = 5; borrowedreadcases();
|
||||
signalled = 6; stringview();
|
||||
|
||||
Reference in New Issue
Block a user