9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2026-01-04 15:41:40 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0107-Cache-canHoldAnyFluid-result.patch
2025-01-18 10:44:55 -05:00

84 lines
4.6 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 canHoldAnyFluid result
Cache the result of half of canHoldAnyFluid logic, since there is a state#is in this 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 36ac6114cc3449a3b344baac5f3034288cf77a63..82c36a031e883033a76968b7dfccc2c1f2a3d687 100644
--- a/net/minecraft/world/level/block/state/BlockBehaviour.java
+++ b/net/minecraft/world/level/block/state/BlockBehaviour.java
@@ -454,6 +454,8 @@ public abstract class BlockBehaviour implements FeatureElement {
private VoxelShape[] occlusionShapesByFace;
private boolean propagatesSkylightDown;
private int lightBlock;
+ private boolean canHoldAnyFluidInternal; // Leaf - Cache canHoldAnyFluid result
+ private boolean canHoldAnyFluidInternalInit; // Leaf - Cache canHoldAnyFluid result
// Paper start - rewrite chunk system
private boolean isConditionallyFullOpaque;
@@ -603,6 +605,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 canHoldAnyFluid result
+ this.canHoldAnyFluidInternalInit = false; // Leaf - Cache canHoldAnyFluid result
// Paper start - rewrite chunk system
this.isConditionallyFullOpaque = this.canOcclude & this.useShapeForLightOcclusion;
// Paper end - rewrite chunk system
@@ -654,6 +658,18 @@ public abstract class BlockBehaviour implements FeatureElement {
return block != Blocks.COBWEB && block != Blocks.BAMBOO_SAPLING && this.isSolid();
}
+ // Leaf start - Cache canHoldAnyFluid 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 canHoldAnyFluid 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 c535cd03c577d76c3b19da5a8426d0cbee3069f0..efa4f2e2a2ee8b12cdcc7b674f29eae89179b409 100644
--- a/net/minecraft/world/level/material/FlowingFluid.java
+++ b/net/minecraft/world/level/material/FlowingFluid.java
@@ -384,7 +384,7 @@ public abstract class FlowingFluid extends Fluid {
BlockGetter level, BlockPos pos, BlockState state, Direction direction, BlockPos spreadPos, BlockState spreadState, FluidState fluidState
) {
return !this.isSourceBlockOfThisType(fluidState)
- && canHoldAnyFluid(spreadState)
+ && state.canHoldAnyFluidInternal() // Leaf - Cache canHoldAnyFluid result
&& canPassThroughWall(direction, level, pos, state, spreadPos, spreadState);
}
@@ -450,7 +450,7 @@ public abstract class FlowingFluid extends Fluid {
return map;
}
- private static boolean canHoldAnyFluid(BlockState state) {
+ public static boolean canHoldAnyFluid(BlockState state) { // Leaf - Cache canHoldAnyFluid result - private -> public
Block block = state.getBlock();
return block instanceof LiquidBlockContainer
|| !state.blocksMotion()
@@ -466,7 +466,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 canHoldAnyFluid result
}
private static boolean canHoldSpecificFluid(BlockGetter level, BlockPos pos, BlockState state, Fluid fluid) {