From 84ce5f1c73dac9dc35cf37628c8fc6549fed34cf Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Sun, 24 Aug 2014 13:59:37 +0200 Subject: [PATCH] Performance improvements in NBT reading/writing --- src/pocketmine/nbt/NBT.php | 4 ++-- src/pocketmine/nbt/tag/IntArray.php | 30 +++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/pocketmine/nbt/NBT.php b/src/pocketmine/nbt/NBT.php index 8efee6981..b4d2be5a9 100644 --- a/src/pocketmine/nbt/NBT.php +++ b/src/pocketmine/nbt/NBT.php @@ -62,7 +62,7 @@ class NBT{ private $buffer; private $offset; - private $endianness; + public $endianness; private $data; public function get($len){ @@ -73,7 +73,7 @@ class NBT{ }elseif($len === true){ return substr($this->buffer, $this->offset); } - if($len > 1024){ + if($len > 16){ return substr($this->buffer, ($this->offset += $len) - $len, $len); } $buffer = ""; diff --git a/src/pocketmine/nbt/tag/IntArray.php b/src/pocketmine/nbt/tag/IntArray.php index 11c11ba3a..bc9e8bd3f 100644 --- a/src/pocketmine/nbt/tag/IntArray.php +++ b/src/pocketmine/nbt/tag/IntArray.php @@ -22,6 +22,7 @@ namespace pocketmine\nbt\tag; use pocketmine\nbt\NBT; +use pocketmine\utils\Binary; class IntArray extends NamedTag{ @@ -32,15 +33,36 @@ class IntArray extends NamedTag{ public function read(NBT $nbt){ $this->value = []; $size = $nbt->getInt(); - for($i = 0; $i < $size and !$nbt->feof(); ++$i){ - $this->value[] = $nbt->getInt(); + $ints = $nbt->get($size * 4); + $offset = 0; + if($nbt->endianness === NBT::LITTLE_ENDIAN){ + for($i = 0; $i < $size and isset($ints{$offset}); ++$i){ + $this->value[$i] = Binary::readLInt(substr($ints, $offset, 4)); + $offset += 4; + } + }else{ + for($i = 0; $i < $size and isset($ints{$offset}); ++$i){ + $this->value[$i] = Binary::readInt(substr($ints, $offset, 4)); + $offset += 4; + } } + } public function write(NBT $nbt){ $nbt->putInt(count($this->value)); - foreach($this->value as $v){ - $nbt->putInt($v); + + $ints = ""; + if($nbt->endianness === NBT::LITTLE_ENDIAN){ + foreach($this->value as $v){ + $ints .= Binary::writeLInt($v); + } + }else{ + foreach($this->value as $v){ + $ints .= Binary::writeInt($v); + } } + + $nbt->put($ints); } } \ No newline at end of file