Skip to content

Strings are views over adaptive-width buffers; UTF-8 streams in chunks - #873

Closed
phenomenon0 wants to merge 3 commits into
bendlang:mainfrom
phenomenon0:pr/strings
Closed

phenomenon0 wants to merge 3 commits into
bendlang:mainfrom
phenomenon0:pr/strings

Conversation

@phenomenon0

Copy link
Copy Markdown

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.

  • C decode of 8 MiB with File.read + String.length: ASCII 128 -> 25 ms, Unicode 87 -> 28 ms. Peak RSS 198,600 -> 18,512 KiB (ASCII). JS is at parity.
  • tests/strings: 21 tests, each passing check, interp, JS and C. All tests: 1335 / 1335 (1314 upstream plus these 21).
  • Out of tree: an ASan+UBSan probe of the string runtime. 84 M allocations end with zero live; the streaming partition law holds on 177 k C and 180 k JS partitions; 83 injected allocation failures.

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):

ASCII Unicode
Rust 1.92 (read_to_string + chars().count()) 4.0 ms / 10 MB peak 7.8 ms / 10 MB
Python 3.11 (open().read() + len()) 8.0 ms / 31 MB 16.3 ms / 43 MB
Go 1.23 (ReadFile + RuneCountInString) 10.4 ms / 18 MB 11.7 ms / 19 MB
Node 22 (readFileSync utf8 + count) 11.3 ms / 62 MB 56.8 ms / 64 MB
Bend (this branch, C lane) 26.4 ms / 18 MB 27.9 ms / 30 MB

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:

  • gates/repo.ts lifts comp.ts from 62000 to 75000 ttok (74112 now).
  • List and Char join RUNTIME_ADTS, since the runtime builds Nil/Con/Chr cells. A user constructor that mangles to CID_NIL or CID_CON now clashes, as one mangling to CID_CHR already does.
  • Non-scalar Chars behave as upstream: JS rejects them at char_new, and interp and C keep the cell.

🤖 Generated with Claude Fable 5.1

fable and others added 3 commits September 19, 2026 13:40
…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]>
@VictorTaelin

Copy link
Copy Markdown
Contributor

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 comp.ts and base.bend, far past the complexity budget those files live under, and it changes the representation of String that every backend, effect and test currently agrees on. Taking it would mean owning that representation change across the whole runtime at once. What we will do is try to bring this in within the complexity cap in the future, in smaller steps, and your design is the reference for that. Closing the PR, with thanks for the work and the ambition.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants