From fcec612718174f6de545605cd2dcda0cd81f7a3e Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 3 Mar 2025 22:19:57 +0700 Subject: [PATCH] Lock CMWC state size to 256 WORDs, improving performance of nextInt() --- .../mc/otm/core/util/CMWCRandom.kt | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/CMWCRandom.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/CMWCRandom.kt index aec7fc452..ae291f2d1 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/CMWCRandom.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/CMWCRandom.kt @@ -7,8 +7,8 @@ import net.minecraft.world.level.levelgen.PositionalRandomFactory import java.lang.StringBuilder import java.util.random.RandomGenerator -class CMWCRandom(seed: Long = System.nanoTime(), private val stateSize: Int = CMWC_DEFAULT_STATE_SIZE) : RandomGenerator, RandomSource { - private val state = IntArray(stateSize) +class CMWCRandom(seed: Long = System.nanoTime()) : RandomGenerator, RandomSource { + private val state = IntArray(CMWC_STATE_SIZE) private var carry = 0 private var stateIndex = 0 private val gaussian = MarsagliaPolarGaussian(this) @@ -37,7 +37,7 @@ class CMWCRandom(seed: Long = System.nanoTime(), private val stateSize: Int = CM } override fun nextInt(): Int { - stateIndex = (stateIndex + 1) % state.size + stateIndex = (stateIndex + 1).and(CMWC_STATE_SIZE - 1) val t = 18782L * state[stateIndex] + carry carry = t.ushr(32).toInt() @@ -83,24 +83,24 @@ class CMWCRandom(seed: Long = System.nanoTime(), private val stateSize: Int = CM } override fun fork(): RandomSource { - return CMWCRandom(nextLong(), stateSize) + return CMWCRandom(nextLong()) } override fun forkPositional(): PositionalRandomFactory { - return Positional(nextLong(), stateSize) + return Positional(nextLong()) } - class Positional(val seed: Long, val stateSize: Int = CMWC_DEFAULT_STATE_SIZE) : PositionalRandomFactory { + class Positional(val seed: Long) : PositionalRandomFactory { override fun at(x: Int, y: Int, z: Int): RandomSource { - return CMWCRandom(Mth.getSeed(x, y, z).xor(seed), stateSize) + return CMWCRandom(Mth.getSeed(x, y, z).xor(seed)) } override fun fromHashOf(name: String): RandomSource { - return CMWCRandom(name.hashCode().toLong().xor(seed), stateSize) + return CMWCRandom(name.hashCode().toLong().xor(seed)) } override fun fromSeed(seed: Long): RandomSource { - return CMWCRandom(seed, stateSize) + return CMWCRandom(seed) } override fun parityConfigString(builder: StringBuilder) { @@ -109,7 +109,7 @@ class CMWCRandom(seed: Long = System.nanoTime(), private val stateSize: Int = CM } companion object { - const val CMWC_DEFAULT_STATE_SIZE = 256 // 4096 + const val CMWC_STATE_SIZE = 256 // 4096 const val CMWC_CARRY_MAX = 809430660 } }