Remove Decimal interning

This commit is contained in:
DBotThePony 2023-06-18 18:15:08 +07:00
parent 9276d087df
commit 849563e7eb
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -128,7 +128,7 @@ class Decimal private constructor(val mag: BigInteger, marker: Nothing?) : Numbe
return this return this
} }
return Decimal(mag + other.mag, null).interned() return Decimal(mag + other.mag, null)
} }
operator fun minus(other: Decimal): Decimal { operator fun minus(other: Decimal): Decimal {
@ -136,7 +136,7 @@ class Decimal private constructor(val mag: BigInteger, marker: Nothing?) : Numbe
return this return this
} }
return Decimal(mag - other.mag, null).interned() return Decimal(mag - other.mag, null)
} }
operator fun times(other: Decimal): Decimal { operator fun times(other: Decimal): Decimal {
@ -145,16 +145,16 @@ class Decimal private constructor(val mag: BigInteger, marker: Nothing?) : Numbe
} else if (other.mag == BigInteger.ONE) { } else if (other.mag == BigInteger.ONE) {
return this return this
} else if (other.mag == BI_MINUS_ONE) { } else if (other.mag == BI_MINUS_ONE) {
return Decimal(-mag, null).interned() return Decimal(-mag, null)
} }
val result = mag * other.mag val result = mag * other.mag
val (a, b) = result.divideAndRemainder(PRECISION_POW_BI) val (a, b) = result.divideAndRemainder(PRECISION_POW_BI)
if (b >= PRECISION_POW_BI_HIGH) { if (b >= PRECISION_POW_BI_HIGH) {
return Decimal(a + BigInteger.ONE, null).interned() return Decimal(a + BigInteger.ONE, null)
} else { } else {
return Decimal(a, null).interned() return Decimal(a, null)
} }
} }
@ -164,10 +164,10 @@ class Decimal private constructor(val mag: BigInteger, marker: Nothing?) : Numbe
} else if (other.mag == BigInteger.ONE) { } else if (other.mag == BigInteger.ONE) {
return this return this
} else if (other.mag == BI_MINUS_ONE) { } else if (other.mag == BI_MINUS_ONE) {
return Decimal(-mag, null).interned() return Decimal(-mag, null)
} }
return Decimal((mag * PRECISION_POW_BI) / other.mag, null).interned() return Decimal((mag * PRECISION_POW_BI) / other.mag, null)
} }
operator fun rem(other: Decimal): Decimal { operator fun rem(other: Decimal): Decimal {
@ -413,7 +413,7 @@ class Decimal private constructor(val mag: BigInteger, marker: Nothing?) : Numbe
return this return this
} }
return Decimal(-mag, null).interned() return Decimal(-mag, null)
} }
operator fun unaryPlus() = this operator fun unaryPlus() = this
@ -578,10 +578,6 @@ class Decimal private constructor(val mag: BigInteger, marker: Nothing?) : Numbe
return toBigDecmial().divide(divisor.toBigDecmial(), PERCENTAGE_CONTEXT).toFloat() return toBigDecmial().divide(divisor.toBigDecmial(), PERCENTAGE_CONTEXT).toFloat()
} }
private fun interned(): Decimal {
return pool[this] ?: this
}
@Suppress("unused") @Suppress("unused")
companion object { companion object {
/** /**
@ -604,13 +600,6 @@ class Decimal private constructor(val mag: BigInteger, marker: Nothing?) : Numbe
private const val cacheSizeL = cacheSize.toLong() private const val cacheSizeL = cacheSize.toLong()
private val cache = Array(cacheSize) { Decimal(BigInteger.valueOf(it.toLong() - cacheSize / 2)) } private val cache = Array(cacheSize) { Decimal(BigInteger.valueOf(it.toLong() - cacheSize / 2)) }
private val pool = Object2ObjectOpenHashMap<Decimal, Decimal>()
init {
for (value in cache) {
pool[value] = value
}
}
/** /**
* Returns pooled value if present, otherwise constructs new object * Returns pooled value if present, otherwise constructs new object
@ -648,11 +637,11 @@ class Decimal private constructor(val mag: BigInteger, marker: Nothing?) : Numbe
@JvmStatic @JvmStatic
fun valueOf(value: Byte) = valueOf(value.toInt()) fun valueOf(value: Byte) = valueOf(value.toInt())
@JvmStatic fun valueOf(value: BigInteger) = Decimal(value).interned() @JvmStatic fun valueOf(value: BigInteger) = Decimal(value)
@JvmStatic fun valueOf(value: BigDecimal) = Decimal(value).interned() @JvmStatic fun valueOf(value: BigDecimal) = Decimal(value)
@JvmStatic fun valueOf(value: String) = Decimal(value).interned() @JvmStatic fun valueOf(value: String) = Decimal(value)
@JvmStatic fun valueOf(value: Float) = Decimal(value).interned() @JvmStatic fun valueOf(value: Float) = Decimal(value)
@JvmStatic fun valueOf(value: Double) = Decimal(value).interned() @JvmStatic fun valueOf(value: Double) = Decimal(value)
@JvmStatic @JvmStatic
fun fromByteArray(input: ByteArray): Decimal { fun fromByteArray(input: ByteArray): Decimal {