Skip to content

Commit feb3425

Browse files
committed
More efficient writing of numeric and java.time.* values using Scala.js
1 parent 52add53 commit feb3425

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,13 +1617,15 @@ final class JsonReader private[jsoniter_scala](
16171617
throw new JsonReaderException(new String(charBuf, 0, i), cause, config.throwReaderExceptionWithStackTrace)
16181618
}
16191619

1620+
@inline
16201621
@tailrec
16211622
private[this] def nextByte(pos: Int): Byte =
16221623
if (pos < tail) {
16231624
head = pos + 1
16241625
buf(pos)
16251626
} else nextByte(loadMoreOrError(pos))
16261627

1628+
@inline
16271629
@tailrec
16281630
private[this] def nextByteOrError(t: Byte, pos: Int): Unit =
16291631
if (pos < tail) {
@@ -1658,6 +1660,7 @@ final class JsonReader private[jsoniter_scala](
16581660
b == t || ((b == ' ' || b == '\n' || (b | 0x4) == '\r') && nextToken(pos + 1) == t)
16591661
} else isNextToken(t, loadMoreOrError(pos))
16601662

1663+
@inline
16611664
private[this] def isCurrentToken(t: Byte, pos: Int): Boolean = {
16621665
if (pos == 0) illegalTokenOperation()
16631666
buf(pos - 1) == t
@@ -2499,6 +2502,7 @@ final class JsonReader private[jsoniter_scala](
24992502
// 64-bit unsigned multiplication was adopted from the great Hacker's Delight function
25002503
// (Henry S. Warren, Hacker's Delight, Addison-Wesley, 2nd edition, Fig. 8.2)
25012504
// https://doc.lagout.org/security/Hackers%20Delight.pdf
2505+
@inline
25022506
private[this] def unsignedMultiplyHigh(x: Long, y: Long): Long = {
25032507
val xl = x & 0xFFFFFFFFL
25042508
val xh = x >>> 32
@@ -2684,6 +2688,7 @@ final class JsonReader private[jsoniter_scala](
26842688
}
26852689
}
26862690

2691+
@inline
26872692
private[this] def toBigInt(buf: Array[Byte], p: Int, limit: Int, s: Int): BigInt = {
26882693
val len = limit - p
26892694
if (len < 19) {
@@ -3646,6 +3651,7 @@ final class JsonReader private[jsoniter_scala](
36463651
parseChar(loadMoreOrError(pos))
36473652
}
36483653

3654+
@inline
36493655
private[this] def readEscapedUnicode(pos: Int, buf: Array[Byte]): Char = {
36503656
val ns = nibbles
36513657
val x =
@@ -3927,6 +3933,7 @@ final class JsonReader private[jsoniter_scala](
39273933
i + 2
39283934
}
39293935

3936+
@inline
39303937
private[this] def putHexInt(d: Int, i: Int, charBuf: Array[Char], ds: Array[Char]): Unit = {
39313938
charBuf(i) = ds(d >>> 28)
39323939
charBuf(i + 1) = ds(d >> 24 & 0xF)
@@ -3948,6 +3955,7 @@ final class JsonReader private[jsoniter_scala](
39483955
charBufLen
39493956
}
39503957

3958+
@inline
39513959
private[this] def ensureCharBufCapacity(required: Int): Unit =
39523960
if (charBuf.length < required) growCharBuf(required): Unit
39533961

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,7 @@ final class JsonWriter private[jsoniter_scala](
11141114
}
11151115
}
11161116

1117+
@inline
11171118
private[this] def writeNestedStart(b: Byte): Unit = {
11181119
writeOptionalCommaAndIndentionBeforeKey()
11191120
writeBytes(b)
@@ -1124,6 +1125,7 @@ final class JsonWriter private[jsoniter_scala](
11241125
}
11251126
}
11261127

1128+
@inline
11271129
private[this] def writeNestedEnd(b: Byte): Unit = {
11281130
comma = true
11291131
if (indention != 0) {
@@ -1290,6 +1292,7 @@ final class JsonWriter private[jsoniter_scala](
12901292
count = pos + 1
12911293
}
12921294

1295+
@inline
12931296
private[this] def writeRawBytes(bs: Array[Byte]): Unit = {
12941297
var pos = count
12951298
var step = Math.max(config.preferredBufSize, limit - pos)
@@ -1551,6 +1554,7 @@ final class JsonWriter private[jsoniter_scala](
15511554
count = pos + 1
15521555
}
15531556

1557+
@inline
15541558
private[this] def writeEscapedUnicode(ch: Int, pos: Int, buf: Array[Byte], ds: Array[Short]): Int = {
15551559
buf(pos) = '\\'
15561560
buf(pos + 1) = 'u'
@@ -1563,6 +1567,7 @@ final class JsonWriter private[jsoniter_scala](
15631567
pos + 6
15641568
}
15651569

1570+
@inline
15661571
private[this] def writeEscapedUnicode(b: Byte, pos: Int, buf: Array[Byte], ds: Array[Short]): Int = {
15671572
buf(pos) = '\\'
15681573
buf(pos + 1) = 'u'
@@ -1674,6 +1679,7 @@ final class JsonWriter private[jsoniter_scala](
16741679
writeBigDecimalRemainder(qr(1), scale, blockScale, n - 1, ss)
16751680
}
16761681

1682+
@inline
16771683
private[this] def calculateTenPow18SquareNumber(bitLen: Int): Int = {
16781684
val m = Math.max((bitLen * 0.016723888647998956).toInt - 1, 1) // Math.max((x.bitLength * Math.log(2) / Math.log(1e18)).toInt - 1, 1)
16791685
31 - java.lang.Integer.numberOfLeadingZeros(m)
@@ -1886,6 +1892,7 @@ final class JsonWriter private[jsoniter_scala](
18861892
writeInstant(year, month, day, secsOfDay, x.getNano)
18871893
}
18881894

1895+
@inline
18891896
private[this] def writeInstant(year: Int, month: Int, day: Int, secsOfDay: Int, nano: Int): Unit = {
18901897
var pos = ensureBufCapacity(39) // 39 == Instant.MAX.toString.length + 2
18911898
val buf = this.buf
@@ -2095,6 +2102,7 @@ final class JsonWriter private[jsoniter_scala](
20952102
write2Digits(month, pos + 1, buf, ds)
20962103
}
20972104

2105+
@inline
20982106
private[this] def writeYear(year: Int, pos: Int, buf: Array[Byte], ds: Array[Short]): Int =
20992107
if (year >= 0 && year < 10000) write4Digits(year, pos, buf, ds)
21002108
else writeYearWithSign(year, pos, buf, ds)
@@ -2185,13 +2193,15 @@ final class JsonWriter private[jsoniter_scala](
21852193
}
21862194
}
21872195

2196+
@inline
21882197
private[this] def write2Digits(x: Int, pos: Int, buf: Array[Byte], ds: Array[Short]): Int = {
21892198
val d = ds(x)
21902199
buf(pos) = d.toByte
21912200
buf(pos + 1) = (d >> 8).toByte
21922201
pos + 2
21932202
}
21942203

2204+
@inline
21952205
private[this] def write3Digits(x: Int, pos: Int, buf: Array[Byte], ds: Array[Short]): Int = {
21962206
val q1 = x * 1311 >> 17 // divide a small positive int by 100
21972207
buf(pos) = (q1 + '0').toByte
@@ -2201,6 +2211,7 @@ final class JsonWriter private[jsoniter_scala](
22012211
pos + 3
22022212
}
22032213

2214+
@inline
22042215
private[this] def write4Digits(x: Int, pos: Int, buf: Array[Byte], ds: Array[Short]): Int = {
22052216
val q1 = x * 5243 >> 19 // divide a small positive int by 100
22062217
val d1 = ds(q1)
@@ -2212,6 +2223,7 @@ final class JsonWriter private[jsoniter_scala](
22122223
pos + 4
22132224
}
22142225

2226+
@inline
22152227
private[this] def write8Digits(x: Int, pos: Int, buf: Array[Byte], ds: Array[Short]): Int = {
22162228
val q1 = x / 10000
22172229
val q2 = q1 * 5243 >> 19 // divide a small positive int by 100
@@ -2232,6 +2244,7 @@ final class JsonWriter private[jsoniter_scala](
22322244
pos + 8
22332245
}
22342246

2247+
@inline
22352248
private[this] def write18Digits(x: Long, pos: Int, buf: Array[Byte], ds: Array[Short]): Int = {
22362249
val q1 = ((x >>> 8) * 2.56e-6).toLong // divide a medium positive long by 100000000
22372250
write8Digits((x - q1 * 100000000L).toInt, {
@@ -2286,6 +2299,7 @@ final class JsonWriter private[jsoniter_scala](
22862299
count = pos
22872300
}
22882301

2302+
@inline
22892303
private[this] def writeLong(x: Long): Unit =
22902304
count = writeLong(x, ensureBufCapacity(20), buf) // Long.MinValue.toString.length
22912305

@@ -2456,6 +2470,7 @@ final class JsonWriter private[jsoniter_scala](
24562470
count = pos
24572471
}
24582472

2473+
@inline
24592474
private[this] def rop(g: Long, cp: Int): Int = {
24602475
val x = ((g & 0xFFFFFFFFL) * cp >>> 32) + (g >>> 32) * cp
24612476
(x >>> 31).toInt | -x.toInt >>> 31
@@ -2587,6 +2602,7 @@ final class JsonWriter private[jsoniter_scala](
25872602
count = pos
25882603
}
25892604

2605+
@inline
25902606
private[this] def rop(g1: Long, g0: Long, cp: Long): Long = {
25912607
val x = multiplyHigh(g0, cp) + (g1 * cp >>> 1)
25922608
var y = multiplyHigh(g1, cp)
@@ -2595,6 +2611,7 @@ final class JsonWriter private[jsoniter_scala](
25952611
y
25962612
}
25972613

2614+
@inline
25982615
private[this] def multiplyHigh(x: Long, y: Long): Long = { // Karatsuba technique for two positive longs
25992616
val x2 = x & 0xFFFFFFFFL
26002617
val y2 = y & 0xFFFFFFFFL
@@ -2605,6 +2622,7 @@ final class JsonWriter private[jsoniter_scala](
26052622
((b >>> 32) + (x1 + x2) * (y1 + y2) - b - a >>> 32) + a
26062623
}
26072624

2625+
@inline
26082626
private[this] def digitCount(q0: Long): Int =
26092627
if (q0 >= 1000000000000000L) {
26102628
if (q0 >= 10000000000000000L) 17
@@ -2708,6 +2726,7 @@ final class JsonWriter private[jsoniter_scala](
27082726

27092727
private[this] def illegalNumberError(x: Double): Nothing = encodeError("illegal number: " + x)
27102728

2729+
@inline
27112730
private[this] def ensureBufCapacity(required: Int): Int = {
27122731
val pos = count
27132732
if (pos + required <= limit) pos

0 commit comments

Comments
 (0)