Skip to content

Commit a0224f3

Browse files
authored
Discard read bytes when accumulating continuation frames (#444)
Motivation: When accumulating sequences of CONTINUATION frames, each frame is parsed from a buffer. These bytes are read when the CONTINUATION frame is parsed, but if more CONTINUATION frames follow then the buffer isn't reset. This means that long sequences of CONTINUATION frames can result in a larger than necessary buffer where most of the contents have already been read. Modifications: - Discard the bytes of the accumulation buffer when transitioning back to AccumulatingHeaderBlockFragmentsParserState if more than half of the buffer has been read. Result: Lower memory footprint when parsing sequences of CONTINUATION frames.
1 parent 6af2cf6 commit a0224f3

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Sources/NIOHTTP2/HTTP2FrameParser.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,19 @@ struct HTTP2FrameDecoder {
537537

538538
// we have collected enough bytes: is this the last CONTINUATION frame?
539539
guard self.continuationHeader.flags.contains(.endHeaders) else {
540+
// When accumulating a sequence of CONTINUATION frames the buffer will continue to
541+
// grow as bytes are appended to it and the reader index will continue to advance
542+
// as frames are read. However this allows for an ever increasing buffer where only
543+
// the unread bytes are useful. The read bytes should be periodically discarded.
544+
//
545+
// As a basic heuristic is to discard the read bytes when more than 1KB has been
546+
// read, and that 1KB accounts for more than half of the capacity.
547+
let readerIndex = self.continuationPayload.readerIndex
548+
let capacity = self.continuationPayload.capacity
549+
if readerIndex > 1024 && readerIndex > (capacity / 2) {
550+
self.continuationPayload.discardReadBytes()
551+
}
552+
540553
// nope, switch back to accumulating fragments
541554
return .accumulateContinuationHeader(
542555
AccumulatingHeaderBlockFragmentsParserState(

0 commit comments

Comments
 (0)