mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-01 23:59:53 +00:00
Improved varint write performance (#126)
This commit is contained in:
parent
763ef7f937
commit
1b543b2c16
@ -468,23 +468,19 @@ class Binary{
|
|||||||
return self::writeUnsignedVarInt(($v << 1) ^ ($v >> (PHP_INT_SIZE === 8 ? 63 : 31)));
|
return self::writeUnsignedVarInt(($v << 1) ^ ($v >> (PHP_INT_SIZE === 8 ? 63 : 31)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function writeUnsignedVarInt($v){
|
public static function writeUnsignedVarInt($value){
|
||||||
$buf = "";
|
$buf = "";
|
||||||
$loops = 0;
|
for($i = 0; $i < 10; ++$i){
|
||||||
do{
|
if(($value >> 7) !== 0){
|
||||||
if($loops > 9){
|
$buf .= chr($value | 0x80); //Let chr() take the last byte of this, it's faster than adding another & 0x7f.
|
||||||
throw new \InvalidArgumentException("Varint cannot be longer than 10 bytes!"); //for safety reasons
|
}else{
|
||||||
|
$buf .= chr($value & 0x7f);
|
||||||
|
return $buf;
|
||||||
}
|
}
|
||||||
$w = $v & 0x7f;
|
|
||||||
if(($v >> 7) !== 0){
|
|
||||||
$w = $v | 0x80;
|
|
||||||
}
|
|
||||||
$buf .= self::writeByte($w);
|
|
||||||
$v = (($v >> 7) & (PHP_INT_MAX >> 6)); //PHP really needs a logical right-shift operator
|
|
||||||
++$loops;
|
|
||||||
}while($v);
|
|
||||||
|
|
||||||
return $buf;
|
$value = (($value >> 7) & (PHP_INT_MAX >> 6)); //PHP really needs a logical right-shift operator
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new \InvalidArgumentException("Value too large to be encoded as a varint");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user