Skip to content

Commit 5f0b5a5

Browse files
committed
More efficient parsing of BigDecimal values
1 parent f2d3867 commit 5f0b5a5

File tree

3 files changed

+36
-19
lines changed
  • jsoniter-scala-core

3 files changed

+36
-19
lines changed

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,7 +2619,7 @@ final class JsonReader private[jsoniter_scala](
26192619
val sb = b
26202620
if (b == '-' || b == '+') b = nextByte(head)
26212621
if (b < '0' || b > '9') numberError()
2622-
var exp = (b - '0').toLong
2622+
scale = '0' - b
26232623
pos = head
26242624
buf = this.buf
26252625
while ((pos < tail || {
@@ -2630,13 +2630,14 @@ final class JsonReader private[jsoniter_scala](
26302630
b = buf(pos)
26312631
b >= '0' && b <= '9'
26322632
}) {
2633-
exp = exp * 10 + (b - '0')
2634-
if (exp > 2147483648L) numberError(pos)
2633+
if (scale < -214748364 || {
2634+
scale = scale * 10 + ('0' - b)
2635+
scale > 0
2636+
}) numberError(pos)
26352637
pos += 1
26362638
}
2637-
if (sb == '-') scale = exp.toInt
2638-
else if (exp == 2147483648L) numberError(pos - 1)
2639-
else scale = -exp.toInt
2639+
if (sb == '-') scale = -scale
2640+
if (scale == -2147483648) numberError(pos - 1)
26402641
}
26412642
head = pos
26422643
if (mark == 0) from -= newMark
@@ -2646,7 +2647,21 @@ final class JsonReader private[jsoniter_scala](
26462647
val limit = from + digits + 1
26472648
val fracPos = limit - fracLen
26482649
val fracLimit = fracPos - 1
2649-
if (digits < 19) {
2650+
if (digits < 10) {
2651+
var x = buf(from) - '0'
2652+
from += 1
2653+
while (from < fracLimit) {
2654+
x = x * 10 + (buf(from) - '0')
2655+
from += 1
2656+
}
2657+
from += 1
2658+
while (from < limit) {
2659+
x = x * 10 + (buf(from) - '0')
2660+
from += 1
2661+
}
2662+
if (isNeg) x = -x
2663+
java.math.BigDecimal.valueOf(x.toLong, scale + fracLen)
2664+
} else if (digits < 19) {
26502665
var x = (buf(from) - '0').toLong
26512666
from += 1
26522667
while (from < fracLimit) {

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,7 +2632,7 @@ final class JsonReader private[jsoniter_scala](
26322632
val sb = b
26332633
if (b == '-' || b == '+') b = nextByte(head)
26342634
if (b < '0' || b > '9') numberError()
2635-
var exp = (b - '0').toLong
2635+
scale = '0' - b
26362636
pos = head
26372637
buf = this.buf
26382638
while ((pos < tail || {
@@ -2643,13 +2643,14 @@ final class JsonReader private[jsoniter_scala](
26432643
b = buf(pos)
26442644
b >= '0' && b <= '9'
26452645
}) {
2646-
exp = exp * 10 + (b - '0')
2647-
if (exp > 2147483648L) numberError(pos)
2646+
if (scale < -214748364 || {
2647+
scale = scale * 10 + ('0' - b)
2648+
scale > 0
2649+
}) numberError(pos)
26482650
pos += 1
26492651
}
2650-
if (sb == '-') scale = exp.toInt
2651-
else if (exp == 2147483648L) numberError(pos - 1)
2652-
else scale = -exp.toInt
2652+
if (sb == '-') scale = -scale
2653+
if (scale == -2147483648) numberError(pos - 1)
26532654
}
26542655
head = pos
26552656
if (mark == 0) from -= newMark

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,7 +2629,7 @@ final class JsonReader private[jsoniter_scala](
26292629
val sb = b
26302630
if (b == '-' || b == '+') b = nextByte(head)
26312631
if (b < '0' || b > '9') numberError()
2632-
var exp = (b - '0').toLong
2632+
scale = '0' - b
26332633
pos = head
26342634
buf = this.buf
26352635
while ((pos < tail || {
@@ -2640,13 +2640,14 @@ final class JsonReader private[jsoniter_scala](
26402640
b = buf(pos)
26412641
b >= '0' && b <= '9'
26422642
}) {
2643-
exp = exp * 10 + (b - '0')
2644-
if (exp > 2147483648L) numberError(pos)
2643+
if (scale < -214748364 || {
2644+
scale = scale * 10 + ('0' - b)
2645+
scale > 0
2646+
}) numberError(pos)
26452647
pos += 1
26462648
}
2647-
if (sb == '-') scale = exp.toInt
2648-
else if (exp == 2147483648L) numberError(pos - 1)
2649-
else scale = -exp.toInt
2649+
if (sb == '-') scale = -scale
2650+
if (scale == -2147483648) numberError(pos - 1)
26502651
}
26512652
head = pos
26522653
if (mark == 0) from -= newMark

0 commit comments

Comments
 (0)