Skip to content

Commit a2c81a6

Browse files
committed
More efficient parsing and serialization of UUIDs and hex escaped characters of strings
1 parent b9268dc commit a2c81a6

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3031,13 +3031,9 @@ final class JsonReader private[jsoniter_scala](
30313031
((fromHexDigit(pos) << 12) + (fromHexDigit(pos + 1) << 8) + (fromHexDigit(pos + 2) << 4) + fromHexDigit(pos + 3)).toChar
30323032

30333033
private[this] def fromHexDigit(pos: Int): Int = {
3034-
val b = buf(pos)
3035-
if (b >= '0' && b <= '9') b - '0'
3036-
else {
3037-
val b1 = b | 0x20 // 0x20 == 'A' ^ 'a' or 'B' ^ 'b', etc.
3038-
if (b1 >= 'a' && b1 <= 'f') b1 - 0x57 // 0x57 == 'a' - 10
3039-
else decodeError("expected hex digit", pos)
3040-
}
3034+
val n = nibbles(buf(pos))
3035+
if (n < 0) decodeError("expected hex digit", pos)
3036+
n
30413037
}
30423038

30433039
private[this] def illegalEscapeSequenceError(pos: Int): Nothing = decodeError("illegal escape sequence", pos)
@@ -3282,6 +3278,32 @@ object JsonReader {
32823278
1e+12, 1e+13, 1e+14, 1e+15, 1e+16, 1e+17, 1e+18, 1e+19, 1e+20, 1e+21, 1e+22)
32833279
private final val nanoMultiplier: Array[Int] =
32843280
Array(1000000000, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1)
3281+
private final val nibbles: Array[Byte] = {
3282+
val ns = Array.fill[Byte](256)(-1)
3283+
ns('0') = 0
3284+
ns('1') = 1
3285+
ns('2') = 2
3286+
ns('3') = 3
3287+
ns('4') = 4
3288+
ns('5') = 5
3289+
ns('6') = 6
3290+
ns('7') = 7
3291+
ns('8') = 8
3292+
ns('9') = 9
3293+
ns('A') = 10
3294+
ns('B') = 11
3295+
ns('C') = 12
3296+
ns('D') = 13
3297+
ns('E') = 14
3298+
ns('F') = 15
3299+
ns('a') = 10
3300+
ns('b') = 11
3301+
ns('c') = 12
3302+
ns('d') = 13
3303+
ns('e') = 14
3304+
ns('f') = 15
3305+
ns
3306+
}
32853307
private final val dumpHeader: Array[Char] = {
32863308
"\n +-------------------------------------------------+" +
32873309
"\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: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -700,10 +700,7 @@ final class JsonWriter private[jsoniter_scala](
700700
pos + 6
701701
}
702702

703-
private[this] def toHexDigit(n: Int): Byte = {
704-
val nibble = n & 15
705-
(((9 - nibble) >> 31) & 39) + (nibble + 48) // branchless conversion of nibble to hex digit
706-
}.toByte
703+
private[this] def toHexDigit(n: Int): Byte = hexDigits(n & 15)
707704

708705
private[this] def illegalSurrogateError(): Nothing = encodeError("illegal char sequence of surrogate pair")
709706

@@ -1262,6 +1259,8 @@ object JsonWriter {
12621259
}(breakOut)
12631260
private final val digits: Array[Short] =
12641261
(0 to 99).map(i => (((i / 10 + '0') << 8) + (i % 10 + '0')).toShort)(breakOut)
1262+
private final val hexDigits: Array[Byte] =
1263+
Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f')
12651264
private final val minIntBytes: Array[Byte] = "-2147483648".getBytes
12661265
private final val minLongBytes: Array[Byte] = "-9223372036854775808".getBytes
12671266
private final val maxZonedDateTimeLength: Int = {

0 commit comments

Comments
 (0)