From 74b061aadb4313b4edbd0992d2767da210fc82fd Mon Sep 17 00:00:00 2001 From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com> Date: Sun, 27 Apr 2025 14:19:14 +0300 Subject: [PATCH] some lithium patches --- .../features/0008-Misc-Optimizations.patch | 13 ++++ .../net/minecraft/core/BlockPos.java.patch | 21 ++++++ .../util/cache/CachedOrNewBitsGetter.java | 21 ++++++ .../util/cache/IterateOutwardsCache.java | 64 +++++++++++++++++++ .../LongList2BlockPosMutableIterable.java | 45 +++++++++++++ 5 files changed, 164 insertions(+) create mode 100644 divinemc-server/minecraft-patches/sources/net/minecraft/core/BlockPos.java.patch create mode 100644 divinemc-server/src/main/java/org/bxteam/divinemc/util/cache/CachedOrNewBitsGetter.java create mode 100644 divinemc-server/src/main/java/org/bxteam/divinemc/util/cache/IterateOutwardsCache.java create mode 100644 divinemc-server/src/main/java/org/bxteam/divinemc/util/cache/LongList2BlockPosMutableIterable.java diff --git a/divinemc-server/minecraft-patches/features/0008-Misc-Optimizations.patch b/divinemc-server/minecraft-patches/features/0008-Misc-Optimizations.patch index 9aa0cfe..eaf917a 100644 --- a/divinemc-server/minecraft-patches/features/0008-Misc-Optimizations.patch +++ b/divinemc-server/minecraft-patches/features/0008-Misc-Optimizations.patch @@ -611,6 +611,19 @@ index 01e5b29d6e9a5c53c0e23b61ed0c1d7be1a0fe08..d80df05e40f3941ade5ed320e12f8dcf return 1.0 * (32.0 * (d1 - 128.0) - 3.0 * (d1 - 120.0) * d2 + 3.0 * d2 * d2) / (128.0 * (32.0 - 3.0 * d2)); } +diff --git a/net/minecraft/world/level/levelgen/feature/OreFeature.java b/net/minecraft/world/level/levelgen/feature/OreFeature.java +index c7b46efd4f08067e2c9c5c8b0e8b71a94a79823d..c7252c636fcea34a866dcc4862b60cef31071666 100644 +--- a/net/minecraft/world/level/levelgen/feature/OreFeature.java ++++ b/net/minecraft/world/level/levelgen/feature/OreFeature.java +@@ -69,7 +69,7 @@ public class OreFeature extends Feature { + int height + ) { + int i = 0; +- BitSet bitSet = new BitSet(width * height * width); ++ BitSet bitSet = org.bxteam.divinemc.util.cache.CachedOrNewBitsGetter.getCachedOrNewBitSet(width * height * width); // DivineMC - Misc Optimizations + BlockPos.MutableBlockPos mutableBlockPos = new BlockPos.MutableBlockPos(); + int i1 = config.size; + double[] doubles = new double[i1 * 4]; diff --git a/net/minecraft/world/level/storage/DimensionDataStorage.java b/net/minecraft/world/level/storage/DimensionDataStorage.java index 05361803a929cbcf651a37cf43009e57acb25f3e..5d21d2520dd33889988d8300060e6a41a8334562 100644 --- a/net/minecraft/world/level/storage/DimensionDataStorage.java diff --git a/divinemc-server/minecraft-patches/sources/net/minecraft/core/BlockPos.java.patch b/divinemc-server/minecraft-patches/sources/net/minecraft/core/BlockPos.java.patch new file mode 100644 index 0000000..fb2c309 --- /dev/null +++ b/divinemc-server/minecraft-patches/sources/net/minecraft/core/BlockPos.java.patch @@ -0,0 +1,21 @@ +--- a/net/minecraft/core/BlockPos.java ++++ b/net/minecraft/core/BlockPos.java +@@ -347,7 +_,18 @@ + }; + } + ++ // DivineMC start - lithium: cached_iterate_outwards ++ private static final org.bxteam.divinemc.util.cache.IterateOutwardsCache ITERATE_OUTWARDS_CACHE = new org.bxteam.divinemc.util.cache.IterateOutwardsCache(50); ++ private static final it.unimi.dsi.fastutil.longs.LongList HOGLIN_PIGLIN_CACHE = ITERATE_OUTWARDS_CACHE.getOrCompute(8, 4, 8); ++ // DivineMC end - lithium: cached_iterate_outwards ++ + public static Iterable withinManhattan(BlockPos pos, int xSize, int ySize, int zSize) { ++ // DivineMC start - lithium: cached_iterate_outwards ++ if (pos != org.bxteam.divinemc.util.cache.IterateOutwardsCache.POS_ZERO) { ++ final it.unimi.dsi.fastutil.longs.LongList positions = xSize == 8 && ySize == 4 && zSize == 8 ? HOGLIN_PIGLIN_CACHE : ITERATE_OUTWARDS_CACHE.getOrCompute(xSize, ySize, zSize); ++ return new org.bxteam.divinemc.util.cache.LongList2BlockPosMutableIterable(pos, positions); ++ } ++ // DivineMC end - lithium: cached_iterate_outwards + int i = xSize + ySize + zSize; + int x1 = pos.getX(); + int y1 = pos.getY(); diff --git a/divinemc-server/src/main/java/org/bxteam/divinemc/util/cache/CachedOrNewBitsGetter.java b/divinemc-server/src/main/java/org/bxteam/divinemc/util/cache/CachedOrNewBitsGetter.java new file mode 100644 index 0000000..b92f2c0 --- /dev/null +++ b/divinemc-server/src/main/java/org/bxteam/divinemc/util/cache/CachedOrNewBitsGetter.java @@ -0,0 +1,21 @@ +package org.bxteam.divinemc.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 BITSET_CONSTRUCTOR = BitSet::new; + private static final ThreadLocal> BITSETS = ThreadLocal.withInitial(Int2ObjectOpenHashMap::new); + + private CachedOrNewBitsGetter() { } + + public static BitSet getCachedOrNewBitSet(int bits) { + final BitSet bitSet = BITSETS.get().computeIfAbsent(bits, BITSET_CONSTRUCTOR); + + bitSet.clear(); + + return bitSet; + } +} diff --git a/divinemc-server/src/main/java/org/bxteam/divinemc/util/cache/IterateOutwardsCache.java b/divinemc-server/src/main/java/org/bxteam/divinemc/util/cache/IterateOutwardsCache.java new file mode 100644 index 0000000..2b2ce66 --- /dev/null +++ b/divinemc-server/src/main/java/org/bxteam/divinemc/util/cache/IterateOutwardsCache.java @@ -0,0 +1,64 @@ +package org.bxteam.divinemc.util.cache; + +import it.unimi.dsi.fastutil.longs.LongArrayList; +import it.unimi.dsi.fastutil.longs.LongList; + +import java.util.Iterator; +import java.util.Random; +import java.util.concurrent.ConcurrentHashMap; + +import net.minecraft.core.BlockPos; + +/** + * @author 2No2Name, original implemenation by SuperCoder7979 and Gegy1000 + */ +public class IterateOutwardsCache { + public static final BlockPos POS_ZERO = new BlockPos(0, 0, 0); + + private final ConcurrentHashMap table; + private final int capacity; + private final Random random; + + public IterateOutwardsCache(int capacity) { + this.capacity = capacity; + this.table = new ConcurrentHashMap<>(31); + this.random = new Random(); + } + + private void fillPositionsWithIterateOutwards(LongList entry, int xRange, int yRange, int zRange) { + for (BlockPos pos : BlockPos.withinManhattan(POS_ZERO, xRange, yRange, zRange)) { + entry.add(pos.asLong()); + } + } + + public LongList getOrCompute(int xRange, int yRange, int zRange) { + long key = BlockPos.asLong(xRange, yRange, zRange); + + LongArrayList entry = this.table.get(key); + if (entry != null) { + return entry; + } + + entry = new LongArrayList(128); + + this.fillPositionsWithIterateOutwards(entry, xRange, yRange, zRange); + + entry.trim(); + + Object previousEntry = this.table.put(key, entry); + + if (previousEntry == null && this.table.size() > this.capacity) { + final Iterator iterator = this.table.keySet().iterator(); + + for (int i = -this.capacity; iterator.hasNext() && i < 5; i++) { + Long key2 = iterator.next(); + + if (this.random.nextInt(8) == 0 && key2 != key) { + iterator.remove(); + } + } + } + + return entry; + } +} diff --git a/divinemc-server/src/main/java/org/bxteam/divinemc/util/cache/LongList2BlockPosMutableIterable.java b/divinemc-server/src/main/java/org/bxteam/divinemc/util/cache/LongList2BlockPosMutableIterable.java new file mode 100644 index 0000000..a274dfb --- /dev/null +++ b/divinemc-server/src/main/java/org/bxteam/divinemc/util/cache/LongList2BlockPosMutableIterable.java @@ -0,0 +1,45 @@ +package org.bxteam.divinemc.util.cache; + +import it.unimi.dsi.fastutil.longs.LongIterator; +import it.unimi.dsi.fastutil.longs.LongList; + +import java.util.Iterator; + +import net.minecraft.core.BlockPos; + +/** + * @author 2No2Name + */ +public class LongList2BlockPosMutableIterable implements Iterable { + private final LongList positions; + private final int xOffset, yOffset, zOffset; + + public LongList2BlockPosMutableIterable(BlockPos offset, LongList posList) { + this.xOffset = offset.getX(); + this.yOffset = offset.getY(); + this.zOffset = offset.getZ(); + this.positions = posList; + } + + @Override + public Iterator iterator() { + return new Iterator<>() { + private final LongIterator it = LongList2BlockPosMutableIterable.this.positions.iterator(); + private final BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos(); + + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public net.minecraft.core.BlockPos next() { + long nextPos = this.it.nextLong(); + return this.pos.set( + LongList2BlockPosMutableIterable.this.xOffset + BlockPos.getX(nextPos), + LongList2BlockPosMutableIterable.this.yOffset + BlockPos.getY(nextPos), + LongList2BlockPosMutableIterable.this.zOffset + BlockPos.getZ(nextPos)); + } + }; + } +}