Cache numbers between 0 and 256 to make serialization/deserialization faster (#1482)

This commit is contained in:
Chris Stewart 2020-05-29 05:00:15 -05:00 committed by GitHub
parent b561e47d6b
commit 87c2459c9f

View File

@ -315,8 +315,13 @@ object UInt32
s"Cannot create ${super.getClass.getSimpleName} from $underlying")
}
lazy val zero = UInt32(BigInt(0))
lazy val one = UInt32(BigInt(1))
/** Cache from 0 to 256 UInt32 */
private val cached: Vector[UInt32] = {
0.until(256).map(i => UInt32(BigInt(i))).toVector
}
lazy val zero = cached(0)
lazy val one = cached(1)
private lazy val minUnderlying: A = 0
private lazy val maxUnderlying: A = BigInt(4294967295L)
@ -324,6 +329,7 @@ object UInt32
lazy val min = zero
lazy val max = UInt32(maxUnderlying)
override def isInBound(num: A): Boolean =
num <= maxUnderlying && num >= minUnderlying
@ -344,9 +350,7 @@ object UInt32
/** Checks if we have the number cached, if not allocates a new object to represent the number */
private def checkCached(long: Long): UInt32 = {
if (long == 0) zero
else if (long == 1) one
else if (long == maxUnderlying) max
if (long < 256 && long >= 0) cached(long.toInt)
else {
UInt32(BigInt(long))
}
@ -362,8 +366,8 @@ object UInt64
s"Cannot create ${super.getClass.getSimpleName} from $underlying")
}
lazy val zero = UInt64(BigInt(0))
lazy val one = UInt64(BigInt(1))
lazy val zero = cached(0)
lazy val one = cached(1)
private lazy val minUnderlying: A = 0
private lazy val maxUnderlying: A = BigInt("18446744073709551615")
@ -371,6 +375,11 @@ object UInt64
lazy val min = UInt64(minUnderlying)
lazy val max = UInt64(maxUnderlying)
/** Cache from 0 to 256 UInt64 */
private val cached: Vector[UInt64] = {
0.until(256).map(i => UInt64(BigInt(i))).toVector
}
lazy val twentyThree = UInt64(BigInt(23)) //p2sh compact size uint
lazy val twentyFive = UInt64(BigInt(25)) //p2pkh compact size uint
lazy val oneHundredFive = UInt64(BigInt(105)) //multisig spk 3 public keys
@ -392,13 +401,7 @@ object UInt64
def apply(num: BigInt): UInt64 = UInt64Impl(num)
private def checkCached(num: Long): UInt64 = {
if (num == 0) zero
else if (num == 1) one
else if (num == 25) twentyFive
else if (num == 23) twentyThree
else if (num == 34) thirtyFour
else if (num == 22) twentyTwo
else if (num == 105) oneHundredFive
if (num < 256 && num >= 0) cached(num.toInt)
else UInt64Impl(num)
}
}
@ -412,9 +415,14 @@ object Int32
s"Cannot create ${super.getClass.getSimpleName} from $underlying")
}
val zero = Int32(BigInt(0))
val one = Int32(BigInt(1))
val two = Int32(BigInt(2))
/** Cache from 0 to 256 Int32 */
private val cached: Vector[Int32] = {
0.until(256).map(i => Int32(BigInt(i))).toVector
}
val zero = cached(0)
val one = cached(1)
val two = cached(2)
private lazy val minUnderlying: A = BigInt(-2147483648)
private lazy val maxUnderlying: A = BigInt(2147483647)
@ -437,10 +445,7 @@ object Int32
def apply(bigInt: BigInt): Int32 = Int32Impl(bigInt)
private def checkCached(int: Int): Int32 = {
if (int == 0) zero
else if (int == 1) one
else if (int == 2) two
else if (int == maxUnderlying) max
if (int < 256 && int >= 0) cached(int)
else {
Int32(BigInt(int))
}
@ -456,8 +461,13 @@ object Int64
s"Cannot create ${super.getClass.getSimpleName} from $underlying")
}
lazy val zero = Int64(0)
lazy val one = Int64(1)
/** Cache from 0 to 256 Int64 */
private val cached: Vector[Int64] = {
0.until(256).map(i => Int64(BigInt(i))).toVector
}
lazy val zero = cached(0)
lazy val one = cached(1)
private lazy val minUnderlying: A = -9223372036854775808L
private lazy val maxUnderlying: A = 9223372036854775807L
@ -473,7 +483,15 @@ object Int64
Int64(BigInt(bytes.toArray).toLong)
}
def apply(long: Long): Int64 = Int64(BigInt(long))
def apply(long: Long): Int64 = {
checkCached(long)
}
def apply(bigInt: BigInt): Int64 = Int64Impl(bigInt)
private def checkCached(long: Long): Int64 = {
if (long < 256 && long >= 0) cached(long.toInt)
else Int64(BigInt(long))
}
}