From 29edf383cda3013524646b2acbec119ce92d149b Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sun, 9 Mar 2025 20:06:49 +0700 Subject: [PATCH] Use thread local non-cryptographic random for Quantum Battery UUIDs since they dont need to be cryptographically secure --- .../kotlin/ru/dbotthepony/mc/otm/GlobalEventHandler.kt | 6 ++++++ src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt | 4 ++++ .../ru/dbotthepony/mc/otm/core/util/PCG32RandomSource.kt | 2 -- .../ru/dbotthepony/mc/otm/item/QuantumBatteryItem.kt | 9 ++++++--- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/GlobalEventHandler.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/GlobalEventHandler.kt index 1e4a600d9..5f49c00f4 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/GlobalEventHandler.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/GlobalEventHandler.kt @@ -7,6 +7,7 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder import net.minecraft.client.server.IntegratedServer import net.minecraft.core.HolderLookup import net.minecraft.server.MinecraftServer +import net.minecraft.util.RandomSource import net.minecraft.world.level.Level import net.neoforged.api.distmarker.Dist import net.neoforged.fml.loading.FMLLoader @@ -22,12 +23,17 @@ import ru.dbotthepony.mc.otm.core.collect.WeakHashSet import ru.dbotthepony.mc.otm.core.util.AtomicallyInvalidatedLazy import ru.dbotthepony.mc.otm.core.util.IConditionalTickable import ru.dbotthepony.mc.otm.core.util.ITickable +import ru.dbotthepony.mc.otm.core.util.PCG32RandomSource import ru.dbotthepony.mc.otm.core.util.TickList import ru.dbotthepony.mc.otm.graph.GraphNodeList import java.lang.ref.Cleaner import java.util.* import java.util.concurrent.atomic.AtomicInteger +private val threadLocalRandom = ThreadLocal.withInitial { PCG32RandomSource() } +internal val THREAD_LOCAL_RANDOM: RandomSource + get() = threadLocalRandom.get() + private val preServerTick = TickList() private val postServerTick = TickList() private val preWorldTick = WeakHashMap() diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt index 97326b9b1..6203217b1 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt @@ -627,6 +627,10 @@ infix fun FluidStack.isNotSameAs(other: FluidStack): Boolean { data class DoublePair(val first: Double, val second: Double) +fun RandomSource.nextUUID(): UUID { + return UUID(nextLong(), nextLong()) +} + // normal distribution via Marsaglia polar method fun RandomGenerator.nextNormalDoubles(stddev: Double, mean: Double): DoublePair { var rand1: Double diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/PCG32RandomSource.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/PCG32RandomSource.kt index da61dbff2..7b721c195 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/PCG32RandomSource.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/PCG32RandomSource.kt @@ -2,11 +2,9 @@ package ru.dbotthepony.mc.otm.core.util import net.minecraft.util.Mth import net.minecraft.util.RandomSource -import net.minecraft.world.level.levelgen.LegacyRandomSource import net.minecraft.world.level.levelgen.MarsagliaPolarGaussian import net.minecraft.world.level.levelgen.PositionalRandomFactory import net.minecraft.world.level.levelgen.RandomSupport -import ru.dbotthepony.kommons.random.LCG64Random import ru.dbotthepony.kommons.random.PCG32Random import java.lang.StringBuilder diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/QuantumBatteryItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/QuantumBatteryItem.kt index 473813cce..76098d215 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/QuantumBatteryItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/QuantumBatteryItem.kt @@ -24,6 +24,7 @@ import net.neoforged.neoforge.event.tick.ServerTickEvent import net.neoforged.neoforge.network.PacketDistributor import net.neoforged.neoforge.network.handling.IPayloadContext import ru.dbotthepony.mc.otm.OverdriveThatMatters +import ru.dbotthepony.mc.otm.THREAD_LOCAL_RANDOM import ru.dbotthepony.mc.otm.capability.FlowDirection import ru.dbotthepony.mc.otm.capability.MatteryCapability import ru.dbotthepony.mc.otm.capability.trackedItems @@ -42,6 +43,8 @@ import ru.dbotthepony.mc.otm.core.math.readDecimal import ru.dbotthepony.mc.otm.core.math.set import ru.dbotthepony.mc.otm.core.math.writeDecimal import ru.dbotthepony.mc.otm.core.nbt.set +import ru.dbotthepony.mc.otm.core.nextUUID +import ru.dbotthepony.mc.otm.core.util.PCG32RandomSource import ru.dbotthepony.mc.otm.core.util.formatPower import ru.dbotthepony.mc.otm.isClientThread import ru.dbotthepony.mc.otm.isServerThread @@ -82,7 +85,7 @@ class QuantumBatteryItem(val savedataID: String, val balanceValues: EnergyBalanc } } - class UnboundValues(override val uuid: UUID = UUID.randomUUID()) : IValues { + class UnboundValues(override val uuid: UUID = THREAD_LOCAL_RANDOM.nextUUID()) : IValues { override var energy: Decimal = Decimal.ZERO override var passed: Decimal = Decimal.ZERO override var received: Decimal = Decimal.ZERO @@ -125,7 +128,7 @@ class QuantumBatteryItem(val savedataID: String, val balanceValues: EnergyBalanc } fun values(): Values { - return values(UUID.randomUUID()) + return values(THREAD_LOCAL_RANDOM.nextUUID()) } override fun save(nbt: CompoundTag, registry: HolderLookup.Provider): CompoundTag { @@ -160,7 +163,7 @@ class QuantumBatteryItem(val savedataID: String, val balanceValues: EnergyBalanc fun updateValues() { if (!values.isServer && isServerThread()) { - values = serverData.values(stack[MDataComponentTypes.QUANTUM_LINK_ID] ?: UUID.randomUUID().also { stack[MDataComponentTypes.QUANTUM_LINK_ID] = it }) + values = serverData.values(stack[MDataComponentTypes.QUANTUM_LINK_ID] ?: THREAD_LOCAL_RANDOM.nextUUID().also { stack[MDataComponentTypes.QUANTUM_LINK_ID] = it }) } else if (isClientThread()) { val id = stack[MDataComponentTypes.QUANTUM_LINK_ID] ?: return