From 0744d40f786917cf7d07dd4c5c1d997868dab442 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Sun, 29 Sep 2024 18:37:59 -0700 Subject: [PATCH 1/2] Don't call recalcBlockCounts on the client This logic is expensive, and we only need to know if there may be any special colliding blocks in the section. Before this change, ClientboundLevelChunkWithLightPacket was the top contributor to frame time spikes, after this change it's nowhere near the top of the profile. Specifically, the recalc logic was taking upwards of 80% of the processing time for this packet, the simplified check only takes <5%. --- .../mixin/block_counting/LevelChunkSectionMixin.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/ca/spottedleaf/moonrise/mixin/block_counting/LevelChunkSectionMixin.java b/src/main/java/ca/spottedleaf/moonrise/mixin/block_counting/LevelChunkSectionMixin.java index db261ab..e97ba3e 100644 --- a/src/main/java/ca/spottedleaf/moonrise/mixin/block_counting/LevelChunkSectionMixin.java +++ b/src/main/java/ca/spottedleaf/moonrise/mixin/block_counting/LevelChunkSectionMixin.java @@ -55,6 +55,7 @@ abstract class LevelChunkSectionMixin implements BlockCountingChunkSection { } } + // Client-side: this will only be 0 or 1 (0 meaning no special colliders, 1 meaning any non-0 amount), see checkForSpecialCollidingBlocksClient and uses of moonrise$getSpecialCollidingBlocks @Unique private int specialCollidingBlocks; @@ -184,7 +185,7 @@ public void recalcBlockCounts() { } /** - * @reason Call recalcBlockCounts on the client, as the client does not invoke it when deserializing chunk sections. + * @reason We need to know if there are any special colliding blocks in the section * @author Spottedleaf */ @Inject( @@ -193,7 +194,7 @@ public void recalcBlockCounts() { value = "RETURN" ) ) - private void callRecalcBlocksClient(final CallbackInfo ci) { - this.recalcBlockCounts(); + private void checkForSpecialCollidingBlocksClient(final CallbackInfo ci) { + this.specialCollidingBlocks = this.maybeHas(CollisionUtil::isSpecialCollidingBlock) ? 1 : 0; } } From 31d3d5d0ff9b0eee8ccaf2859a5121a04c34dd28 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Sun, 29 Sep 2024 19:18:14 -0700 Subject: [PATCH 2/2] Make updateBlockCallback aware of when the special colliding block count is invalid --- .../LevelChunkSectionMixin.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/ca/spottedleaf/moonrise/mixin/block_counting/LevelChunkSectionMixin.java b/src/main/java/ca/spottedleaf/moonrise/mixin/block_counting/LevelChunkSectionMixin.java index e97ba3e..1bb3cfd 100644 --- a/src/main/java/ca/spottedleaf/moonrise/mixin/block_counting/LevelChunkSectionMixin.java +++ b/src/main/java/ca/spottedleaf/moonrise/mixin/block_counting/LevelChunkSectionMixin.java @@ -59,6 +59,9 @@ abstract class LevelChunkSectionMixin implements BlockCountingChunkSection { @Unique private int specialCollidingBlocks; + @Unique + private boolean client = false; + @Unique private final IntList tickingBlocks = new IntList(); @@ -87,11 +90,17 @@ private void updateBlockCallback(final int x, final int y, final int z, final Bl if (oldState == newState) { return; } - if (CollisionUtil.isSpecialCollidingBlock(oldState)) { - --this.specialCollidingBlocks; - } - if (CollisionUtil.isSpecialCollidingBlock(newState)) { - ++this.specialCollidingBlocks; + if (!this.client) { + if (CollisionUtil.isSpecialCollidingBlock(oldState)) { + --this.specialCollidingBlocks; + } + if (CollisionUtil.isSpecialCollidingBlock(newState)) { + ++this.specialCollidingBlocks; + } + } else { + if (CollisionUtil.isSpecialCollidingBlock(newState)) { + this.specialCollidingBlocks = 1; + } } final int position = x | (z << 4) | (y << (4+4)); @@ -196,5 +205,6 @@ public void recalcBlockCounts() { ) private void checkForSpecialCollidingBlocksClient(final CallbackInfo ci) { this.specialCollidingBlocks = this.maybeHas(CollisionUtil::isSpecialCollidingBlock) ? 1 : 0; + this.client = true; } }