From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Wed, 26 Jun 2024 18:52:28 +0800 Subject: [PATCH] Replace shape full block cache with hashtable Removed since 1.21 diff --git a/src/main/java/me/jellysquid/mods/lithium/common/util/collections/Object2BooleanCacheTable.java b/src/main/java/me/jellysquid/mods/lithium/common/util/collections/Object2BooleanCacheTable.java new file mode 100644 index 0000000000000000000000000000000000000000..5e422708ca0a6161892293a49342f78ae671bf9b --- /dev/null +++ b/src/main/java/me/jellysquid/mods/lithium/common/util/collections/Object2BooleanCacheTable.java @@ -0,0 +1,62 @@ +// Gale - Lithium - replace shape full block cache with hashtable - cache table utility + +package me.jellysquid.mods.lithium.common.util.collections; + +import it.unimi.dsi.fastutil.HashCommon; +import net.minecraft.util.Mth; + +import java.util.function.Predicate; + +/** + * A lossy hashtable implementation that stores a mapping between an object and a boolean. + *

+ * Any hash collisions will result in an overwrite: this is safe because the correct value can always be recomputed, + * given that the given operator is deterministic. + *

+ * This implementation is safe to use from multiple threads + */ +public final class Object2BooleanCacheTable { + private final int mask; + + private final Node[] nodes; + + private final Predicate operator; + + @SuppressWarnings("unchecked") + public Object2BooleanCacheTable(int capacity, Predicate operator) { + int capacity1 = Mth.smallestEncompassingPowerOfTwo(capacity); + this.mask = capacity1 - 1; + + this.nodes = (Node[]) new Node[capacity1]; + + this.operator = operator; + } + + private static int hash(T key) { + return HashCommon.mix(key.hashCode()); + } + + public boolean get(T key) { + int idx = hash(key) & this.mask; + + Node node = this.nodes[idx]; + if (node != null && key.equals(node.key)) { + return node.value; + } + + boolean test = this.operator.test(key); + this.nodes[idx] = new Node<>(key, test); + + return test; + } + + static class Node { + final T key; + final boolean value; + + Node(T key, boolean value) { + this.key = key; + this.value = value; + } + } +} diff --git a/src/main/java/net/minecraft/world/level/block/Block.java b/src/main/java/net/minecraft/world/level/block/Block.java index a7108b2be0746aa1f0e574d8c6f5ffad6d369835..3d703b6cb79939367b73b57df3d3ef497589a580 100644 --- a/src/main/java/net/minecraft/world/level/block/Block.java +++ b/src/main/java/net/minecraft/world/level/block/Block.java @@ -68,11 +68,12 @@ public class Block extends BlockBehaviour implements ItemLike { private static final Logger LOGGER = LogUtils.getLogger(); private final Holder.Reference builtInRegistryHolder; public static final IdMapper BLOCK_STATE_REGISTRY = new IdMapper<>(); - private static final LoadingCache SHAPE_FULL_BLOCK_CACHE = CacheBuilder.newBuilder().maximumSize(512L).weakKeys().build(new CacheLoader() { - public Boolean load(VoxelShape voxelshape) { - return !Shapes.joinIsNotEmpty(Shapes.block(), voxelshape, BooleanOp.NOT_SAME); - } - }); + // Gale start - Lithium - replace shape full block cache with hashtable + private static final me.jellysquid.mods.lithium.common.util.collections.Object2BooleanCacheTable SHAPE_FULL_BLOCK_CACHE = new me.jellysquid.mods.lithium.common.util.collections.Object2BooleanCacheTable<>( + 512, + shape -> !Shapes.joinIsNotEmpty(Shapes.block(), shape, BooleanOp.NOT_SAME) + ); + // Gale end - Lithium - replace shape full block cache with hashtable public static final int UPDATE_NEIGHBORS = 1; public static final int UPDATE_CLIENTS = 2; public static final int UPDATE_INVISIBLE = 4; @@ -280,7 +281,7 @@ public class Block extends BlockBehaviour implements ItemLike { } public static boolean isShapeFullBlock(VoxelShape shape) { - return ((ca.spottedleaf.moonrise.patches.collisions.shape.CollisionVoxelShape)shape).moonrise$isFullBlock(); // Paper - optimise collisions + return SHAPE_FULL_BLOCK_CACHE.get(shape); // Gale - Lithium - replace shape full block cache with hashtable } public void animateTick(BlockState state, Level world, BlockPos pos, RandomSource random) {}