Skip to content

Commit

Permalink
Avoid walking twice through accumulated values
Browse files Browse the repository at this point in the history
  • Loading branch information
satabin committed Jun 26, 2023
1 parent 6df4578 commit f0d3c5a
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions json/src/main/scala/fs2/data/json/internal/ValueParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ package data
package json
package internals

import ast._
import scala.collection.mutable.ListBuffer

import scala.collection.immutable.VectorBuilder
import ast._

private[json] object ValueParser {

private def pullArray[F[_], Json](chunk: Chunk[Token], idx: Int, rest: Stream[F, Token], acc: VectorBuilder[Json])(
private def pullArray[F[_], Json](chunk: Chunk[Token], idx: Int, rest: Stream[F, Token], acc: ListBuffer[Json])(
implicit
F: RaiseThrowable[F],
builder: Builder[Json]): Pull[F, Nothing, Result[F, Json]] =
Expand All @@ -37,7 +37,7 @@ private[json] object ValueParser {
} else {
chunk(idx) match {
case Token.EndArray =>
Pull.pure(Some((chunk, idx + 1, rest, builder.makeArray(acc.result()))))
Pull.pure(Some((chunk, idx + 1, rest, builder.makeArray(acc))))
case _ =>
Pull.suspend(pullValue(chunk, idx, rest).flatMap {
case Some((chunk, idx, rest, json)) => pullArray(chunk, idx, rest, acc += json)
Expand All @@ -49,7 +49,7 @@ private[json] object ValueParser {
private def pullObject[F[_], Json](chunk: Chunk[Token],
idx: Int,
rest: Stream[F, Token],
acc: VectorBuilder[(String, Json)])(implicit
acc: ListBuffer[(String, Json)])(implicit
F: RaiseThrowable[F],
builder: Builder[Json]): Pull[F, Nothing, Result[F, Json]] =
if (idx >= chunk.size) {
Expand All @@ -60,7 +60,7 @@ private[json] object ValueParser {
} else {
chunk(idx) match {
case Token.EndObject =>
Pull.pure(Some((chunk, idx + 1, rest, builder.makeObject(acc.result()))))
Pull.pure(Some((chunk, idx + 1, rest, builder.makeObject(acc))))
case Token.Key(key) =>
pullValue(chunk, idx + 1, rest).flatMap {
case Some((chunk, idx, rest, json)) => pullObject(chunk, idx, rest, acc += (key -> json))
Expand All @@ -86,8 +86,8 @@ private[json] object ValueParser {
case Token.NullValue => Pull.pure(Some((chunk, idx + 1, rest, builder.makeNull)))
case Token.StringValue(s) => Pull.pure(Some((chunk, idx + 1, rest, builder.makeString(s))))
case Token.NumberValue(s) => Pull.pure(Some((chunk, idx + 1, rest, builder.makeNumber(s))))
case Token.StartArray => pullArray(chunk, idx + 1, rest, new VectorBuilder)
case Token.StartObject => pullObject(chunk, idx + 1, rest, new VectorBuilder)
case Token.StartArray => pullArray(chunk, idx + 1, rest, new ListBuffer)
case Token.StartObject => pullObject(chunk, idx + 1, rest, new ListBuffer)
case token => Pull.raiseError[F](new JsonException(s"malformed json (unexpected $token)"))
}
}
Expand Down

0 comments on commit f0d3c5a

Please sign in to comment.