Skip to content

Commit efabb1f

Browse files
committed
#37 Improved performance
Bumped versions
1 parent a6f610b commit efabb1f

File tree

5 files changed

+279
-95
lines changed

5 files changed

+279
-95
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import sbtcrossproject.crossProject
77

88
val currentScalaVersion = "2.11.11"
99
val scala210Version = "2.10.6"
10-
val scala212Version = "2.12.2"
10+
val scala212Version = "2.12.3"
1111
val scalaCheckVersion = "1.13.4"
1212
val specs2Version = "3.9.1"
1313

project/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=0.13.15
1+
sbt.version=0.13.16

project/plugins.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.16")
1+
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.19")
22

33
addSbtPlugin("org.scala-native" % "sbt-crossproject" % "0.2.0")
44

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

Lines changed: 138 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -352,16 +352,17 @@ package object ast {
352352
@inline def maxLengthConstant: Int = 10
353353
var limit: Int = -Integer.MAX_VALUE
354354
var decimalFlag = false
355-
var result: BigInt = 0
355+
var result: Int = 0
356+
var resultBigInt: BigInt = null
356357
var negative = false
357358
var multmin: Int = 0
358359
var char: Char = 0
359360
var i = 0
360361
var eFlag = false
361-
var trailingZeroes: BigInt = 0
362+
var trailingZeroes: Int = 0
362363
var negativeEFlag: Boolean = false
363-
var resultNegativeEFlag: BigInt = 0
364-
var digitLength: BigInt = 0
364+
var resultNegativeEFlag: Int = 0
365+
var digitLength: Int = 0
365366
multmin = limit / radix
366367
if (value.charAt(0) == '-') {
367368
limit = Integer.MIN_VALUE
@@ -403,7 +404,10 @@ package object ast {
403404
if (trailingZeroes >= resultNegativeEFlag) {
404405
var i2: Int = 0
405406
while (i2 < resultNegativeEFlag) {
406-
result = result / radix
407+
if (resultBigInt == null)
408+
result = result / radix
409+
else
410+
resultBigInt = resultBigInt / radix
407411
i2 += 1
408412
}
409413
} else
@@ -413,45 +417,78 @@ package object ast {
413417

414418
val maxLenCheck = digitLength <= maxLengthConstant
415419

416-
if (result < multmin && maxLenCheck && !eFlag)
417-
return None
420+
if (resultBigInt == null) {
421+
if (result < multmin && maxLenCheck && !eFlag)
422+
return None
423+
} else {
424+
if (resultBigInt < multmin && maxLenCheck && !eFlag)
425+
return None
426+
}
418427

419428
if (!(digit == 0 && (decimalFlag || eFlag))) {
420429

421430
if (!negativeEFlag) {
422-
result *= radix
423-
if (result < limit + digit && maxLenCheck)
424-
return None
425-
result -= digit
431+
if (digitLength == maxLengthConstant -1) {
432+
var result2: Int = result
433+
result2 *= radix
434+
435+
if (!negative && result2 < 0 || negative && result2 > 0) {
436+
resultBigInt = BigInt(result)
437+
}
438+
}
439+
440+
if (resultBigInt == null) {
441+
result *= radix
442+
if (result < limit + digit && maxLenCheck)
443+
return None
444+
result -= digit
445+
} else {
446+
resultBigInt *= radix
447+
if (resultBigInt < limit + digit && maxLenCheck)
448+
return None
449+
resultBigInt -= digit
450+
}
426451
}
427452
}
428453
}
429454
i += 1
430455
}
431-
if (result < limit)
432-
None
433-
else {
434-
if (negative)
435-
Some(result.toInt)
436-
else
437-
Some(-result.toInt)
456+
if (resultBigInt == null) {
457+
if (result < limit)
458+
None
459+
else {
460+
if (negative)
461+
Some(result)
462+
else
463+
Some(-result)
464+
}
465+
} else {
466+
if (resultBigInt < limit)
467+
None
468+
else {
469+
if (negative)
470+
Some(resultBigInt.toInt)
471+
else
472+
Some(-resultBigInt.toInt)
473+
}
438474
}
439475
}
440476

441477
private[ast] def toLong(value: String): Option[Long] = {
442478
@inline def maxLengthConstant: Int = 19
443479
var limit: Long = -Long.MaxValue
444480
var decimalFlag = false
445-
var result: BigInt = 0
481+
var result: Long = 0
482+
var resultBigInt: BigInt = null
446483
var negative = false
447484
var multmin: Long = 0
448485
var char: Char = 0
449486
var i = 0
450487
var eFlag = false
451-
var trailingZeroes: BigInt = 0
488+
var trailingZeroes: Int = 0
452489
var negativeEFlag: Boolean = false
453-
var resultNegativeEFlag: BigInt = 0
454-
var digitLength: BigInt = 0
490+
var resultNegativeEFlag: Long = 0
491+
var digitLength: Int = 0
455492
multmin = limit / radix
456493
if (value.charAt(0) == '-') {
457494
limit = Long.MinValue
@@ -493,7 +530,10 @@ package object ast {
493530
if (trailingZeroes >= resultNegativeEFlag) {
494531
var i2: Int = 0
495532
while (i2 < resultNegativeEFlag) {
496-
result = result / radix
533+
if (resultBigInt == null)
534+
result = result / radix
535+
else
536+
resultBigInt = resultBigInt / radix
497537
i2 += 1
498538
}
499539
} else
@@ -503,42 +543,75 @@ package object ast {
503543

504544
val maxLenCheck = digitLength <= maxLengthConstant
505545

506-
if (result < multmin && maxLenCheck && !eFlag)
507-
return None
546+
if (resultBigInt == null) {
547+
if (result < multmin && maxLenCheck && !eFlag)
548+
return None
549+
} else {
550+
if (resultBigInt < multmin && maxLenCheck && !eFlag)
551+
return None
552+
}
508553

509554
if (!(digit == 0 && (decimalFlag || eFlag))) {
510555

511556
if (!negativeEFlag) {
512-
result *= radix
513-
if (result < limit + digit && maxLenCheck)
514-
return None
515-
result -= digit
557+
if (digitLength == maxLengthConstant -1) {
558+
var result2: Long = result
559+
result2 *= radix
560+
561+
if (!negative && result2 < 0 || negative && result2 > 0) {
562+
resultBigInt = BigInt(result)
563+
}
564+
}
565+
566+
if (resultBigInt == null) {
567+
result *= radix
568+
if (result < limit + digit && maxLenCheck)
569+
return None
570+
result -= digit
571+
} else {
572+
resultBigInt *= radix
573+
if (resultBigInt < limit + digit && maxLenCheck)
574+
return None
575+
resultBigInt -= digit
576+
}
516577
}
517578
}
518579
}
519580
i += 1
520581
}
521-
if (result < limit)
522-
None
523-
else {
524-
if (negative)
525-
Some(result.toLong)
526-
else
527-
Some(-result.toLong)
582+
if (resultBigInt == null) {
583+
if (result < limit)
584+
None
585+
else {
586+
if (negative)
587+
Some(result)
588+
else
589+
Some(-result)
590+
}
591+
} else {
592+
if (resultBigInt < limit)
593+
None
594+
else {
595+
if (negative)
596+
Some(resultBigInt.toLong)
597+
else
598+
Some(-resultBigInt.toLong)
599+
}
528600
}
529601
}
530602

531603
private[ast] def toBigInt(value: String): Option[BigInt] = {
532604
var decimalFlag = false
533-
var result: BigInt = 0
605+
var result: Int = 0 // assume that values are initially small when we convert to bigInt
606+
var resultBigInt: BigInt = null
534607
var negative = false
535608
var char: Char = 0
536609
var i = 0
537610
var eFlag = false
538-
var trailingZeroes: BigInt = 0
611+
var trailingZeroes: Int = 0
539612
var negativeEFlag: Boolean = false
540-
var resultNegativeEFlag: BigInt = 0
541-
var digitLength: BigInt = 0
613+
var resultNegativeEFlag: Int = 0
614+
var digitLength: Int = 0
542615
if (value.charAt(0) == '-') {
543616
negative = true
544617
i += 1
@@ -589,17 +662,36 @@ package object ast {
589662
if (!(digit == 0 && (decimalFlag || eFlag))) {
590663

591664
if (!negativeEFlag) {
592-
result *= radix
593-
result -= digit
665+
var result2: Int = result
666+
result2 *= radix
667+
if (!negative && result2 < 0 || negative && result2 > 0) {
668+
resultBigInt = BigInt(result)
669+
}
670+
671+
if (resultBigInt == null) {
672+
result *= radix
673+
result -= digit
674+
} else {
675+
resultBigInt *= radix
676+
resultBigInt -= digit
677+
}
594678
}
595679
}
596680
}
597681
i += 1
598682
}
599-
if (negative)
600-
Some(result)
601-
else
602-
Some(-result)
683+
684+
if (resultBigInt == null) {
685+
if (negative)
686+
Some(BigInt(result))
687+
else
688+
Some(BigInt(-result))
689+
} else {
690+
if (negative)
691+
Some(resultBigInt)
692+
else
693+
Some(resultBigInt)
694+
}
603695
}
604696

605697
private[ast] def toDouble(value: String): Option[Double] = {

0 commit comments

Comments
 (0)