# HG changeset patch # User Kim Alvefur # Date 1625761319 -7200 # Node ID 735b8f4a6d7e127e950212a63cf91efbffd97993 # Parent 68f0196ece2aac3add7642040aba5799a4612f2e net.http: Send entire HTTP request header as one write When opportunistic writes are enabled this reduces the number of syscalls and TCP packets sent on the wire. Experiments with TCP Fast Open made this even more obvious. That table trick probably wasn't as efficient. Lua generates bytecode for a table with zero array slots and space for two entries in the hash part, plus code to set [2] and [4]. I didn't verify but I suspect it would have had to resize the table when setting [1] and [3], although probably only once. Concatenating the strings directly in Lua is easier to read and involves no extra table or function call. diff -r 68f0196ece2a -r 735b8f4a6d7e net/http.lua --- a/net/http.lua Thu Jul 08 17:57:44 2021 +0200 +++ b/net/http.lua Thu Jul 08 18:21:59 2021 +0200 @@ -164,13 +164,11 @@ t_insert(request_line, 4, "?"..req.query); end + for k, v in pairs(req.headers) do + t_insert(request_line, k .. ": " .. v .. "\r\n"); + end + t_insert(request_line, "\r\n") conn:write(t_concat(request_line)); - local t = { [2] = ": ", [4] = "\r\n" }; - for k, v in pairs(req.headers) do - t[1], t[3] = k, v; - conn:write(t_concat(t)); - end - conn:write("\r\n"); if req.body then conn:write(req.body);