9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0195-Cache-part-of-canHoldFluid-result.patch
2025-06-11 05:20:38 +08:00

66 lines
3.7 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com>
Date: Tue, 26 Nov 2024 17:15:38 -0500
Subject: [PATCH] Cache part of canHoldFluid result
Cache the result of part of canHoldFluid logic, since there are many state#is in canAnyHoldFluid method,
it uses map contains to do iteration to check whether a block has a specific block tag key,
which the contains iteration call is very expensive if called everytime
In the test, it can improve ~30% performance in ~1577000 times of canHoldAnyFluid calls (~159ms -> ~111ms)
diff --git a/net/minecraft/world/level/block/state/BlockBehaviour.java b/net/minecraft/world/level/block/state/BlockBehaviour.java
index 84f4d772bfe06383ca718b6a00d983e97e2e35f4..3219b9caa654c7a64e5e4a92178528e1104b34c7 100644
--- a/net/minecraft/world/level/block/state/BlockBehaviour.java
+++ b/net/minecraft/world/level/block/state/BlockBehaviour.java
@@ -451,6 +451,8 @@ public abstract class BlockBehaviour implements FeatureElement {
private VoxelShape[] occlusionShapesByFace;
private boolean propagatesSkylightDown;
private int lightBlock;
+ private boolean canHoldAnyFluidInternal; // Leaf - Cache part of canHoldFluid result
+ private boolean canHoldAnyFluidInternalInit; // Leaf - Cache part of canHoldFluid result
// Paper start - rewrite chunk system
private boolean isConditionallyFullOpaque;
@@ -600,6 +602,8 @@ public abstract class BlockBehaviour implements FeatureElement {
this.propagatesSkylightDown = this.owner.propagatesSkylightDown(this.asState());
this.lightBlock = this.owner.getLightBlock(this.asState());
+ this.canHoldAnyFluidInternal = false; // Leaf - Cache part of canHoldFluid result
+ this.canHoldAnyFluidInternalInit = false; // Leaf - Cache part of canHoldFluid result
// Paper start - rewrite chunk system
this.isConditionallyFullOpaque = this.canOcclude & this.useShapeForLightOcclusion;
// Paper end - rewrite chunk system
@@ -651,6 +655,18 @@ public abstract class BlockBehaviour implements FeatureElement {
return block != Blocks.COBWEB && block != Blocks.BAMBOO_SAPLING && this.isSolid();
}
+ // Leaf start - Cache part of canHoldFluid result
+ public boolean canHoldAnyFluidInternal() {
+ // Lazy load cache
+ if (!canHoldAnyFluidInternalInit) {
+ canHoldAnyFluidInternal = net.minecraft.world.level.material.FlowingFluid.canHoldAnyFluid(this.asState());
+ canHoldAnyFluidInternalInit = true;
+ }
+
+ return canHoldAnyFluidInternal;
+ }
+ // Leaf end - Cache part of canHoldFluid result
+
@Deprecated
public boolean isSolid() {
return this.legacySolid;
diff --git a/net/minecraft/world/level/material/FlowingFluid.java b/net/minecraft/world/level/material/FlowingFluid.java
index 2a2883b4c38b8ac6e014dab66da0c141f6ac2a64..4625bd55e1cb01dfb9921dcd033f05b4a8f9ad74 100644
--- a/net/minecraft/world/level/material/FlowingFluid.java
+++ b/net/minecraft/world/level/material/FlowingFluid.java
@@ -467,7 +467,7 @@ public abstract class FlowingFluid extends Fluid {
}
private static boolean canHoldFluid(BlockGetter level, BlockPos pos, BlockState state, Fluid fluid) {
- return canHoldAnyFluid(state) && canHoldSpecificFluid(level, pos, state, fluid);
+ return /*canHoldAnyFluid(state)*/ state.canHoldAnyFluidInternal() && canHoldSpecificFluid(level, pos, state, fluid); // Leaf - Cache part of canHoldFluid result
}
private static boolean canHoldSpecificFluid(BlockGetter level, BlockPos pos, BlockState state, Fluid fluid) {