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.
This commit is contained in:
Dylan K. Taylor 2023-11-06 17:04:39 +00:00
parent 457660235e
commit bbe66e8e09
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -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;
}
/**