From bbe66e8e0982d9c0efc47d991fad7ad89ac5a661 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 6 Nov 2023 17:04:39 +0000 Subject: [PATCH] Block: Improve performance of encodeFullState() if there's no state data to encode, we can avoid useless calls and object allocations. For the best cases (blocks which don't use state data at all) this improves the performance of getStateId() by more than 10x. Blocks which use one or the other benefit by a smaller but still significant margin. --- src/block/Block.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/block/Block.php b/src/block/Block.php index a1d553b9d..b2847bb35 100644 --- a/src/block/Block.php +++ b/src/block/Block.php @@ -270,11 +270,22 @@ class Block{ } private function encodeFullState() : int{ - $writer = new RuntimeDataWriter($this->requiredBlockItemStateDataBits + $this->requiredBlockOnlyStateDataBits); - $writer->writeInt($this->requiredBlockItemStateDataBits, $this->encodeBlockItemState()); - $writer->writeInt($this->requiredBlockOnlyStateDataBits, $this->encodeBlockOnlyState()); + $blockItemBits = $this->requiredBlockItemStateDataBits; + $blockOnlyBits = $this->requiredBlockOnlyStateDataBits; - return $writer->getValue(); + if($blockOnlyBits === 0 && $blockItemBits === 0){ + return 0; + } + + $result = 0; + if($blockItemBits > 0){ + $result |= $this->encodeBlockItemState(); + } + if($blockOnlyBits > 0){ + $result |= $this->encodeBlockOnlyState() << $blockItemBits; + } + + return $result; } /**