Do not calculate block counts on the client

It appears that this call is very expensive. We instead substitute
an exact check with setting the special colliding blocks to whether
the palette initially contains a special colliding block, and after
on block updating by whether any special colliding block was set.
This commit is contained in:
Spottedleaf
2024-09-17 11:33:26 -07:00
parent a9678c76eb
commit fef4872a3a
4 changed files with 24 additions and 8 deletions

View File

@@ -56,14 +56,20 @@ abstract class LevelChunkSectionMixin implements BlockCountingChunkSection {
}
@Unique
private int specialCollidingBlocks;
private boolean isClient;
@Unique
private static final short CLIENT_FORCED_SPECIAL_COLLIDING_BLOCKS = (short)9999;
@Unique
private short specialCollidingBlocks;
@Unique
private final IntList tickingBlocks = new IntList();
@Override
public final int moonrise$getSpecialCollidingBlocks() {
return this.specialCollidingBlocks;
public final boolean moonrise$hasSpecialCollidingBlocks() {
return this.specialCollidingBlocks != 0;
}
@Override
@@ -86,6 +92,14 @@ abstract class LevelChunkSectionMixin implements BlockCountingChunkSection {
if (oldState == newState) {
return;
}
if (this.isClient) {
if (CollisionUtil.isSpecialCollidingBlock(newState)) {
this.specialCollidingBlocks = CLIENT_FORCED_SPECIAL_COLLIDING_BLOCKS;
}
return;
}
if (CollisionUtil.isSpecialCollidingBlock(oldState)) {
--this.specialCollidingBlocks;
}
@@ -184,7 +198,7 @@ abstract class LevelChunkSectionMixin implements BlockCountingChunkSection {
}
/**
* @reason Call recalcBlockCounts on the client, as the client does not invoke it when deserializing chunk sections.
* @reason Set up special colliding blocks on the client, as it is too expensive to perform a full calculation
* @author Spottedleaf
*/
@Inject(
@@ -194,6 +208,8 @@ abstract class LevelChunkSectionMixin implements BlockCountingChunkSection {
)
)
private void callRecalcBlocksClient(final CallbackInfo ci) {
this.recalcBlockCounts();
this.isClient = true;
// force has special colliding blocks to be true
this.specialCollidingBlocks = this.nonEmptyBlockCount != (short)0 && this.maybeHas(CollisionUtil::isSpecialCollidingBlock) ? CLIENT_FORCED_SPECIAL_COLLIDING_BLOCKS : (short)0;
}
}

View File

@@ -427,7 +427,7 @@ abstract class LevelMixin implements CollisionLevel, LevelAccessor, AutoCloseabl
continue;
}
final boolean hasSpecial = ((BlockCountingChunkSection)section).moonrise$getSpecialCollidingBlocks() != 0;
final boolean hasSpecial = ((BlockCountingChunkSection)section).moonrise$hasSpecialCollidingBlocks();
final int sectionAdjust = !hasSpecial ? 1 : 0;
final PalettedContainer<BlockState> blocks = section.states;

View File

@@ -4,7 +4,7 @@ import ca.spottedleaf.moonrise.common.list.IntList;
public interface BlockCountingChunkSection {
public int moonrise$getSpecialCollidingBlocks();
public boolean moonrise$hasSpecialCollidingBlocks();
public IntList moonrise$getTickingBlockList();

View File

@@ -1994,7 +1994,7 @@ public final class CollisionUtil {
continue;
}
final boolean hasSpecial = ((BlockCountingChunkSection)section).moonrise$getSpecialCollidingBlocks() != 0;
final boolean hasSpecial = ((BlockCountingChunkSection)section).moonrise$hasSpecialCollidingBlocks();
final int sectionAdjust = !hasSpecial ? 1 : 0;
final PalettedContainer<BlockState> blocks = section.states;