Repository navigation
perf(http-result): send buffered responses with Content-Length - #108
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Every AntelopeJS app answers through
HTTPResult.sendResponse. It calledwriteHeadbeforeend(body), so Node never learned the body length: every buffered response went outTransfer-Encoding: chunked. Node then writes it as awritevof header, chunk size, body and terminator, which took 30% of the server's CPU on a small JSON route. With aContent-Length, Node writes header and body in one write. This settles the ANT-1915 lead with an interleaved A/B through the api module. ANT-1925.What changed
sendResponseaddsContent-Length: Buffer.byteLength(body)to a buffered response.rawResponsesetContent-Length,Transfer-EncodingorTrailer. Streams stay chunked.docs/3.http-handling.mdand thesendResponseTSDoc state the rule. Theresult-sendverify journey expects the new header.Scope
sendHeadResponseand stream responses are unchanged.Tradeoffs
res.statusCode+setHeader+end(body)lets Node add the length itself, but measured 5% slower than today:setHeadervalidation cost more than the single write saved.Content-Lengthunconditionally towriteHeadis faster still but sends it on 204 (RFC 9110 forbids that) and throwsERR_HTTP_TRAILER_INVALIDwhen aTrailerheader is set.Blast Radius
Every non-HEAD, non-stream response of every AntelopeJS app changes framing from chunked to
Content-Length. Body, status and other headers are unchanged. Clients handle both framings. Trailers added withrawResponse.addTrailersbut not declared in aTrailerheader are no longer sent, the same as Node's ownres.end(body). AsendResponseargument must now havegetHeaderNames, which anyServerResponsehas; the only caller,@antelopejs/api, passes a real one.Door: two-way.
One-way-door: contractflagssrc/http-result.ts: response framing is part of the HTTP contract, and a revert restores chunked framing with no data to migrate.Verification
Workload.
@antelopejs/api1.7.0 linked to this build (ajs module teston a bench controller), autocannon-c 50 -d 5 -w 2after a 2 s warm-up, server CPU at 100%. 9 rounds, base and change interleaved, server restarted for each run. Evidence level: confirmed through end-to-end benchmark and CPU profile.text("hello")j10(689 B JSON)result(201HTTPResult)The change won all 9 paired rounds on each route, and no ranges overlap. A 140 KB JSON body measured −0.6% (neutral):
JSON.stringifydominates there. Profile:writev31.3% self time → a singlewriteUtf8String. Cumulative impact: about 2.4 µs of CPU saved per small response, so ~12 ms CPU/s at 5,000 req/s.Tests. The new
HTTPResult body framingsuite sends through a real loopback server. On origin/main, "Frames a buffered body with its byte length" fails (content-lengthisnull, response chunked); after, all 13 cases inhttp-result.test.jspass. The trailer and raw-response cases fail on a version that always adds the length (ERR_HTTP_TRAILER_INVALID, duplicate framing).antelopejs-verify journey --plan: 18/18 journeys pass.pnpm run checks: 11 passed, 0 failed.