diff --git a/stdlib/socket.rail b/stdlib/socket.rail index 6ae20f5..3acf5fc 100644 --- a/stdlib/socket.rail +++ b/stdlib/socket.rail @@ -193,12 +193,16 @@ accept_tcp listen_fd = cfd -- Copy `n` bytes from buf[off..off+n) into a Rail string. --- Uses char_from_int + join; O(N²) alloc, fine for small reads. +-- Builds a list of single-char strings then joins once: O(N) allocation +-- instead of O(N²) from repeated prefix-copying concat. tcp_bytes_to_str buf off n acc = - if n <= 0 then acc + join "" (tcp_bytes_to_list buf off n) + +tcp_bytes_to_list buf off n = + if n <= 0 then [] else let c = byte_at buf off - tcp_bytes_to_str buf (off + 1) (n - 1) (join "" [acc, char_from_int c]) + (char_from_int c) : (tcp_bytes_to_list buf (off + 1) (n - 1)) -- Receive bytes until the HTTP header terminator \r\n\r\n appears, then -- continue reading Content-Length bytes of body if present. Returns the