9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-25 09:59:15 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0211-Rewrite-ClientboundLightUpdatePacketData.patch
Dreeam 66887e3367 Update Leaf 1.21.6 (#385)
* Init 1.21.6

* Finish API patches

* Apply purpur patches & all paper server patches

* Apply some

* Apply all

* Updated Upstream (Paper/Purpur)

Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@6aafe29e 1.21.6 dev
PaperMC/Paper@ae7c74ec tweak mob goal keys, generate missing api
PaperMC/Paper@9ea1460b Revert "fix sponge-absorb deleting chest content (#12647)"

Purpur Changes:
PurpurMC/Purpur@eb0ba67d Updated Upstream (Paper)
PurpurMC/Purpur@7c6502dc Updated Upstream (Paper)
PurpurMC/Purpur@aa289e2c Updated Upstream (Paper)
PurpurMC/Purpur@b1d412fb Updated Upstream (Paper)
PurpurMC/Purpur@293e28a0 use empty registryaccess where context is not needed, closes #1676
PurpurMC/Purpur@452bb319 port PaperMC/Paper#12654, closes #1665
PurpurMC/Purpur@849bc79c register test subcommands used for debugging, closes #1675

* Fix build & diff

* Update vanilla attribute size

* [ci/skip] Forget to remove this

* Updated Upstream (Paper/Purpur)

Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@a24f9b20 1.21.6 dev
PaperMC/Paper@457a0a69 Bump version string and protocol version to 1.21.6 release
PaperMC/Paper@245b5473 Update base to 1.21.6
PaperMC/Paper@b28d77b3 Update Moonrise to 1.21.6
PaperMC/Paper@8318dcb7 Update DataConverter to 1.21.6-rc1

Purpur Changes:
PurpurMC/Purpur@b228e316 Updated Upstream (Paper)
PurpurMC/Purpur@d6562a57 make happy ghast takes-damage-from-water and always-drop-exp options work
PurpurMC/Purpur@08d989b6 inline import
PurpurMC/Purpur@61d7f559 Updated Upstream (Paper)
PurpurMC/Purpur@469a4de5 Merge branch 'ver/1.21.5' into ver/1.21.6

* Updated Upstream (Paper)

Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@d2b1158e Set correct world access on BSL snapshots

* Updated Upstream (Paper)

Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@023e6c29 Ensure waypoints are per world
PaperMC/Paper@40d5d31c Fix recursively converting JSON Text Components

* Update spigot mapping to remap reobf mapping to mojamp for plugins

* Updated Upstream (Paper/Purpur)

Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@1fcc4162 Implement FeatureHooks#isChunkSent for Moonrise chunk system
PaperMC/Paper@e4d7178a Implement WaypointTransmitter#isChunkVisible
PaperMC/Paper@8980ead7 Directly walk text component after converting from JSON
PaperMC/Paper@5613ed6d Provide env environment variable and copy spigots sys prop for overriding default repository
PaperMC/Paper@62b7f86d Avoid and discourage use of Maven Central as a CDN (#12689)
PaperMC/Paper@f5534cb7 [ci/skip] Run generators (#12691)
PaperMC/Paper@4781d28b Re-add Log4j Javadocs (#12693)
PaperMC/Paper@74fbcce5 Check type of Material in get/set stats (#12607)
PaperMC/Paper@4b3f967e Improve Fix MC-44654 (#12703)
PaperMC/Paper@a7dd2635 Enable spigot obfuscation support (#12695)
PaperMC/Paper@6a51c44e Cleanup Primed TNT Fix (#12704)
PaperMC/Paper@839c6a18 Fix #11169 (#12706)
PaperMC/Paper@c77d5f99 Fix MC-297591
PaperMC/Paper@219f86ee Implement chunk unload delay config option
PaperMC/Paper@e4eb69b8 Do not allow ticket level decreases to be processed asynchronously
PaperMC/Paper@71b0c768 Adds support for vanilla negative explosions (#12705)
PaperMC/Paper@3750927a [ci/skip] Fix PermissionManager#clearPermissions() docs bad wording (#12657)
PaperMC/Paper@d61a51e8 Add ItemType#getBurnDuration() (#12604)
PaperMC/Paper@29fc8532 Allow empty paths in namespaced keys (#12687)
PaperMC/Paper@4419afb9 fix: Safely handle nanosecond overflow in ClickCallback (#12686)
PaperMC/Paper@56528821 Add `isProxyEnabled` method to ServerConfiguration (#12664)
PaperMC/Paper@c0dda0ea Add `isForceDefaultGameMode` method (#12673)
PaperMC/Paper@e714de63 Fix excess slot updates
PaperMC/Paper@6d0821d2 [ci/skip] Fix docs for Spawner class and cleanup (#12710)
PaperMC/Paper@cceffe3d Release ItemType and BlockType (#12708)
PaperMC/Paper@186e9e33 Relocate CommandMap#registerServerAliases() call to after lifecycle events have been run (#12601)
PaperMC/Paper@5edcf6dd Cleanup/command dispatching (#12713)

Purpur Changes:
PurpurMC/Purpur@baa196f5 Updated Upstream (Paper)
PurpurMC/Purpur@fdd1e980 Updated Upstream (Paper)
PurpurMC/Purpur@439f15db Updated Upstream (Paper)
PurpurMC/Purpur@46a28b93 [ci/skip] update version in README
PurpurMC/Purpur@162bd288 Updated Upstream (Paper)
PurpurMC/Purpur@afcdf9bb Updated Upstream (Paper)

* fix random tick order

Signed-off-by: hayanesuru <hayanesuru@outlook.jp>

* update optimize random tick

* fix mob spawning over limit

* optimize mob spawning

* fix FastBitRadixSort memory leak

* optimize brain collections

* [ci/skip] update comments

* Revert "fix FastBitRadixSort memory leak"

This reverts commit bdea683525.

* optimize brain collections

* fix async mob spawning data race

* [ci/skip] cleanup

* remove hash lookup in optimize random tick

* Updated Upstream (Paper/Purpur)

Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@803baf0b Support hidden entities in Waypoints (#12715)
PaperMC/Paper@1814d8b4 build: publish to fill (#12717)
PaperMC/Paper@e454fef4 Add support for private constructors in plugin main classes (#12652)
PaperMC/Paper@d0e808f4 Move player to correct position in vehicle move packet handler
PaperMC/Paper@46b4b0b8 Improve keepalive ping system
PaperMC/Paper@38c1ddb5 Add and use FeatureHooks.getAllEntities
PaperMC/Paper@2f083acb Ensure player entity schedulers are ticked when they are dead
PaperMC/Paper@7c90c7c4 Optimise EntityScheduler ticking
PaperMC/Paper@aa6ee44a Re-add global region scheduler tick erroneously removed in last commit
PaperMC/Paper@d7510efc Fix #12722 (#12726)
PaperMC/Paper@0caf75f8 Fix #12721 (#12725)
PaperMC/Paper@bee28792 Adventure 4.23.0 (#12690)
PaperMC/Paper@692e93a9 Fix MC-299110
PaperMC/Paper@ea10fa4a Don't mutate the position of Items for MC-4 Fix (#12702)
PaperMC/Paper@aa6cd74c Remove unnecesary item check for ServerboundPlayerActionPacket RELEASE_USE_ITEM (#12668)
PaperMC/Paper@c9e89f49 Expose arrow velocity in EntityShootBowEvent for mobs (#12688)
PaperMC/Paper@7ec3174a Jump out of experimental phase
PaperMC/Paper@4e1a2555 Update try catch for command handling
PaperMC/Paper@e382e687 Fix 0 yield on explosion events
PaperMC/Paper@35b2c6ec Use dropped item for stats info (#12747)
PaperMC/Paper@bd79e20c [ci/skip] PluginManager#getPlugin and PluginManager#isPluginEnabled are case-insensitive (#12723)

Purpur Changes:
PurpurMC/Purpur@4a5974cf Updated Upstream (Paper)
PurpurMC/Purpur@3893bba0 this is important...
PurpurMC/Purpur@47e758fb Updated Upstream (Paper)

* Update PWT patch

* cleanup

* fix playermobcaps command

* fix Didn't fill chunk buffer in async chunk sending

* cleanup

* fix HurtByTargetGoal npe

* optimize collectSpawningChunks (#382)

* optimize collectSpawningChunks

* add bit set

* cleanup

* cleanup

* cleanup

* cleanup

* Updated Upstream (Paper/Purpur)

Upstream has released updates that appear to apply and compile correctly

Paper Changes:
PaperMC/Paper@57c202e0 Allow adventure nbt codec to parse all tags

Purpur Changes:
PurpurMC/Purpur@a4776d7b Updated Upstream (Paper)
PurpurMC/Purpur@dfd8a7ad Fix phantom going at high speed when ridden (#1680)

* Fix diff

---------

Signed-off-by: hayanesuru <hayanesuru@outlook.jp>
Co-authored-by: hayanesuru <hayanesuru@outlook.jp>
2025-06-30 05:17:33 +08:00

241 lines
10 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Taiyou06 <kaandindar21@gmail.com>
Date: Mon, 24 Feb 2025 21:33:24 +0100
Subject: [PATCH] Rewrite ClientboundLightUpdatePacketData
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
On writing operations:
At 256 sections with 0.75 fill ratio: 18x faster (333.489 μs → 18.606 μs)
At 256 sections with 0.5 fill ratio: 13.5x faster (147.804 μs → 10.962 μs)
diff --git a/net/minecraft/network/protocol/game/ClientboundLightUpdatePacketData.java b/net/minecraft/network/protocol/game/ClientboundLightUpdatePacketData.java
index a0b54f3a3d11e0f0f1cb806406a870ba36da8f07..234280499fe1bc495bcdd4c3e144d1f99b7e6975 100644
--- a/net/minecraft/network/protocol/game/ClientboundLightUpdatePacketData.java
+++ b/net/minecraft/network/protocol/game/ClientboundLightUpdatePacketData.java
@@ -16,30 +16,113 @@ import net.minecraft.world.level.lighting.LevelLightEngine;
public class ClientboundLightUpdatePacketData {
private static final StreamCodec<ByteBuf, byte[]> DATA_LAYER_STREAM_CODEC = ByteBufCodecs.byteArray(2048);
+
+ // Leaf start - Rewrite ClientboundLightUpdatePacketData
+ // Static constants to avoid allocations
+ private static final byte[][] EMPTY_ARRAY = new byte[0][];
+
+ // Pre-sized arrays to avoid dynamic resizing
+ private static final ThreadLocal<byte[][]> SKY_BUFFER = ThreadLocal.withInitial(() -> new byte[256][]);
+ private static final ThreadLocal<byte[][]> BLOCK_BUFFER = ThreadLocal.withInitial(() -> new byte[256][]);
+
+ // Pre-cached BitSets with fixed size
private final BitSet skyYMask;
private final BitSet blockYMask;
private final BitSet emptySkyYMask;
private final BitSet emptyBlockYMask;
- private final List<byte[]> skyUpdates;
- private final List<byte[]> blockUpdates;
+
+ // Fixed arrays with exact counts
+ private final byte[][] skyUpdates;
+ private final byte[][] blockUpdates;
+ private final int skyUpdateCount;
+ private final int blockUpdateCount;
+ // Leaf end - Rewrite ClientboundLightUpdatePacketData
public ClientboundLightUpdatePacketData(ChunkPos chunkPos, LevelLightEngine lightEngine, @Nullable BitSet skyLight, @Nullable BitSet blockLight) {
- this.skyYMask = new BitSet();
- this.blockYMask = new BitSet();
- this.emptySkyYMask = new BitSet();
- this.emptyBlockYMask = new BitSet();
- this.skyUpdates = Lists.newArrayList();
- this.blockUpdates = Lists.newArrayList();
-
- for (int i = 0; i < lightEngine.getLightSectionCount(); i++) {
+ // Leaf start - Rewrite ClientboundLightUpdatePacketData
+ int sectionCount = lightEngine.getLightSectionCount();
+
+ // Round up to nearest long boundary (64 bits) to prevent BitSet expansion
+ int longWords = (sectionCount + 63) >>> 6;
+ int bitSetSize = longWords << 6;
+
+ // Pre-size all BitSets to exact size needed
+ this.skyYMask = new BitSet(bitSetSize);
+ this.blockYMask = new BitSet(bitSetSize);
+ this.emptySkyYMask = new BitSet(bitSetSize);
+ this.emptyBlockYMask = new BitSet(bitSetSize);
+
+ // Get buffer arrays from thread local storage to avoid allocations
+ byte[][] skyBuffer = SKY_BUFFER.get();
+ byte[][] blockBuffer = BLOCK_BUFFER.get();
+
+ // Process all sections in a single pass
+ int skyCount = 0;
+ int blockCount = 0;
+ int minLightSection = lightEngine.getMinLightSection();
+
+ // Cache layer listeners to avoid repeated method calls
+ var skyLayerListener = lightEngine.getLayerListener(LightLayer.SKY);
+ var blockLayerListener = lightEngine.getLayerListener(LightLayer.BLOCK);
+
+ // Single pass through all sections
+ for (int i = 0; i < sectionCount; i++) {
+ int sectionY = minLightSection + i;
+ SectionPos sectionPos = SectionPos.of(chunkPos.x, sectionY, chunkPos.z);
+
+ // Process sky light
if (skyLight == null || skyLight.get(i)) {
- this.prepareSectionData(chunkPos, lightEngine, LightLayer.SKY, i, this.skyYMask, this.emptySkyYMask, this.skyUpdates);
+ DataLayer skyData = skyLayerListener.getDataLayerData(sectionPos);
+ if (skyData != null) {
+ if (skyData.isEmpty()) {
+ emptySkyYMask.set(i);
+ } else {
+ skyYMask.set(i);
+ // Store in buffer temporarily - only clone at the end
+ skyBuffer[skyCount++] = skyData.getData();
+ }
+ }
}
+ // Process block light
if (blockLight == null || blockLight.get(i)) {
- this.prepareSectionData(chunkPos, lightEngine, LightLayer.BLOCK, i, this.blockYMask, this.emptyBlockYMask, this.blockUpdates);
+ DataLayer blockData = blockLayerListener.getDataLayerData(sectionPos);
+ if (blockData != null) {
+ if (blockData.isEmpty()) {
+ emptyBlockYMask.set(i);
+ } else {
+ blockYMask.set(i);
+ // Store in buffer temporarily - only clone at the end
+ blockBuffer[blockCount++] = blockData.getData();
+ }
+ }
+ }
+ }
+
+ // Create final arrays with exact sizes
+ if (skyCount > 0) {
+ this.skyUpdates = new byte[skyCount][];
+ // Clone only at the end to minimize work
+ for (int i = 0; i < skyCount; i++) {
+ this.skyUpdates[i] = skyBuffer[i].clone();
+ }
+ } else {
+ this.skyUpdates = EMPTY_ARRAY;
+ }
+
+ if (blockCount > 0) {
+ this.blockUpdates = new byte[blockCount][];
+ // Clone only at the end to minimize work
+ for (int i = 0; i < blockCount; i++) {
+ this.blockUpdates[i] = blockBuffer[i].clone();
}
+ } else {
+ this.blockUpdates = EMPTY_ARRAY;
}
+
+ this.skyUpdateCount = skyCount;
+ this.blockUpdateCount = blockCount;
+ // Leaf end - Rewrite ClientboundLightUpdatePacketData
}
public ClientboundLightUpdatePacketData(FriendlyByteBuf buffer, int x, int z) {
@@ -47,8 +130,30 @@ public class ClientboundLightUpdatePacketData {
this.blockYMask = buffer.readBitSet();
this.emptySkyYMask = buffer.readBitSet();
this.emptyBlockYMask = buffer.readBitSet();
- this.skyUpdates = buffer.readList(DATA_LAYER_STREAM_CODEC);
- this.blockUpdates = buffer.readList(DATA_LAYER_STREAM_CODEC);
+
+ // Leaf start - Rewrite ClientboundLightUpdatePacketData
+ // Read lists directly as arrays to avoid intermediate collections
+ List<byte[]> skyList = buffer.readList(DATA_LAYER_STREAM_CODEC);
+ List<byte[]> blockList = buffer.readList(DATA_LAYER_STREAM_CODEC);
+
+ int skySize = skyList.size();
+ int blockSize = blockList.size();
+
+ if (skySize > 0) {
+ this.skyUpdates = skyList.toArray(new byte[skySize][]);
+ } else {
+ this.skyUpdates = EMPTY_ARRAY;
+ }
+
+ if (blockSize > 0) {
+ this.blockUpdates = blockList.toArray(new byte[blockSize][]);
+ } else {
+ this.blockUpdates = EMPTY_ARRAY;
+ }
+
+ this.skyUpdateCount = skySize;
+ this.blockUpdateCount = blockSize;
+ // Leaf end - Rewrite ClientboundLightUpdatePacketData
}
public void write(FriendlyByteBuf buffer) {
@@ -56,25 +161,33 @@ public class ClientboundLightUpdatePacketData {
buffer.writeBitSet(this.blockYMask);
buffer.writeBitSet(this.emptySkyYMask);
buffer.writeBitSet(this.emptyBlockYMask);
- buffer.writeCollection(this.skyUpdates, DATA_LAYER_STREAM_CODEC);
- buffer.writeCollection(this.blockUpdates, DATA_LAYER_STREAM_CODEC);
- }
- private void prepareSectionData(
- ChunkPos chunkPos, LevelLightEngine levelLightEngine, LightLayer lightLayer, int index, BitSet skyLight, BitSet blockLight, List<byte[]> updates
- ) {
- DataLayer dataLayerData = levelLightEngine.getLayerListener(lightLayer)
- .getDataLayerData(SectionPos.of(chunkPos, levelLightEngine.getMinLightSection() + index));
- if (dataLayerData != null) {
- if (dataLayerData.isEmpty()) {
- blockLight.set(index);
- } else {
- skyLight.set(index);
- updates.add(dataLayerData.copy().getData());
+ // Leaf start - Rewrite ClientboundLightUpdatePacketData
+ // Avoid creating unnecessary objects when writing
+ if (this.skyUpdateCount > 0) {
+ // Use direct array access for efficiency
+ buffer.writeVarInt(this.skyUpdateCount);
+ for (int i = 0; i < this.skyUpdateCount; i++) {
+ DATA_LAYER_STREAM_CODEC.encode(buffer, this.skyUpdates[i]);
}
+ } else {
+ buffer.writeVarInt(0);
}
+
+ if (this.blockUpdateCount > 0) {
+ // Use direct array access for efficiency
+ buffer.writeVarInt(this.blockUpdateCount);
+ for (int i = 0; i < this.blockUpdateCount; i++) {
+ DATA_LAYER_STREAM_CODEC.encode(buffer, this.blockUpdates[i]);
+ }
+ } else {
+ buffer.writeVarInt(0);
+ }
+ // Leaf end - Rewrite ClientboundLightUpdatePacketData
}
+ // Getter methods
+
public BitSet getSkyYMask() {
return this.skyYMask;
}
@@ -84,7 +197,7 @@ public class ClientboundLightUpdatePacketData {
}
public List<byte[]> getSkyUpdates() {
- return this.skyUpdates;
+ return this.skyUpdateCount > 0 ? java.util.Arrays.asList(this.skyUpdates) : List.of(); // Leaf - Rewrite ClientboundLightUpdatePacketData
}
public BitSet getBlockYMask() {
@@ -96,6 +209,6 @@ public class ClientboundLightUpdatePacketData {
}
public List<byte[]> getBlockUpdates() {
- return this.blockUpdates;
+ return this.blockUpdateCount > 0 ? java.util.Arrays.asList(this.blockUpdates) : List.of(); // Leaf - Rewrite ClientboundLightUpdatePacketData
}
}