Skip to content

Commit 28c3116

Browse files
committed
More efficient serialization of BigInt, BigDecimal, and Long values using Scala.js (#1167)
1 parent 58229c6 commit 28c3116

File tree

3 files changed

+15
-17
lines changed
  • jsoniter-scala-core

3 files changed

+15
-17
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,7 +1617,7 @@ final class JsonWriter private[jsoniter_scala](
16171617
pos += digitCount(q)
16181618
count = pos
16191619
} else {
1620-
val q1 = exp / 100000000
1620+
val q1 = (exp >> 8) * 1441151881 >> 49 // divide a small positive long by 100000000
16211621
q = q1.toInt
16221622
pos += digitCount(q)
16231623
count = write8Digits((exp - q1 * 100000000).toInt, pos, buf, ds)
@@ -2206,10 +2206,10 @@ final class JsonWriter private[jsoniter_scala](
22062206
pos + 8
22072207
}
22082208

2209-
private[this] def write18Digits(q0: Long, pos: Int, buf: Array[Byte], ds: Array[Short]): Int = {
2210-
val q1 = q0 / 100000000
2211-
write8Digits((q0 - q1 * 100000000).toInt, {
2212-
val q2 = q1 / 100000000
2209+
private[this] def write18Digits(x: Long, pos: Int, buf: Array[Byte], ds: Array[Short]): Int = {
2210+
val q1 = x / 100000000
2211+
write8Digits((x - q1 * 100000000).toInt, {
2212+
val q2 = (q1 >> 8) * 1441151881 >> 49 // divide a small positive long by 100000000
22132213
write8Digits((q1 - q2 * 100000000).toInt, write2Digits(q2.toInt, pos, buf, ds), buf, ds)
22142214
}, buf, ds)
22152215
}
@@ -2291,7 +2291,7 @@ final class JsonWriter private[jsoniter_scala](
22912291
lastPos += digitCount(q)
22922292
pos = lastPos
22932293
} else {
2294-
val q2 = q1 / 100000000
2294+
val q2 = (q1 >> 8) * 1441151881 >> 49 // divide a small positive long by 100000000
22952295
q = q2.toInt
22962296
lastPos += digitCount(q)
22972297
pos = write8Digits((q1 - q2 * 100000000).toInt, lastPos, buf, ds)
@@ -2572,13 +2572,13 @@ final class JsonWriter private[jsoniter_scala](
25722572
else if (x < 100000000) (9999999 - x >>> 31) + 7
25732573
else (999999999 - x >>> 31) + 9
25742574

2575-
private[this] def writeSignificantFractionDigits(q: Long, p: Int, pl: Int, buf: Array[Byte], ds: Array[Short]): Int = {
2576-
var q0 = q.toInt
2575+
private[this] def writeSignificantFractionDigits(x: Long, p: Int, pl: Int, buf: Array[Byte], ds: Array[Short]): Int = {
2576+
var q0 = x.toInt
25772577
var pos = p
25782578
var posLim = pl
2579-
if (q0 != q) {
2580-
val q1 = (q / 100000000).toInt // divide a positive long by 100000000
2581-
val r1 = (q - q1 * 100000000L).toInt
2579+
if (q0 != x) {
2580+
val q1 = (x / 100000000).toInt // divide a positive long by 100000000
2581+
val r1 = (x - q1 * 100000000L).toInt
25822582
val posm8 = pos - 8
25832583
if (r1 == 0) {
25842584
q0 = q1

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,10 +1608,9 @@ final class JsonWriter private[jsoniter_scala](
16081608
buf(pos) = '-'
16091609
pos += 1
16101610
}
1611-
var q = 0
1611+
var q = hours.toInt
16121612
var lastPos = pos
1613-
if (hours < 100000000) {
1614-
q = hours.toInt
1613+
if (hours == q) {
16151614
lastPos += digitCount(hours)
16161615
pos = lastPos
16171616
} else {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,10 +1608,9 @@ final class JsonWriter private[jsoniter_scala](
16081608
buf(pos) = '-'
16091609
pos += 1
16101610
}
1611-
var q = 0
1611+
var q = hours.toInt
16121612
var lastPos = pos
1613-
if (hours < 100000000) {
1614-
q = hours.toInt
1613+
if (hours == q) {
16151614
lastPos += digitCount(hours)
16161615
pos = lastPos
16171616
} else {

0 commit comments

Comments
 (0)