From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com> Date: Mon, 13 May 2024 17:40:02 +0300 Subject: [PATCH] C2ME: reduce_allocs diff --git a/src/main/java/net/minecraft/world/level/levelgen/feature/OreFeature.java b/src/main/java/net/minecraft/world/level/levelgen/feature/OreFeature.java index 506b2afd099c9b7e9ac3f6f2fcea8e523fae396b..2394f6f2c031b25a6cbeb79918a04b5710548d98 100644 --- a/src/main/java/net/minecraft/world/level/levelgen/feature/OreFeature.java +++ b/src/main/java/net/minecraft/world/level/levelgen/feature/OreFeature.java @@ -69,7 +69,7 @@ public class OreFeature extends Feature { int verticalSize ) { int i = 0; - BitSet bitSet = new BitSet(horizontalSize * verticalSize * horizontalSize); + BitSet bitSet = space.bxteam.divinemc.util.c2me.ObjectCachingUtils.getCachedOrNewBitSet(horizontalSize * verticalSize * horizontalSize); // DivineMC - C2ME: reduce_allocs BlockPos.MutableBlockPos mutableBlockPos = new BlockPos.MutableBlockPos(); int j = config.size; double[] ds = new double[j * 4]; diff --git a/src/main/java/space/bxteam/divinemc/util/c2me/ObjectCachingUtils.java b/src/main/java/space/bxteam/divinemc/util/c2me/ObjectCachingUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..828b9cb2f6506b9775dfccc934e9a64d5ddfaeff --- /dev/null +++ b/src/main/java/space/bxteam/divinemc/util/c2me/ObjectCachingUtils.java @@ -0,0 +1,20 @@ +package space.bxteam.divinemc.util.c2me; + +import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; + +import java.util.BitSet; +import java.util.function.IntFunction; + +public class ObjectCachingUtils { + private static final IntFunction bitSetConstructor = BitSet::new; + + public static ThreadLocal> BITSETS = ThreadLocal.withInitial(Int2ObjectOpenHashMap::new); + + private ObjectCachingUtils() {} + + public static BitSet getCachedOrNewBitSet(int bits) { + final BitSet bitSet = BITSETS.get().computeIfAbsent(bits, bitSetConstructor); + bitSet.clear(); + return bitSet; + } +}