Skip to content

Commit

Permalink
Use only pow of 2 values for buffer sizes + more efficient printing o…
Browse files Browse the repository at this point in the history
…f hex dumps
  • Loading branch information
plokhotnyuk committed Apr 1, 2018
1 parent 741af95 commit 0b89501
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3175,13 +3175,10 @@ final class JsonReader private[jsoniter_scala](
charBuf(i + 1) = toHexDigit(b)
}

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

private[this] def growCharBuf(required: Int): Int = {
val newLim = Math.max(charBuf.length << 1, required)
val newLim = Integer.highestOneBit(charBuf.length | required) << 1
charBuf = java.util.Arrays.copyOf(charBuf, newLim)
newLim
}
Expand Down Expand Up @@ -3306,6 +3303,8 @@ object JsonReader {
ns('f') = 15
ns
}
private final val hexDigits: Array[Char] =
Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f')
private final val dumpHeader: Array[Char] = {
"\n +-------------------------------------------------+" +
"\n | 0 1 2 3 4 5 6 7 8 9 a b c d e f |"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ final class JsonWriter private[jsoniter_scala](
}

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

private[this] def freeTooLongBuf(): Unit =
Expand Down

0 comments on commit 0b89501

Please sign in to comment.