From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Taiyou06 Date: Thu, 7 Nov 2024 19:45:31 +0100 Subject: [PATCH] C2ME: Reduce Allocations This patch is based on the following mixin: "com/ishland/c2me/opts/allocs/mixin/object_pooling_caching/MixinOreFeature.java" By: ishland As part of: C2ME (https://github.com/RelativityMC/C2ME-fabric) Licensed under: MIT (https://opensource.org/licenses/MIT) 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..df119e67ffdd73a7c2c93dfb35985d0c850f9e64 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 = org.dreeam.leaf.util.cache.CachedOrNewBitsGetter.getCachedOrNewBitSet(horizontalSize * verticalSize * horizontalSize); // Leaf - C2ME - Reduce Allocations BlockPos.MutableBlockPos mutableBlockPos = new BlockPos.MutableBlockPos(); int j = config.size; double[] ds = new double[j * 4]; diff --git a/src/main/java/org/dreeam/leaf/util/cache/CachedOrNewBitsGetter.java b/src/main/java/org/dreeam/leaf/util/cache/CachedOrNewBitsGetter.java new file mode 100644 index 0000000000000000000000000000000000000000..5a8abdff3069d64a9866ebf01e2c8b70f4791a74 --- /dev/null +++ b/src/main/java/org/dreeam/leaf/util/cache/CachedOrNewBitsGetter.java @@ -0,0 +1,21 @@ +package org.dreeam.leaf.util.cache; + +import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; + +import java.util.BitSet; +import java.util.function.IntFunction; + +public class CachedOrNewBitsGetter { + private static final IntFunction bitSetConstructor = BitSet::new; + + public static ThreadLocal> BITSETS = ThreadLocal.withInitial(Int2ObjectOpenHashMap::new); + + private CachedOrNewBitsGetter() { + } + + public static BitSet getCachedOrNewBitSet(int bits) { + final BitSet bitSet = BITSETS.get().computeIfAbsent(bits, bitSetConstructor); + bitSet.clear(); + return bitSet; + } +}