Skip to content

Commit f755a21

Browse files
committed
#37 Fixed performance problem with BigInt
1 parent efabb1f commit f755a21

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

shared/src/main/scala-2.10/scalajson.ast/package.scala

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ package object ast {
428428
if (!(digit == 0 && (decimalFlag || eFlag))) {
429429

430430
if (!negativeEFlag) {
431-
if (digitLength == maxLengthConstant -1) {
431+
if (digitLength == maxLengthConstant - 1) {
432432
var result2: Int = result
433433
result2 *= radix
434434

@@ -554,7 +554,7 @@ package object ast {
554554
if (!(digit == 0 && (decimalFlag || eFlag))) {
555555

556556
if (!negativeEFlag) {
557-
if (digitLength == maxLengthConstant -1) {
557+
if (digitLength == maxLengthConstant - 1) {
558558
var result2: Long = result
559559
result2 *= radix
560560

@@ -602,6 +602,7 @@ package object ast {
602602

603603
private[ast] def toBigInt(value: String): Option[BigInt] = {
604604
var decimalFlag = false
605+
@inline def maxLengthConstant: Int = 10
605606
var result: Int = 0 // assume that values are initially small when we convert to bigInt
606607
var resultBigInt: BigInt = null
607608
var negative = false
@@ -662,10 +663,13 @@ package object ast {
662663
if (!(digit == 0 && (decimalFlag || eFlag))) {
663664

664665
if (!negativeEFlag) {
665-
var result2: Int = result
666-
result2 *= radix
667-
if (!negative && result2 < 0 || negative && result2 > 0) {
668-
resultBigInt = BigInt(result)
666+
if (digitLength == maxLengthConstant - 1) {
667+
var result2: Long = result
668+
result2 *= radix
669+
670+
if (!negative && result2 < 0 || negative && result2 > 0) {
671+
resultBigInt = BigInt(result)
672+
}
669673
}
670674

671675
if (resultBigInt == null) {
@@ -697,7 +701,9 @@ package object ast {
697701
private[ast] def toDouble(value: String): Option[Double] = {
698702
try {
699703
val asDouble = value.toDouble
700-
if (BigDecimal(value, MathContext.UNLIMITED) == BigDecimal(asDouble, MathContext.UNLIMITED))
704+
if (BigDecimal(value, MathContext.UNLIMITED) == BigDecimal(
705+
asDouble,
706+
MathContext.UNLIMITED))
701707
Some(asDouble)
702708
else
703709
None

shared/src/main/scala/scalajson/ast/package.scala

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ package object ast {
428428
if (!(digit == 0 && (decimalFlag || eFlag))) {
429429

430430
if (!negativeEFlag) {
431-
if (digitLength == maxLengthConstant -1) {
431+
if (digitLength == maxLengthConstant - 1) {
432432
var result2: Int = result
433433
result2 *= radix
434434

@@ -554,7 +554,7 @@ package object ast {
554554
if (!(digit == 0 && (decimalFlag || eFlag))) {
555555

556556
if (!negativeEFlag) {
557-
if (digitLength == maxLengthConstant -1) {
557+
if (digitLength == maxLengthConstant - 1) {
558558
var result2: Long = result
559559
result2 *= radix
560560

@@ -602,6 +602,7 @@ package object ast {
602602

603603
private[ast] def toBigInt(value: String): Option[BigInt] = {
604604
var decimalFlag = false
605+
@inline def maxLengthConstant: Int = 10
605606
var result: Int = 0 // assume that values are initially small when we convert to bigInt
606607
var resultBigInt: BigInt = null
607608
var negative = false
@@ -662,10 +663,13 @@ package object ast {
662663
if (!(digit == 0 && (decimalFlag || eFlag))) {
663664

664665
if (!negativeEFlag) {
665-
var result2: Int = result
666-
result2 *= radix
667-
if (!negative && result2 < 0 || negative && result2 > 0) {
668-
resultBigInt = BigInt(result)
666+
if (digitLength == maxLengthConstant - 1) {
667+
var result2: Long = result
668+
result2 *= radix
669+
670+
if (!negative && result2 < 0 || negative && result2 > 0) {
671+
resultBigInt = BigInt(result)
672+
}
669673
}
670674

671675
if (resultBigInt == null) {
@@ -697,7 +701,9 @@ package object ast {
697701
private[ast] def toDouble(value: String): Option[Double] = {
698702
try {
699703
val asDouble = value.toDouble
700-
if (BigDecimal(value, MathContext.UNLIMITED) == BigDecimal(asDouble, MathContext.UNLIMITED))
704+
if (BigDecimal(value, MathContext.UNLIMITED) == BigDecimal(
705+
asDouble,
706+
MathContext.UNLIMITED))
701707
Some(asDouble)
702708
else
703709
None

0 commit comments

Comments
 (0)