Skip to content

Commit ac1d2bb

Browse files
committed
More efficient array copying in reader & generated codecs for arrays, see details: https://shipilev.net/blog/2016/arrays-wisdom-ancients/
1 parent e1567c1 commit ac1d2bb

File tree

2 files changed

+4
-15
lines changed

2 files changed

+4
-15
lines changed

core/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonReader.scala

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3173,11 +3173,8 @@ final class JsonReader private[jsoniter_scala](
31733173
}.toChar
31743174

31753175
private[this] def growCharBuf(required: Int): Int = {
3176-
val lim = charBuf.length
3177-
val newLim = Math.max(lim << 1, required)
3178-
val cs = new Array[Char](newLim)
3179-
System.arraycopy(charBuf, 0, cs, 0, lim)
3180-
charBuf = cs
3176+
val newLim = Math.max(charBuf.length << 1, required)
3177+
charBuf = java.util.Arrays.copyOf(charBuf, newLim)
31813178
newLim
31823179
}
31833180

@@ -3250,11 +3247,7 @@ final class JsonReader private[jsoniter_scala](
32503247
if (mark != 2147483647) mark -= minPos
32513248
}
32523249
tail = remaining
3253-
} else if (tail > 0) {
3254-
val bs = new Array[Byte](buf.length << 1)
3255-
System.arraycopy(buf, 0, bs, 0, buf.length)
3256-
buf = bs
3257-
}
3250+
} else if (tail > 0) buf = java.util.Arrays.copyOf(buf, buf.length << 1)
32583251
minPos
32593252
}
32603253

macros/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/macros/JsonCodecMaker.scala

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -576,11 +576,7 @@ object JsonCodecMaker {
576576
genReadArray(
577577
q"""var x = new Array[$tpe1](16)
578578
var i = 0""",
579-
q"""if (i == x.length) {
580-
val x1 = new Array[$tpe1](i << 1)
581-
System.arraycopy(x, 0, x1, 0, i)
582-
x = x1
583-
}
579+
q"""if (i == x.length) x = java.util.Arrays.copyOf(x, i << 1)
584580
x(i) = ${genReadVal(tpe1, nullValue(tpe1), isStringified)}
585581
i += 1""",
586582
q"""if (i == x.length) x

0 commit comments

Comments
 (0)