9
0
mirror of https://github.com/Dreeam-qwq/Gale.git synced 2025-12-20 15:29:30 +00:00
Files
Gale/patches/server/0074-Replace-shape-full-block-cache-with-hashtable.patch
2022-12-01 17:27:21 +01:00

123 lines
5.1 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: MartijnMuijsers <martijnmuijsers@live.nl>
Date: Thu, 1 Dec 2022 14:11:53 +0100
Subject: [PATCH] Replace shape full block cache with hashtable
License: LGPL-3.0 (https://www.gnu.org/licenses/lgpl-3.0.html)
This patch is based on the following mixins and classes:
* "me/jellysquid/mods/lithium/common/util/collections/Object2BooleanCacheTable.java"
* "me/jellysquid/mods/lithium/mixin/shapes/blockstate_cache/BlockMixin.java"
By: Gegy <gegy1000@gmail.com>
As part of: Lithium (https://github.com/CaffeineMC/lithium-fabric)
Licensed under: LGPL-3.0 (https://www.gnu.org/licenses/lgpl-3.0.html)
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..48c32b224095f606669a38f0e200505aff9486c1
--- /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.
+ * <p>
+ * 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.
+ * <p>
+ * This implementation is safe to use from multiple threads
+ */
+public final class Object2BooleanCacheTable<T> {
+
+ private final int mask;
+ private final Node<T>[] nodes;
+ private final Predicate<T> operator;
+
+ @SuppressWarnings("unchecked")
+ public Object2BooleanCacheTable(int capacity, Predicate<T> operator) {
+ int capacity1 = Mth.smallestEncompassingPowerOfTwo(capacity);
+ this.mask = capacity1 - 1;
+ this.nodes = (Node<T>[]) new Node[capacity1];
+ this.operator = operator;
+ }
+
+ private static <T> int hash(T key) {
+ return HashCommon.mix(key.hashCode());
+ }
+
+ public boolean get(T key) {
+ int idx = hash(key) & this.mask;
+
+ Node<T> 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<T> {
+
+ 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 2e65b44f10aeb44fd524a58e7eb815a566c1ad61..ee13a217a72a41d103789b66eef70bf351d1e624 100644
--- a/src/main/java/net/minecraft/world/level/block/Block.java
+++ b/src/main/java/net/minecraft/world/level/block/Block.java
@@ -11,6 +11,8 @@ import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.annotation.Nullable;
+
+import me.jellysquid.mods.lithium.common.util.collections.Object2BooleanCacheTable;
import net.minecraft.SharedConstants;
import net.minecraft.Util;
import net.minecraft.core.BlockPos;
@@ -70,11 +72,12 @@ public class Block extends BlockBehaviour implements ItemLike {
private static final Logger LOGGER = LogUtils.getLogger();
private final Holder.Reference<Block> builtInRegistryHolder;
public static final IdMapper<BlockState> BLOCK_STATE_REGISTRY = new IdMapper<>();
- private static final LoadingCache<VoxelShape, Boolean> SHAPE_FULL_BLOCK_CACHE = CacheBuilder.newBuilder().maximumSize(512L).weakKeys().build(new CacheLoader<VoxelShape, Boolean>() {
- 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 Object2BooleanCacheTable<VoxelShape> SHAPE_FULL_BLOCK_CACHE = new Object2BooleanCacheTable<>(
+ 1536,
+ 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;
@@ -281,7 +284,7 @@ public class Block extends BlockBehaviour implements ItemLike {
}
public static boolean isShapeFullBlock(VoxelShape shape) {
- return (Boolean) Block.SHAPE_FULL_BLOCK_CACHE.getUnchecked(shape);
+ return Block.SHAPE_FULL_BLOCK_CACHE.get(shape); // Gale - Lithium - replace shape full block cache with hashtable
}
public boolean propagatesSkylightDown(BlockState state, BlockGetter world, BlockPos pos) {