Skip to content

Commit 0dda95c

Browse files
authored
Fix CoW in HTTPResponseAggregator (#345)
Motivation: HTTPResponseAggregator attempts to build a single, complete response object. This necessarily means it loads the entire response payload into memory. It wants to provide this payload as a single contiguous buffer of data, and it does so by aggregating the data into a single contiguous buffer as it goes. Because ByteBuffer does exponential reallocation, the cost of doing this should be amortised constant-time, even though we do have to copy some data sometimes. However, if this operation triggers a copy-on-write then the operation will become quadratic. For large buffers this will rapidly come to dominate the runtime. Unfortunately in at least Swift 5.3 Swift cannot safely see that during the body stanza the state variable is dead. Swift is not necessarily wrong about this: there's a cross-module call to ByteBuffer.writeBuffer in place and Swift cannot easily prove that that call will not lead to a re-entrant access of the `HTTPResponseAggregator` object. For this reason, during the call to `didReceiveBodyPart` there will be two copies of the body buffer alive, and so the write will CoW. This quadratic behaviour is a nasty performance trap that can become highly apparent even at quite small body sizes. Modifications: While Swift can't prove that the `self.state` variable is dead, we can! To that end, we temporarily set it to a different value that does not store the buffer in question. This will force Swift to drop the ref on the buffer, making it uniquely owned and avoiding the CoW. Sadly, it's extremely difficult to test for "does not CoW", so this patch does not currently come with any tests. I have experimentally verified the behaviour. Result: No copy-on-write in the HTTPResponseAggregator during body aggregation.
1 parent 5d9b784 commit 0dda95c

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

Diff for: Sources/AsyncHTTPClient/HTTPHandler.swift

+5
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,11 @@ public class ResponseAccumulator: HTTPClientResponseDelegate {
380380
case .head(let head):
381381
self.state = .body(head, part)
382382
case .body(let head, var body):
383+
// The compiler can't prove that `self.state` is dead here (and it kinda isn't, there's
384+
// a cross-module call in the way) so we need to drop the original reference to `body` in
385+
// `self.state` or we'll get a CoW. To fix that we temporarily set the state to `.end` (which
386+
// has no associated data). We'll fix it at the bottom of this block.
387+
self.state = .end
383388
var part = part
384389
body.writeBuffer(&part)
385390
self.state = .body(head, body)

0 commit comments

Comments
 (0)