Repository navigation
Strings are views over adaptive-width buffers; UTF-8 streams in chunks - #873
phenomenon0 wants to merge 3 commits into
Conversation
…code in chunks A String was a cell per code point walked one cons at a time, so length, indexing, slicing and compare cost a walk and 8 MiB of text cost a list. The C runtime now lays a String as a two-word view (buffer, offset and length) over cells 1, 2 or 4 bytes wide, the narrowest that fits every code point; slices share their buffer, and length, compare, search, split, replace and the case and trim family run natively in C and JS. Utf8.Dec decodes bytes in chunks with a carry across the cut (leads are sync points, so the chunked output equals the whole decode, WHATWG maximal subparts); File.read_text and TCP.recv_text stream through it, with effs/file_read_text_go and effs/tcp_recv_text_go as their kits. The C runtime builds List and Char cells, so both join the runtime ADTs. gates/repo.ts lifts comp.ts to 75000 ttok (74112 now). Co-Authored-By: Claude Opus 5 <[email protected]>
…text Each runs in all four lanes (check, interp, JS, C) against one printed answer: the ASCII and Unicode families, compare and hash laws, sharing of slices, deep and wide strings, map keys, and IO: a WHATWG fixture read whole (io_utf8), the same bytes and a corpus streamed through File.read_text at several chunk sizes (io_stream), and UTF-8 cut across TCP reads (io_sock_utf8). Co-Authored-By: Claude Opus 5 <[email protected]>
A caller of File.read_text threaded the file, the decoder and its state through its own loop to the end of the file. File.fold_text takes the step, a fuel bound on the chunk count, and a chunk size; it answers the folded state, closes the file, and dies as IO.try on a read error. tests/strings/io_fold counts the scalars of io_stream's byte fixture at max 1, 3 and 4096, and each answers 14. Co-Authored-By: Claude Opus 5 <[email protected]>
|
Thank you for this, and we want to be clear about how we see it: strings as views over adaptive-width buffers with streamed UTF-8 is important, and it is the direction String should go. This is not a "no" to the idea. We cannot take the PR: it is about 16,000 tokens across Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself. |
A String was a list of Char cells, so length, indexing, slicing and compare walked it, and 8 MiB of text cost a list of 8 M nodes. The C runtime now lays a String as a two-word view (buffer, offset and length) over cells 1, 2 or 4 bytes wide, the narrowest that fits its code points. Slices share their buffer, and length, compare, search, split, replace, trim, case and padding run natively in C and JS. The String API keeps its types.
Utf8.Dec decodes bytes in chunks with a carry across the cut. Leads are sync points, so any chunking answers the whole decode (WHATWG maximal subparts, as in #809). File.read_text and TCP.recv_text stream through it, and File.fold_text folds a step over a file's chunks and closes it.
How it compares. Same work - read the whole 8 MiB UTF-8 file and answer its character count - on a busy box (load ~6 of 16), median of 7, startup subtracted, all counts agree (8,388,608 / 4,934,470):
Rust's count is a validation-only walk and Node's ASCII count is O(1) on a one-byte string; Bend materializes every code point into a fitted buffer and answers from the view. Among the dynamic runtimes Bend is the memory-leanest, Node's Unicode decode costs 2x the time at 2x the RAM, and the whole-text family (search, split, replace, compare, slices) then runs over the view with no extra buffers.
Review notes:
🤖 Generated with Claude Fable 5.1