Skip to content

Commit 8a6bca3

Browse files
Fix to remove needless allocations (performance) (plokhotnyuk#1335)
Before this fix, every decode operation would reallocate the `buf` Array. This is because at the start of read, pos=head=0. Then, because we've grown the buffer, at the end of the read operation we'd allocate the array AGAIN to shrink it back because it is now larger than the preferred buffer size config! The problem started with the commit: f0ebbe1 Code clean up (Andriy Plokhotnyuk) 2020-01-02| ```diff - newPos - } else { - if (tail > 0) buf = java.util.Arrays.copyOf(buf, buf.length << 1) - pos - } + } else buf = java.util.Arrays.copyOf(buf, buf.length << 1) ``` This change restores the original check for the `tail > 0` which is used to indicate that the buffer is currently empty (start of the read operation) and we should not grow it.
1 parent 10e8213 commit 8a6bca3

File tree

2 files changed

+8
-4
lines changed
  • jsoniter-scala-core
    • js/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core
    • jvm-native/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core

2 files changed

+8
-4
lines changed

jsoniter-scala-core/js/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonReader.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4051,7 +4051,9 @@ final class JsonReader private[jsoniter_scala](
40514051
if (mark > 0) mark = 0
40524052
tail = remaining
40534053
head = newPos
4054-
} else growBuf()
4054+
} else {
4055+
if (tail > 0) growBuf()
4056+
}
40554057
var len = buf.length - tail
40564058
if (bbuf ne null) {
40574059
len = Math.min(bbuf.remaining, len)
@@ -4490,4 +4492,4 @@ private class Key {
44904492
private def fromIndex: Int = from
44914493

44924494
private def toIndex: Int = to
4493-
}
4495+
}

jsoniter-scala-core/jvm-native/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonReader.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4506,7 +4506,9 @@ final class JsonReader private[jsoniter_scala](
45064506
if (mark > 0) mark = 0
45074507
tail = remaining
45084508
head = newPos
4509-
} else growBuf()
4509+
} else {
4510+
if (tail > 0) growBuf()
4511+
}
45104512
var len = buf.length - tail
45114513
if (bbuf ne null) {
45124514
len = Math.min(bbuf.remaining, len)
@@ -4943,4 +4945,4 @@ private class Key {
49434945
private def fromIndex: Int = from
49444946

49454947
private def toIndex: Int = to
4946-
}
4948+
}

0 commit comments

Comments
 (0)