From 849563e7eb542920607d562621a918a4b2f4523a Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sun, 18 Jun 2023 18:15:08 +0700 Subject: [PATCH] Remove Decimal interning --- .../dbotthepony/mc/otm/core/math/Decimal.kt | 37 +++++++------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt index b10e92863..267509fb1 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt @@ -128,7 +128,7 @@ class Decimal private constructor(val mag: BigInteger, marker: Nothing?) : Numbe return this } - return Decimal(mag + other.mag, null).interned() + return Decimal(mag + other.mag, null) } operator fun minus(other: Decimal): Decimal { @@ -136,7 +136,7 @@ class Decimal private constructor(val mag: BigInteger, marker: Nothing?) : Numbe return this } - return Decimal(mag - other.mag, null).interned() + return Decimal(mag - other.mag, null) } 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) { return this } else if (other.mag == BI_MINUS_ONE) { - return Decimal(-mag, null).interned() + return Decimal(-mag, null) } val result = mag * other.mag val (a, b) = result.divideAndRemainder(PRECISION_POW_BI) if (b >= PRECISION_POW_BI_HIGH) { - return Decimal(a + BigInteger.ONE, null).interned() + return Decimal(a + BigInteger.ONE, null) } 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) { return this } 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 { @@ -413,7 +413,7 @@ class Decimal private constructor(val mag: BigInteger, marker: Nothing?) : Numbe return this } - return Decimal(-mag, null).interned() + return Decimal(-mag, null) } 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() } - private fun interned(): Decimal { - return pool[this] ?: this - } - @Suppress("unused") companion object { /** @@ -604,13 +600,6 @@ class Decimal private constructor(val mag: BigInteger, marker: Nothing?) : Numbe private const val cacheSizeL = cacheSize.toLong() private val cache = Array(cacheSize) { Decimal(BigInteger.valueOf(it.toLong() - cacheSize / 2)) } - private val pool = Object2ObjectOpenHashMap() - - init { - for (value in cache) { - pool[value] = value - } - } /** * Returns pooled value if present, otherwise constructs new object @@ -648,11 +637,11 @@ class Decimal private constructor(val mag: BigInteger, marker: Nothing?) : Numbe @JvmStatic fun valueOf(value: Byte) = valueOf(value.toInt()) - @JvmStatic fun valueOf(value: BigInteger) = Decimal(value).interned() - @JvmStatic fun valueOf(value: BigDecimal) = Decimal(value).interned() - @JvmStatic fun valueOf(value: String) = Decimal(value).interned() - @JvmStatic fun valueOf(value: Float) = Decimal(value).interned() - @JvmStatic fun valueOf(value: Double) = Decimal(value).interned() + @JvmStatic fun valueOf(value: BigInteger) = Decimal(value) + @JvmStatic fun valueOf(value: BigDecimal) = Decimal(value) + @JvmStatic fun valueOf(value: String) = Decimal(value) + @JvmStatic fun valueOf(value: Float) = Decimal(value) + @JvmStatic fun valueOf(value: Double) = Decimal(value) @JvmStatic fun fromByteArray(input: ByteArray): Decimal {