From 751190f084f55dde8e940548a5fb4c2cda88ea4c Mon Sep 17 00:00:00 2001 From: Johannes Rudolph Date: Mon, 8 Jun 2015 15:04:19 +0200 Subject: [PATCH] =htc compact outgoing ByteStrings in parsers to avoid referencing more memory than necessary This was discussed previously in the context of spray where we came to the conclusion that it makes sense to deliver compact ByteStrings on API boundaries to avoid to carry around unused parts of ByteStrings. --- .../akka/http/impl/engine/parsing/HttpMessageParser.scala | 6 +++--- .../akka/http/impl/engine/parsing/HttpResponseParser.scala | 2 +- .../scala/akka/http/impl/engine/ws/FrameEventParser.scala | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/akka-http-core/src/main/scala/akka/http/impl/engine/parsing/HttpMessageParser.scala b/akka-http-core/src/main/scala/akka/http/impl/engine/parsing/HttpMessageParser.scala index b5bd371965..b2d440a172 100644 --- a/akka-http-core/src/main/scala/akka/http/impl/engine/parsing/HttpMessageParser.scala +++ b/akka-http-core/src/main/scala/akka/http/impl/engine/parsing/HttpMessageParser.scala @@ -170,11 +170,11 @@ private[http] abstract class HttpMessageParser[Output >: MessageOutput <: Parser val remainingInputBytes = input.length - bodyStart if (remainingInputBytes > 0) { if (remainingInputBytes < remainingBodyBytes) { - emit(EntityPart(input drop bodyStart)) + emit(EntityPart(input.drop(bodyStart).compact)) continue(parseFixedLengthBody(remainingBodyBytes - remainingInputBytes, isLastMessage)) } else { val offset = bodyStart + remainingBodyBytes.toInt - emit(EntityPart(input.slice(bodyStart, offset))) + emit(EntityPart(input.slice(bodyStart, offset).compact)) emit(MessageEnd) setCompletionHandling(CompletionOk) if (isLastMessage) terminate() @@ -211,7 +211,7 @@ private[http] abstract class HttpMessageParser[Output >: MessageOutput <: Parser if (chunkSize > 0) { val chunkBodyEnd = cursor + chunkSize def result(terminatorLen: Int) = { - emit(EntityChunk(HttpEntity.Chunk(input.slice(cursor, chunkBodyEnd), extension))) + emit(EntityChunk(HttpEntity.Chunk(input.slice(cursor, chunkBodyEnd).compact, extension))) Trampoline(_ ⇒ parseChunk(input, chunkBodyEnd + terminatorLen, isLastMessage)) } byteChar(input, chunkBodyEnd) match { diff --git a/akka-http-core/src/main/scala/akka/http/impl/engine/parsing/HttpResponseParser.scala b/akka-http-core/src/main/scala/akka/http/impl/engine/parsing/HttpResponseParser.scala index 7922a3e1e8..4751f68123 100644 --- a/akka-http-core/src/main/scala/akka/http/impl/engine/parsing/HttpResponseParser.scala +++ b/akka-http-core/src/main/scala/akka/http/impl/engine/parsing/HttpResponseParser.scala @@ -132,7 +132,7 @@ private[http] class HttpResponseParser(_settings: ParserSettings, _headerParser: // currently we do not check for `settings.maxContentLength` overflow def parseToCloseBody(input: ByteString, bodyStart: Int): StateResult = { if (input.length > bodyStart) - emit(EntityPart(input drop bodyStart)) + emit(EntityPart(input.drop(bodyStart).compact)) continue(parseToCloseBody) } } \ No newline at end of file diff --git a/akka-http-core/src/main/scala/akka/http/impl/engine/ws/FrameEventParser.scala b/akka-http-core/src/main/scala/akka/http/impl/engine/ws/FrameEventParser.scala index 86ae74d01d..927b2a3a67 100644 --- a/akka-http-core/src/main/scala/akka/http/impl/engine/ws/FrameEventParser.scala +++ b/akka-http-core/src/main/scala/akka/http/impl/engine/ws/FrameEventParser.scala @@ -87,7 +87,7 @@ private[http] class FrameEventParser extends ByteStringParserStage[FrameEvent] { if (thisFrameData.length == length) ReadFrameHeader else readData(length - thisFrameData.length) - pushAndBecomeWithRemaining(FrameStart(header, thisFrameData), nextState, remaining, ctx) + pushAndBecomeWithRemaining(FrameStart(header, thisFrameData.compact), nextState, remaining, ctx) } } @@ -103,7 +103,7 @@ private[http] class FrameEventParser extends ByteStringParserStage[FrameEvent] { val frameData = elem.take(remaining.toInt) val remainingData = elem.drop(remaining.toInt) - pushAndBecomeWithRemaining(FrameData(frameData, lastPart = true), ReadFrameHeader, remainingData, ctx) + pushAndBecomeWithRemaining(FrameData(frameData.compact, lastPart = true), ReadFrameHeader, remainingData, ctx) } }