Skip to content

Commit b57c381

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

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
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: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,20 +2503,13 @@ object JsonCodecMaker {
25032503
}
25042504
}.asExprOf[T]
25052505
}
2506-
} else if (tpe <:< TypeRepr.of[List[_]]) withDecoderFor(methodKey, default, in) { (in, default) =>
2506+
} else if (tpe <:< TypeRepr.of[List[_]] || tpe.typeSymbol == TypeRepr.of[Seq[_]].typeSymbol) withDecoderFor(methodKey, default, in) { (in, default) =>
25072507
val tpe1 = typeArg1(tpe)
25082508
tpe1.asType match
25092509
case '[t1] =>
25102510
genReadCollection('{ new mutable.ListBuffer[t1] },
25112511
x => genReadValForGrowable(tpe1 :: types, isStringified, x, in),
2512-
default.asExprOf[List[t1]], x => '{ $x.toList }, in).asExprOf[T]
2513-
} else if (tpe.typeSymbol == TypeRepr.of[Seq[_]].typeSymbol) withDecoderFor(methodKey, default, in) { (in, default) =>
2514-
val tpe1 = typeArg1(tpe)
2515-
tpe1.asType match
2516-
case '[t1] =>
2517-
genReadCollection('{ new mutable.ListBuffer[t1] },
2518-
x => genReadValForGrowable(tpe1 :: types, isStringified, x, in),
2519-
default.asExprOf[Seq[t1]], x => '{ $x.toList }, in).asExprOf[T]
2512+
default, x => '{ $x.toList }, in).asExprOf[T]
25202513
} else if (tpe <:< TypeRepr.of[mutable.ListBuffer[_]]) withDecoderFor(methodKey, default, in) { (in, default) =>
25212514
val tpe1 = typeArg1(tpe)
25222515
tpe1.asType match
@@ -2526,6 +2519,13 @@ object JsonCodecMaker {
25262519
if ($tDefault.isEmpty) $tDefault
25272520
else new mutable.ListBuffer[t1]
25282521
}, x => genReadValForGrowable(tpe1 :: types, isStringified, x, in), tDefault, x => x, in).asExprOf[T]
2522+
} else if (tpe <:< TypeRepr.of[Vector[_]] || tpe.typeSymbol == TypeRepr.of[IndexedSeq[_]].typeSymbol) withDecoderFor(methodKey, default, in) { (in, default) =>
2523+
val tpe1 = typeArg1(tpe)
2524+
tpe1.asType match
2525+
case '[t1] =>
2526+
genReadCollection('{ new immutable.VectorBuilder[t1] },
2527+
x => genReadValForGrowable(tpe1 :: types, isStringified, x, in),
2528+
default, x => '{ $x.result() }, in).asExprOf[T]
25292529
} else if (tpe <:< TypeRepr.of[mutable.Iterable[_] with mutable.Growable[_]]) withDecoderFor(methodKey, default, in) { (in, default) =>
25302530
val tpe1 = typeArg1(tpe)
25312531
tpe1.asType match

0 commit comments

Comments
 (0)