Skip to content

Commit 5ecda59

Browse files
committed
Faster parsing of small JSON arrays to Vector and IndexSeq collections
1 parent ad7f28b commit 5ecda59

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

jsoniter-scala-macros/shared/src/main/scala-2/com/github/plokhotnyuk/jsoniter_scala/macros/JsonCodecMaker.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,6 +1828,10 @@ object JsonCodecMaker {
18281828
val tpe1 = typeArg1(tpe)
18291829
genReadArray(q"{ val x = new _root_.scala.collection.mutable.ListBuffer[$tpe1] }",
18301830
genReadValForGrowable(tpe1 :: types, isStringified), q"x.toList")
1831+
} else if (tpe <:< typeOf[Vector[_]] || tpe.typeSymbol == typeOf[IndexedSeq[_]].typeSymbol) withDecoderFor(methodKey, default) {
1832+
val tpe1 = typeArg1(tpe)
1833+
genReadArray(q"{ val x = new _root_.scala.collection.immutable.VectorBuilder[$tpe1] }",
1834+
genReadValForGrowable(tpe1 :: types, isStringified), q"x.result()")
18311835
} else if (tpe <:< typeOf[mutable.Iterable[_] with mutable.Builder[_, _]] &&
18321836
!(tpe <:< typeOf[mutable.ArrayStack[_]])) withDecoderFor(methodKey, default) { //ArrayStack uses 'push' for '+=' in Scala 2.12.x
18331837
val tpe1 = typeArg1(tpe)

jsoniter-scala-macros/shared/src/main/scala-3/com/github/plokhotnyuk/jsoniter_scala/macros/JsonCodecMaker.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,6 +2526,13 @@ object JsonCodecMaker {
25262526
if ($tDefault.isEmpty) $tDefault
25272527
else new mutable.ListBuffer[t1]
25282528
}, x => genReadValForGrowable(tpe1 :: types, isStringified, x, in), tDefault, x => x, in).asExprOf[T]
2529+
} else if (tpe <:< TypeRepr.of[Vector[_]] || tpe.typeSymbol == TypeRepr.of[IndexedSeq[_]].typeSymbol) withDecoderFor(methodKey, default, in) { (in, default) =>
2530+
val tpe1 = typeArg1(tpe)
2531+
tpe1.asType match
2532+
case '[t1] =>
2533+
genReadCollection('{ new immutable.VectorBuilder[t1] },
2534+
x => genReadValForGrowable(tpe1 :: types, isStringified, x, in),
2535+
default, x => '{ $x.result() }, in).asExprOf[T]
25292536
} else if (tpe <:< TypeRepr.of[mutable.Iterable[_] with mutable.Growable[_]]) withDecoderFor(methodKey, default, in) { (in, default) =>
25302537
val tpe1 = typeArg1(tpe)
25312538
tpe1.asType match

0 commit comments

Comments
 (0)