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.
This commit is contained in:
Dylan K. Taylor 2020-03-17 21:16:43 +00:00
parent 093a7c239e
commit d930abce2d

View File

@ -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);
}
}
}