Repository navigation
Conversation
|
Not a criticism of your PR, but i think it would make more sense to have these functions take an array of bytes input in the future |
| handle (`File`, `Socket`, `Window`) is an affine, opaque value, so every effect | ||
| on one hands it back beside its result, and no program can forge or reuse one. | ||
|
|
||
| A file and a socket carry bytes, and each has a text reader and a byte reader: |
There was a problem hiding this comment.
This is not needed in the guide, it's pretty self explanatory by the function signatures
|
Tested on macOS (arm64), merged onto 2.0.26. This looks good to me: the new effects match Two requests before merge: 1. Test the paths
|
|
Thank you for testing it on macOS, and for writing the edge test. It's in as I also took the optional one, since it's the same shape the other two files already have: in 27454fd On arrays of bytes: agreed. Your measurement matches what I saw (a One small edge I noticed while testing, in case it's useful: with |
…poll_bytes A socket carries bytes and TCP.recv answers a String, so io_str decodes each read as UTF-8 and writes U+FFFD over what it cannot decode. Two kinds of payload do not survive that. A body that is not text -- an image, a protobuf frame, a gzip stream -- loses every byte that is not valid UTF-8. And a perfectly valid character loses itself when the network splits it across two reads: each read decodes on its own, with no state carried over, so "café" sent as 63 61 66 C3 | A9 arrives as code points 99 97 102 65533 and 65533. Nothing in such a program is wrong; the split is the network's choice and it happens under load. File already draws this line: File.read decodes, File.read_bytes answers "the bytes as they are (0..255), one List cell each", and its comment in effs/file_read.c says a text reader would decode them as UTF-8. This commit gives TCP the same pair, with the same types and the same shape: TCP.send_bytes beside TCP.send, TCP.recv_bytes beside TCP.recv, and TCP.poll_bytes beside TCP.poll, so a server with a deadline can read bytes too. No new type, no new concept: List<&2, U32>, as File uses. The text effects keep their code untouched, now under #ifdef CID(TCP.*) as effs/file_read.c does, since each pair shares one effect file; the send loop and the poll deadline are shared rather than copied. The JS lane mirrors the C lane through the same pack-shaped parameter. tests/io/tcp_bytes_split.bend sends a character in two halves and a three-byte payload that is not text, and reads both back byte for byte (99 97 102 195 / 169 / 255 0 128). It checks, interprets, JS-builds and C-builds; tcp_listen_close, tcp_loopback, tcp_poll, tcp_spawn, tcp_short_recv, tcp_send_slow_peer, tcp_connect_black_hole and port_bound keep their output on both runnable lanes. Co-Authored-By: Claude Opus 5 <[email protected]>
…y send, a byte past 255, poll_bytes idle, with data and at EOF
Written in review of this PR. It passes the check, interpreted, JS and C
lanes here (Linux x86-64): an empty send_bytes succeeds, a byte past 255
fails with EINVAL and sends nothing, poll_bytes answers None{} idle,
Some{bytes} with data and Some{[]} at EOF, as recv_bytes answers [] there.
Co-Authored-By: Nico Abril <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…CP.send already do, and the guide keeps no paragraph for them tcp_recv.c's recv/EAGAIN loop lived twice; it is now tcp_recv_step, which takes the caller's own wake and a pack function (text for TCP.recv, a List of bytes for TCP.recv_bytes), plus a shared tcp_recv_start. The JS twin already shared tcp_recv_with. The guide paragraph on text and byte readers is gone: the signatures say it. tcp_bytes_edges, tcp_bytes_split and the other tcp_*, udp_*, file_* and port_bound tests (19) print their expected lines on the check, interpreted, JS and C lanes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Rebased onto main (95317d9): #1042 changed how effect files name things, and the branch no longer merged. The new effects now follow its form in both lanes: Re-run on the new base, Linux x86-64:
macOS was not re-run here after the rebase. |
| @@ -5,15 +5,25 @@ | |||
| // nothing parks on the socket and on the clock, whichever fires first; | |||
There was a problem hiding this comment.
This comment above was for tcp_poll
| function tcp_poll(socket, max, ms, k) { | ||
| // The bytes as they are (0..255), one List cell each, as File.read_bytes | ||
| // gives them. | ||
| function tcp_poll_list(b, n) { |
There was a problem hiding this comment.
This should probably be made into a array-to-list helper at the shared runtime, like io_text
| return xs; | ||
| } | ||
|
|
||
| function tcp_poll_with(socket, max, ms, k, pack) { |
There was a problem hiding this comment.
poll_with now receives a function argument, check if the performance is still the same.
| // still finds nothing (the socket is non-blocking) parks again, on `more`, | ||
| // the caller's own wake. `pack` turns what was read into the answer: text | ||
| // for TCP.recv, bytes for TCP.recv_bytes. The rest is the same for both. | ||
| static Term tcp_recv_step(Env e, IoWork* w, Term (*more)(Env, IoWork*), |
There was a problem hiding this comment.
same thing, test the performance
| // past the deadline it answers None{}, else Some{data} ("" is the peer's | ||
| // close, as TCP.recv answers it). | ||
| function tcp_poll(socket, max, ms, k) { | ||
| // The bytes as they are (0..255), one List cell each, as File.read_bytes |
There was a problem hiding this comment.
This comment is incomprehensive.
…ild a String Asked in review. The six copies of the bytes-to-list loop (File.read_bytes and File.read_at, TCP.recv_bytes, TCP.poll_bytes, in C and JS) now call io_list, next to io_str in the C runtime and io_text in the JS one. The C helper sits under #if defined(CID(Nil)) && defined(CID(Con)): a program with no List has no such ids, and without the guard every IO program that does not use one failed to build. TCP.poll's comment is back above tcp_poll, where it stood before this PR. The 19 tcp_*, udp_*, file_* and port_bound tests print their expected lines on the check, interpreted, JS and C lanes; gates/repo.ts passes 46 / 46. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks. All five are addressed in b930244:
The function argument costs nothing measurable. In C, clang inlines One thing I can't fully explain: in C, the byte effects are about 3% slower with The 19 |
nicolas-abril
left a comment
There was a problem hiding this comment.
Thanks, io_list looks good, and so do the performance numbers. I merged this onto current main (which now has #977 and #1061): it merges cleanly, tcp_bytes_split and tcp_bytes_edges pass on the check, interpreted, JS and C lanes, the other tcp_*, udp_*, file_* tests and port_bound still pass, and gates/repo.ts is 46/46.
Three more things before merge:
-
Share the poll step in
tcp_poll.c.tcp_poll_bytes_moreandtcp_poll_bytes_runare copies oftcp_poll_moreandtcp_poll_run, with onlyio_strswapped forio_list. Please share them through apackfunction the waytcp_recv.cnow does withtcp_recv_step(andtcp_poll.jswithtcp_poll_with). -
Share the list-to-buffer direction too.
tcp_send_bytes_runin C repeatsfile_write_bytes_run's loop, andtcp_send_bytesin JS repeatsfile_write_bytes's. Please do what you offered: one helper for each runtime, next toio_list(andio_cstr/io_bytes), used by bothFile.write_bytesandTCP.send_bytes. -
Fewer comments. Most of the new ones restate what the signature or the function name already says (e.g. "TCP.poll with the bytes as they are, the pair of TCP.recv_bytes…", and the ones on
io_listand abovetcp_send_bytes). The one abovetcp_recv_listexplains whyTCP.recvloses bytes, which belongs in the PR, not the code. Please keep only comments that say something the code doesn't.
On your questions: the ~3% in C for io_list is fine, one shared loop is worth more. Refusing max = 0 makes sense, but as its own PR.
…recv_bytes do
tcp_poll_step(e, w, more, pack) takes over the deadline logic that
tcp_poll_more and tcp_poll_bytes_more used to duplicate; tcp_poll_start
factors the other duplicate, the handle/size/buffer setup. Each effect
keeps only its thin _more (one call into the shared step) and _run
(start, then park). pack is the raw builder, Term (*)(Env, const char*,
u64): io_str for TCP.poll, io_list for TCP.poll_bytes, matching what
tcp_poll.js already passes to tcp_poll_with (io_text / io_list). The step
wraps the result itself (io_done(io_box(Some{pack(...)}))), so pack does
not need to know about IoWork or the Some{} wrapping.
tcp_recv.c's tcp_recv_step took the same pack signature, which drops the
tcp_recv_text and tcp_recv_list wrapper functions: tcp_recv_step now
calls io_str / io_list directly and wraps the result in io_done itself,
the same way tcp_poll_step does. This also removes the comment that
explained why TCP.recv loses bytes (UTF-8 decoding per call, split
characters not surviving) — that belongs in the PR thread, not in code
that no longer has a natural place to hang it once the wrapper it
documented is gone.
Both io_str and io_list stay inside the #ifdef CID(TCP.poll) / CID(TCP.
poll_bytes) and CID(TCP.recv) / CID(TCP.recv_bytes) sections, as before,
so a program that never uses List still builds (io_list itself only
exists under #if defined(CID(Nil)) && defined(CID(Con)), and nothing
outside those effect blocks references it).
Lanes (check/interp/js/c) all PASS on tcp_poll, tcp_bytes_split,
tcp_bytes_edges, the other tcp_*, udp_*, file_* tests and port_bound.
bun gates/repo.ts: PASS 46/46.
Co-Authored-By: Claude Sonnet 5 <[email protected]>
…io_clist io_clist sits next to io_list in both runtimes (bend2/comp.ts), as io_cstr sits next to io_str in C and io_bytes sits next to io_text in JS: io_list already decodes a byte buffer into a List with the same name in both runtimes, so its encode-direction twin keeps that pairing. Semantics are unchanged from the two copies it replaces: every value 0..255 becomes one byte; a value past 255 fails the effect with EINVAL (22) and nothing is written or sent; in C every list cell is still consumed and freed by the same loop, including after a bad value (io_clist always returns a buffer to free, and drains the list to the end via *bad instead of an early return). File.write_bytes's early return (w->code ? file_write_pack(...) : io_work(...)) and TCP.send_bytes's path through tcp_send_more are unchanged. Lanes (check/interp/js/c) all PASS on tcp_bytes_split, tcp_bytes_edges, the other tcp_*, udp_*, file_* tests and port_bound. bun gates/repo.ts: PASS 46/46. Co-Authored-By: Claude Sonnet 5 <[email protected]>
…esn't already say
Trims comments the PR added that restated the signature or the function
name: io_list's ("io_list is io_str's/io_text's twin for bytes...", both
runtimes — the #if guard and the function body already say as much),
tcp_poll_bytes's/tcp_poll.js's ("TCP.poll with the bytes as they are, the
pair of TCP.recv_bytes...") and tcp_recv_bytes's JS twin of the comment
already dropped from tcp_recv.c in the previous commit ("TCP.recv decodes
the same bytes as UTF-8...").
Lanes (check/interp/js/c) all PASS on every tcp_*, udp_*, file_* test and
port_bound. bun gates/repo.ts: PASS 46/46.
Co-Authored-By: Claude Sonnet 5 <[email protected]>
…es takes 141 ms in C, where the loop it replaced took 181 io_clist wrote *bad through its pointer on every cell, and since the byte store into buf may alias it, clang loaded and stored it on every turn. A local flag, stored once at the end, stays in a register. File.write_bytes of 50M bytes to /dev/null, medians of 7 interleaved runs in C: 181 ms before this round (the inline loop), 191 ms with the first io_clist, 141 ms now; the JS lane is unchanged. Its two comments go as well: what they said, the code says. The 19 tcp_*, udp_*, file_* and port_bound tests print their expected lines on the check, interpreted, JS and C lanes; gates/repo.ts passes 46 / 46. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks for merging it onto main and running everything again, and for the clear list. All three are in, in three commits on top of b930244, plus a small fix that came out of measuring them:
Performance. Drained over loopback as last time (256 MB in C). Medians of 7 interleaved runs on an idle machine:
Checks (Linux x86-64):
The |
…ce next to io_str 87a1e9c moved io_str's comment into the section header, which was the only conflict. The poll step picks up main's w->time in place of the io_wait_time it replaced. The 20 tcp_*, udp_*, file_* and port_bound tests (tcp_listen_backlog included) print their expected lines on the check, interpreted, JS and C lanes, and so do programs that mix both effects of each pair and programs with no List; gates/repo.ts passes 46 / 46, comp.ts at 62984 of its 64000 ttok. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks for the three review rounds on this, @nicolas-abril. The piece 2.0.32 leaves out is |
bendlang#978, which was to rewrite them, closed, so they fold like the rest. Co-Authored-By: Claude Opus 5.5 <[email protected]>
What changes
TCP gets the byte twins
Filealready has, with the same types and thesame shape:
File.readFile.read_bytes(today)File.writeFile.write_bytes(today)TCP.recvTCP.recv_bytesTCP.sendTCP.send_bytesTCP.pollTCP.poll_bytesList<&2, U32>, exactly asFile.read_bytesandFile.write_bytesalready use. No new type and no new concept. The text effects keep their
code unchanged, now under
#ifdef CID(TCP.*)the wayeffs/file_read.calready guards its pair, since each pair shares one effect file; the send
loop, the receive step and the poll deadline are shared rather than
copied, and the JS lane mirrors the C lane through the same pack-shaped
parameter.
TCP.poll_bytesis in the set becauseTCP.pollis the only receivewith a deadline: without it, a server that drops idle connections cannot
read bytes.
Why
A socket carries bytes.
TCP.recvanswers aString, soio_strdecodes each read as UTF-8 and writes U+FFFD over whatever it cannot
decode. Two kinds of payload do not survive that.
A body that is not text — an image, a protobuf frame, a gzip
stream — loses every byte that is not valid UTF-8.
FF 00 80reads backas three replacement characters, and
TCP.sendcannot spell those byteseither, since it encodes the String as UTF-8.
A valid character split across two reads. This is the one that bites
a correct program.
"café"is63 61 66 C3 A9; the network may deliverC3at the end of one read andA9at the start of the next. Each readdecodes on its own, with no state carried over, so both halves become
U+FFFD. Measured against a Bend receiver:
Sent whole, the same bytes give
99 97 102 233. Nothing in that programis wrong: the split is the network's choice, it depends on load, and the
loss is silent.
Filealready draws this line.File.readdecodes;File.read_bytesanswers, in the words of the comment in
effs/file_read.c, "the bytes asthey are (0..255), one List cell each; a text reader would decode them as
UTF-8". A socket is the same seam as a file, and today only the file has
both doors.
This is also where #809 leaves things. That issue found the two lanes
decoding invalid UTF-8 differently, and the fix in 2.0.7 made the C
runtime a forward WHATWG decoder, one U+FFFD per maximal subpart, exactly
as
TextDecoderdoes. That is the right decode — and it is preciselywhy a decoding reader cannot be the only way to read a socket: now that
both lanes agree, they agree on discarding the bytes. The decoder is
correct; what is missing is the door that does not decode.
Alternatives considered
Make
TCP.recvcarry decoder state between calls, so a charactersplit across two reads is stitched back. Rejected: it would make a
recvstateful and still leave no way to read a byte that is not text,so the binary case stays broken. It would also change what an existing
TCP.recvreturns.Preserve undecodable bytes inside the String (surrogate escapes, as
Python's
surrogateescapedoes). Rejected: aStringwould stop beinga sequence of valid code points, and the split-character case still needs
the program to re-decode by hand — the work of bytes, with a type that
says text.
Ship only
recv_bytes/send_bytesand leavepoll_bytesout.Rejected:
TCP.pollis the only receive with a deadline, so a serverthat times out idle connections would be forced back to text.
UDP.recv_fromandUDP.pollhave the same seam and are not touchedhere, to keep this reviewable; happy to follow up with the same pair for
UDP if you want it.
Test output
tests/io/tcp_bytes_split.bendsends a character in two halves and thenthree bytes that are not text, reading both back over loopback in one
program:
Checked, interpreted, JS-built and C-built on this tree
(
bun bend2/main.ts <file> [--check-only | -o x.js | -o x]): all fourlanes print those three lines. The same exchange through
TCP.recvgives
99 97 102 65533/65533instead, which is the defect.tests/io/tcp_bytes_edges.bend, written in review, covers the edges:an empty send, a byte past 255 (
EINVAL, nothing sent), andpoll_bytesidle, with data and at EOF, whererecv_bytesanswers[].No regressions, on Linux x86-64 with Bun 1.3.11, on main 95317d9:
tcp_*,udp_*,file_*andport_boundtests, the twonew ones included, print their expected lines on the check,
interpreted, JS and C lanes.
tests/iocheckup (119 modules, one process, asgates/test.tsruns it) is identical to main's but for the two new tests' sections.
gates/repo.tspasses 46 / 46.registered twice, so a program that uses both effects of a pair must
still build:
tcp_bytes_splitmixesrecvandrecv_bytes, and ascratch program mixing
send/send_bytesandpoll/poll_bytespasses all four lanes.
macOS was not re-run here after the rebase onto #1042.
Duplicate check
No open issue or PR asks for byte-level socket effects. #809 (closed,
fixed in 2.0.7) is the decode-divergence issue this builds on, and #846
asks about bundled HTTP/TLS/crypto libraries, which is a different
question — this adds no library, only the byte door on the effect that
already exists.
WONTFIX.txtdoes not cover it.