Skip to content

Commit 0b89501

Browse files
committed
Use only pow of 2 values for buffer sizes + more efficient printing of hex dumps
1 parent 741af95 commit 0b89501

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3175,13 +3175,10 @@ final class JsonReader private[jsoniter_scala](
31753175
charBuf(i + 1) = toHexDigit(b)
31763176
}
31773177

3178-
private[this] def toHexDigit(n: Int): Char = {
3179-
val nibble = n & 15
3180-
(((9 - nibble) >> 31) & 39) + (nibble + 48) // branchless conversion of nibble to hex digit
3181-
}.toChar
3178+
private[this] def toHexDigit(n: Int): Char = hexDigits(n & 15)
31823179

31833180
private[this] def growCharBuf(required: Int): Int = {
3184-
val newLim = Math.max(charBuf.length << 1, required)
3181+
val newLim = Integer.highestOneBit(charBuf.length | required) << 1
31853182
charBuf = java.util.Arrays.copyOf(charBuf, newLim)
31863183
newLim
31873184
}
@@ -3306,6 +3303,8 @@ object JsonReader {
33063303
ns('f') = 15
33073304
ns
33083305
}
3306+
private final val hexDigits: Array[Char] =
3307+
Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f')
33093308
private final val dumpHeader: Array[Char] = {
33103309
"\n +-------------------------------------------------+" +
33113310
"\n | 0 1 2 3 4 5 6 7 8 9 a b c d e f |"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,7 @@ final class JsonWriter private[jsoniter_scala](
12351235
}
12361236

12371237
private[this] def growBuf(required: Int): Unit =
1238-
if (isBufGrowingAllowed) buf = java.util.Arrays.copyOf(buf, Math.max(buf.length << 1, required))
1238+
if (isBufGrowingAllowed) buf = java.util.Arrays.copyOf(buf, Integer.highestOneBit(buf.length | required) << 1)
12391239
else throw new ArrayIndexOutOfBoundsException("`buf` length exceeded")
12401240

12411241
private[this] def freeTooLongBuf(): Unit =

0 commit comments

Comments
 (0)