From d930abce2dd4551273fe8776e0690cfc424bee3d Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 17 Mar 2020 21:16:43 +0000 Subject: [PATCH] ChunkSerializer: improve palette writing performance with one weird trick this optimization relies on the fact that palette entries are always unsigned, and positive zigzag varints are just the same as their non-zigzag counterparts, except shifted left by 1 bit. This eliminates some function call overhead, making the encoding slightly less agonizingly slow. --- src/network/mcpe/serializer/ChunkSerializer.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/network/mcpe/serializer/ChunkSerializer.php b/src/network/mcpe/serializer/ChunkSerializer.php index e1e010d65..2b1643ac0 100644 --- a/src/network/mcpe/serializer/ChunkSerializer.php +++ b/src/network/mcpe/serializer/ChunkSerializer.php @@ -63,9 +63,13 @@ final class ChunkSerializer{ $stream->putByte(($blocks->getBitsPerBlock() << 1) | 1); //last 1-bit means "network format", but seems pointless $stream->put($blocks->getWordArray()); $palette = $blocks->getPalette(); - $stream->putVarInt(count($palette)); //yes, this is intentionally zigzag + + //these LSHIFT by 1 uvarints are optimizations: the client expects zigzag varints here + //but since we know they are always unsigned, we can avoid the extra fcall overhead of + //zigzag and just shift directly. + $stream->putUnsignedVarInt(count($palette) << 1); //yes, this is intentionally zigzag foreach($palette as $p){ - $stream->putVarInt(RuntimeBlockMapping::toStaticRuntimeId($p >> 4, $p & 0xf)); + $stream->putUnsignedVarInt(RuntimeBlockMapping::toStaticRuntimeId($p >> 4, $p & 0xf) << 1); } } }