summaryrefslogtreecommitdiff
path: root/chapter33/docs/growing-arrays.md
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-11-30 10:43:11 -0800
committerCliff Click <cliffc@acm.org>2026-11-30 10:43:11 -0800
commitd9a46e588fe09ed58e160778b0d8dcee62d4d0e4 (patch)
tree80fdd9e8294636c0e666fa52db7cac4a9b8c8d01 /chapter33/docs/growing-arrays.md
parentc37676fc0c04997f51f4659848570619f68ac10d (diff)
downloadSimple-d9a46e588fe09ed58e160778b0d8dcee62d4d0e4.tar.gz
Simple-d9a46e588fe09ed58e160778b0d8dcee62d4d0e4.zip
Checkpoint Allocation2 before separate-compilation redesignch33
Replace the legacy allocation planner, deletion proof engine, and independent FunctionEffects pass with Allocation2 as the sole allocation-analysis phase. Keep one Site per optimized allocation/static object, per-site weak field facts, dependency-driven escape propagation, incoming-edge counts, and points-to dumps. Move the shared node WorkList into util. Normalize Never exits before allocation analysis and TypeCheck while preserving semantic nonreturning summaries. Keep internal class-init call dependencies out of global escapes, sharpen hidden private-memory defaults, refresh singleton constant escape sets after type closure, and publish field-local escape facts. Detach unloaded summary wrappers from scheduling roots. Retain declaration metadata and optimized return states in C0DA objects without legacy contracts. Keep qualified forward type names separate from expression member access, allow peepholes to return dead self, and retain the supporting regressions. Use integer types for symbolic field offsets and Opto-high uninitialized fields. Make Breakout's debounce helper private. Remove disabled escape implementations, obsolete null assertions, duplicate graphics compilation, survey runners/results, investigation transcripts, and source archives. Consolidate the selected ownership, lifetime, storage, deletion, and growing-array designs with their rationale/examples and academic references. Replace the obsolete Chapter33 handoff in the AI notes with current guidance. This is an unfinished analysis checkpoint. Imported pointer identities, transitive escapes, intrinsic calls, construction checks, and cofinite-summary universe rebasing still need work. Import loading order is unchanged. Validation: main/test compilation and git diff --check pass. Before/after make -k -j8 tests_nographics both report 825 executions and 78 failures, with unchanged extracted failure names; all lattice shards pass. Chapter33 retains its three failing deletion/imported-identity/transitive-escape regressions.
Diffstat (limited to 'chapter33/docs/growing-arrays.md')
-rw-r--r--chapter33/docs/growing-arrays.md272
1 files changed, 272 insertions, 0 deletions
diff --git a/chapter33/docs/growing-arrays.md b/chapter33/docs/growing-arrays.md
new file mode 100644
index 00000000..4a0d5046
--- /dev/null
+++ b/chapter33/docs/growing-arrays.md
@@ -0,0 +1,272 @@
+# Scope-owned growing arrays
+
+This is the selected language and storage design, with illustrative source
+examples. Writable array length, whole-array construction scopes, and growable
+data-stack lowering are not implemented. See [memory management](memory-management.md)
+for the analysis and storage-policy boundary.
+
+The selected rule is: `new E[]` creates an initially empty array whose growth
+belongs to the allocating lexical scope. Leaving that scope ends growth,
+including returning the array. Its storage can outlive that scope; its permission
+to grow cannot. Element mutation remains allowed. Returning from a helper that
+merely borrows someone else's builder does not end the owner's growth phase.
+
+## BrainFuck
+
+The interpreter body stays the same. The relevant old code in
+[BrainFuckTest](../src/test/java/com/seaofnodes/simple/BrainFuckTest.java) is:
+
+```text
+val output = new sys.aryu8();
+u8[] !data = new u8[100];
+// ... interpreter loop ...
+output.add(data[d]); // the '.' command
+// ...
+return output; // a growable wrapper
+```
+
+Proposed replacement:
+
+```text
+val data = new u8[100]; // fixed tape, before the growing tail
+val output = new u8[];
+// ... same interpreter loop ...
+output += data[d]; // the '.' command
+// ...
+return output; // ends this scope: result has fixed length
+```
+
+The caller changes from `brain_fuck().write(sys.libc.STDOUT)` to
+`sys.io.write(sys.libc.STDOUT, brain_fuck())`.
+`+=` appends to the existing array; it does not reassign the `val` binding.
+There is no wrapper, final trim-copy, or explicit finish operation.
+
+The proposed whole-array constructor gives the boundary a dedicated body:
+
+```text
+val data = new u8[100];
+val output = new u8[]{ ->
+ // ... interpreter; '.' does self += data[d] ...
+}; // length is now fixed
+sys.io.write(sys.libc.STDOUT, output);
+```
+
+The tape is scratch, while the output survives. Their placement still needs to
+permit reclaiming scratch without invalidating the output; ending growth alone
+does not establish that layout. The existing frozen BrainFuck workloads remain
+useful compiler regressions and need not be rewritten with the new example.
+
+## Agreed constructor scope and exits
+
+The whole-array body executes immediately as a lexical construction scope.
+It can read and write outer-scope variables, subject to their ordinary mutability
+and lifetime rules. It has its own return target and result destination, as if
+it were a function, but does not impose ordinary function capture restrictions.
+`self` is a fixed binding to the array being constructed.
+
+| Within `new E[]{ -> ... }` | Meaning |
+|---|---|
+| Reach the closing brace | Finish growth and yield `self`, regardless of the last statement's value |
+| `return self;` | Finish successfully now, skipping remaining constructor statements |
+| `return null;` | Abandon construction now and yield null |
+| `return anythingElse;` | Error in this context; the explicit result choices are self and null |
+| `return;` | Use `return self;` for explicit successful completion |
+| `break;` | Exit the innermost loop in this constructor; continue after that loop |
+| `continue;` | Continue the innermost loop in this constructor |
+
+The nearest function-like body owns a return. A constructor-local return does
+not return from the enclosing function. Conversely, returning from a helper
+called by the constructor returns only from the helper. Break and continue
+cannot cross the constructor boundary to a surrounding loop; without a local
+loop they are errors. EOF can therefore use `break` to reach final trimming,
+while a read failure uses `return null` to bypass the successful completion path.
+
+This fits the existing indexed generator's return scope:
+
+```text
+new int[N]{i -> if(i==0) return 7; return i*i;}
+```
+
+There, `return` supplies one element and the generator proceeds to the next
+index. In the zero-argument body it supplies the outcome of the whole-array
+construction. A null return in a generator of nullable elements remains a null
+element, not cancellation of the array. That distinction follows which body is
+executing; it must not depend on whether a loop happens to surround the return.
+The current indexed-generator parser rejects assignments to captured locals;
+that existing implementation restriction must not be carried over to the new
+whole-array construction scope.
+
+A whole-array body with a possible null exit has a nullable result. A body
+with no possible null exit has a non-null result. This does not change
+allocation-failure behavior or make every `new Foo` expression nullable.
+Generalizing fallible construction to ordinary classes is a separate choice;
+successful class construction would still have to satisfy initialization rules.
+
+Failure abandons the private construction storage, not its earlier side effects:
+file bytes already read remain consumed, and writes to outer locals and external
+state remain.
+Cleanup must preserve errno. References into abandoned storage must not survive
+its reclamation; the lifetime check includes the failure edge.
+
+Explicit returns keep completion distinct from mutation: assigning null to self
+would hide control flow, a negative length would violate the length invariant,
+and an ordinary break belongs to the nearest loop. No finish keyword is needed.
+
+## Growing and trimming with `#`
+
+Extend the existing length notation during construction:
+
+| Expression | Proposed meaning during construction |
+|---|---|
+| `buf#` | Current accessible element count |
+| `buf# = n` | Extend or trim the tail to checked length `n` |
+| `buf# += n` | Add `n` accessible elements at the end |
+| `buf# -= n` | Remove `n` elements; reject/check underflow |
+| `buf += value` | Append one initialized element |
+
+Length changes are permitted only while the allocating scope still owns growth,
+and when placement permits the tail adjustment. Exiting that scope removes the
+permission through all aliases. Ordinary `new E[N]` arrays never acquire it.
+Zero-length and same-length updates are legal; neither underflow nor length
+overflow wraps. Even a zero-length request to change a finished array's length
+uses an operation for which the growth permission is absent.
+
+For `u8`, extending the length gives initialized, zero-valued elements, consistent
+with current array construction. That keeps the entire visible array safe to
+read before the native call. A later optimization may omit initialization when
+it proves the bytes are overwritten or trimmed before observation. Zero-copy
+does not itself promise zero initialization cost. For inline objects without a
+default initializer, increasing `#` alone cannot construct valid elements; those
+arrays need append/construction into the new element destinations.
+
+## `io.read`: direct read into the growing tail
+
+The [current implementation](../src/main/smp/sys/io.smp) has three buffers over
+its lifetime: a fixed 8 KiB read chunk, the builder's replacement buffers, and
+the final exact-length array. Its data path is:
+
+```text
+libc.read(fd, chunkAddress, chunk#);
+buf._grow(count);
+ary.copy(buf.buf, buf.len, chunk, 0, count);
+buf.len += count;
+// At EOF:
+val out = new u8[buf.len];
+return ary.copy(out, 0, buf.buf, 0, buf.len);
+```
+
+Proposed replacement, including short reads, interrupts, errors, and the array
+length limit:
+
+```text
+val read = { i32 fd ->
+ return new u8[]{ ->
+ u32 used = 0;
+ while( true ) {
+ i64 room = sys.integer.MAX_U32 - used;
+ if( room > 8192 ) room = 8192;
+
+ // An exactly maximum-sized stream can still end successfully.
+ // Only this boundary case needs a separate byte for the EOF probe.
+ if( room == 0 ) {
+ val probe = new u8[1];
+ i64 ptr = probe;
+ i64 got = libc.read(fd, ptr, 1);
+ if( got < 0 ) {
+ if( libc.errno() == libc.EINTR ) continue;
+ return null;
+ }
+ if( got != 0 ) {
+ libc.set_errno(libc.EOVERFLOW);
+ return null;
+ }
+ break;
+ }
+
+ self# = used + room; // space for the next block, without moving self
+ i64 got = 0;
+ {
+ i64 ptr = self;
+ got = libc.read(fd, ptr + used, room);
+ } // end this raw-address borrowing scope
+ if( got < 0 ) {
+ if( libc.errno() == libc.EINTR ) continue;
+ return null; // abort this constructor
+ }
+ if( got == 0 ) break;
+ used += got; // native read promises 0 <= got <= room
+ }
+ self# = used; // drop unwritten tail; no data copy
+ }; // implicit success: yield finished self
+};
+```
+
+Here `used` counts input bytes and `self#` includes the writable tail. A short read
+advances `used` without shrinking on every iteration. The next length assignment
+restores up to 8 KiB of free tail. EINTR leaves `used` unchanged, so retry does not
+accumulate more space. EOF trims once. Empty input returns an empty array.
+The one-byte limit probe preserves acceptance of a stream whose length equals
+the maximum; reporting overflow before probing would wrongly reject it. On an
+oversized stream this version consumes one extra byte before failing, rather
+than the old implementation's possible full extra chunk.
+
+No regular read chunk and no final copy remain. Physical alignment or mapped-page
+slack can remain after trimming; the logical result length is exactly `used`.
+
+## Placement and borrowing constraints
+
+Floating formatting uses the same extend/fill/trim pattern as direct I/O:
+remember the current length, extend by enough space for the requested format,
+let a bounded C helper fill that tail and return the actual count, then trim to
+the old length plus that count. This is an agreed interface direction; the helper
+name and signature are not yet implemented. Any snprintf terminator needs space
+in the writable tail but is excluded from the published length. No temporary
+formatted-string allocation or final copy is needed.
+
+An arbitrary expression within interpolation is ordinary code running while an
+array is growing. It must satisfy the same stack-allocation rules and leave the
+growing end free for the next extension; interpolation adds no special exemption
+or additional lifetime mechanism.
+
+Both examples have a natural end of growth, so neither needs `finish(buf)` or
+`stop growing` syntax. The proposed new operation is a checked, writable `#`
+while growth is allowed. Scope exit does not guess the number of bytes a native
+call wrote: `io.read` must explicitly set the final length from `used`.
+
+The agreed placement is a parallel growable data stack, separate from the control
+stack. Its top is hidden call state; the updated top returns with the finished
+array. The caller's owning lifetime eventually restores the saved data-stack
+mark. A contiguous reserved address range lets the array grow without moving,
+subject to a checked capacity limit. This resolves the result destination without
+a final copy or relocating return addresses and spills. Actual lowering is still
+pending; retained inner results can still block an outer builder's growth.
+
+Growing a non-moving array preserves existing interior references. Trimming is
+different: references into the removed tail must be gone before that storage is
+reclaimed or reused. For these byte programs there are no typed interior
+references, and the native borrow ends before the final trim. Future inline
+objects need that restriction; non-moving alone does not make shrinking safe.
+
+## Persistent containers
+
+Finishable builders and persistent resizable containers have different needs.
+A byte stream, rendered string, or parsed number array can end growth at a
+lexical boundary. Timers, bitsets, and Canvas pixel buffers grow between unrelated
+operations and need an explicit container/storage policy. Consolidating coupled
+timer arrays into entries can simplify placement; it does not remove other live
+growth at the same stack tip. The storage heuristic may choose another growth
+stack, replacement buffers, or report an unsupported layout.
+
+Backing-array aliases are observable. Replacing `v.buf` or `Canvas.pixels` does
+not by itself authorize recycling the old array. An `E[^]` deletable array needs
+proof that no usable aliases or raw-address borrows survive deletion. Ordinary
+arrays retain their automatic-lifetime behavior. Buddy allocation can reuse the
+array header only after this proof; type-compatible element storage alone cannot
+make a stale length field safe. These ownership proofs and runtime recycling
+remain unimplemented.
+
+Time formatting should append into a supplied destination. Floating formatting
+can use the extend/fill/trim protocol above, with a bounded native helper writing
+directly into the destination. Arbitrary interpolation expressions obey ordinary
+lifetime and growth-placement rules: a retained inner result can obstruct the
+outer array's next extension.