From 9b85abd75e74fe6c35030a60c200fd1570ddf465 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Wed, 15 Oct 2014 10:44:01 +0200 Subject: [PATCH] Micro-optimizations --- src/pocketmine/Player.php | 3 - src/pocketmine/entity/DroppedItem.php | 2 +- src/pocketmine/entity/Entity.php | 60 +++++++++---------- src/pocketmine/level/Level.php | 6 +- .../format/generic/BaseLevelProvider.php | 2 +- .../level/format/mcregion/McRegion.php | 2 +- src/pocketmine/nbt/tag/Enum.php | 3 +- .../network/protocol/DataPacket.php | 2 +- .../network/protocol/ExplodePacket.php | 4 +- src/pocketmine/network/rcon/RCON.php | 4 +- src/pocketmine/network/rcon/RCONInstance.php | 6 +- src/pocketmine/scheduler/AsyncTask.php | 4 +- src/pocketmine/updater/AutoUpdater.php | 2 +- src/pocketmine/utils/Binary.php | 24 ++++---- src/pocketmine/utils/Utils.php | 5 +- src/raklib | 2 +- 16 files changed, 63 insertions(+), 68 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 06a984300..8991e55f1 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -1145,9 +1145,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $this->lastPitch = $to->pitch; $ev = new PlayerMoveEvent($this, $from, $to); - if($revert){ - $ev->setCancelled(); - } $this->server->getPluginManager()->callEvent($ev); diff --git a/src/pocketmine/entity/DroppedItem.php b/src/pocketmine/entity/DroppedItem.php index 93e99366b..0f8367a36 100644 --- a/src/pocketmine/entity/DroppedItem.php +++ b/src/pocketmine/entity/DroppedItem.php @@ -54,7 +54,7 @@ class DroppedItem extends Entity{ protected function initEntity(){ $this->namedtag->id = new String("id", "Item"); $this->setMaxHealth(5); - $this->setHealth(@$this->namedtag["Health"]); + $this->setHealth($this->namedtag["Health"]); if(isset($this->namedtag->Age)){ $this->age = $this->namedtag["Age"]; } diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index a2ca413f5..f3186626e 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -211,7 +211,7 @@ abstract class Entity extends Location implements Metadatable{ $this->invulnerable = $this->namedtag["Invulnerable"] > 0 ? true : false; $this->chunk->addEntity($this); - $this->getLevel()->addEntity($this); + $this->level->addEntity($this); $this->initEntity(); $this->lastUpdate = $this->server->getTick(); $this->server->getPluginManager()->callEvent(new EntitySpawnEvent($this)); @@ -384,17 +384,17 @@ abstract class Entity extends Location implements Metadatable{ $diffY = $y - $j; $diffZ = $z - $k; - $list = $this->getLevel()->getCollisionBlocks($this->boundingBox); + $list = $this->level->getCollisionBlocks($this->boundingBox); - if(count($list) === 0 and !$this->getLevel()->isFullBlock(new Vector3($i, $j, $k))){ + if(count($list) === 0 and !$this->level->isFullBlock(new Vector3($i, $j, $k))){ return false; }else{ - $flag = !$this->getLevel()->isFullBlock(new Vector3($i - 1, $j, $k)); - $flag1 = !$this->getLevel()->isFullBlock(new Vector3($i + 1, $j, $k)); - //$flag2 = !$this->getLevel()->isFullBlock(new Vector3($i, $j - 1, $k)); - $flag3 = !$this->getLevel()->isFullBlock(new Vector3($i, $j + 1, $k)); - $flag4 = !$this->getLevel()->isFullBlock(new Vector3($i, $j, $k - 1)); - $flag5 = !$this->getLevel()->isFullBlock(new Vector3($i, $j, $k + 1)); + $flag = !$this->level->isFullBlock(new Vector3($i - 1, $j, $k)); + $flag1 = !$this->level->isFullBlock(new Vector3($i + 1, $j, $k)); + //$flag2 = !$this->level->isFullBlock(new Vector3($i, $j - 1, $k)); + $flag3 = !$this->level->isFullBlock(new Vector3($i, $j + 1, $k)); + $flag4 = !$this->level->isFullBlock(new Vector3($i, $j, $k - 1)); + $flag5 = !$this->level->isFullBlock(new Vector3($i, $j, $k + 1)); $direction = 3; //UP! $limit = 9999; @@ -703,12 +703,12 @@ abstract class Entity extends Location implements Metadatable{ protected function switchLevel(Level $targetLevel){ if($this->isValid()){ - $this->server->getPluginManager()->callEvent($ev = new EntityLevelChangeEvent($this, $this->getLevel(), $targetLevel)); + $this->server->getPluginManager()->callEvent($ev = new EntityLevelChangeEvent($this, $this->level, $targetLevel)); if($ev->isCancelled()){ return false; } - $this->getLevel()->removeEntity($this); + $this->level->removeEntity($this); $this->chunk->removeEntity($this); $this->despawnFromAll(); if($this instanceof Player){ @@ -716,21 +716,21 @@ abstract class Entity extends Location implements Metadatable{ $X = null; $Z = null; Level::getXZ($index, $X, $Z); - foreach($this->getLevel()->getChunkEntities($X, $Z) as $entity){ + foreach($this->level->getChunkEntities($X, $Z) as $entity){ $entity->despawnFrom($this); } } - $this->getLevel()->freeAllChunks($this); + $this->level->freeAllChunks($this); } } $this->setLevel($targetLevel, $this instanceof Player ? true : false); //Hard reference - $this->getLevel()->addEntity($this); + $this->level->addEntity($this); if($this instanceof Player){ $this->usedChunks = []; $pk = new SetTimePacket(); - $pk->time = $this->getLevel()->getTime(); - $pk->started = $this->getLevel()->stopTime == false; + $pk->time = $this->level->getTime(); + $pk->started = $this->level->stopTime == false; $this->dataPacket($pk); } $this->chunk = null; @@ -739,15 +739,15 @@ abstract class Entity extends Location implements Metadatable{ } public function getPosition(){ - return new Position($this->x, $this->y, $this->z, $this->getLevel()); + return new Position($this->x, $this->y, $this->z, $this->level); } public function getLocation(){ - return new Location($this->x, $this->y, $this->z, $this->yaw, $this->pitch, $this->getLevel()); + return new Location($this->x, $this->y, $this->z, $this->yaw, $this->pitch, $this->level); } public function isInsideOfWater(){ - $block = $this->getLevel()->getBlock($pos = (new Vector3($this->x, $y = ($this->y + $this->getEyeHeight()), $this->z))->floor()); + $block = $this->level->getBlock($pos = (new Vector3($this->x, $y = ($this->y + $this->getEyeHeight()), $this->z))->floor()); if($block instanceof Water){ $f = ($pos->y + 1) - ($block->getFluidHeightPercent() - 0.1111111); @@ -758,7 +758,7 @@ abstract class Entity extends Location implements Metadatable{ } public function isInsideOfSolid(){ - $block = $this->getLevel()->getBlock($pos = (new Vector3($this->x, $y = ($this->y + $this->getEyeHeight()), $this->z))->floor()); + $block = $this->level->getBlock($pos = (new Vector3($this->x, $y = ($this->y + $this->getEyeHeight()), $this->z))->floor()); $bb = $block->getBoundingBox(); @@ -811,7 +811,7 @@ abstract class Entity extends Location implements Metadatable{ /*$sneakFlag = $this->onGround and $this instanceof Player; if($sneakFlag){ - for($mov = 0.05; $dx != 0.0 and count($this->getLevel()->getCollisionCubes($this, $this->boundingBox->getOffsetBoundingBox($dx, -1, 0))) === 0; $movX = $dx){ + for($mov = 0.05; $dx != 0.0 and count($this->level->getCollisionCubes($this, $this->boundingBox->getOffsetBoundingBox($dx, -1, 0))) === 0; $movX = $dx){ if($dx < $mov and $dx >= -$mov){ $dx = 0; }elseif($dx > 0){ @@ -821,7 +821,7 @@ abstract class Entity extends Location implements Metadatable{ } } - for(; $dz != 0.0 and count($this->getLevel()->getCollisionCubes($this, $this->boundingBox->getOffsetBoundingBox(0, -1, $dz))) === 0; $movZ = $dz){ + for(; $dz != 0.0 and count($this->level->getCollisionCubes($this, $this->boundingBox->getOffsetBoundingBox(0, -1, $dz))) === 0; $movZ = $dz){ if($dz < $mov and $dz >= -$mov){ $dz = 0; }elseif($dz > 0){ @@ -834,7 +834,7 @@ abstract class Entity extends Location implements Metadatable{ //TODO: big messy loop }*/ - $list = $this->getLevel()->getCollisionCubes($this, $this->boundingBox->getOffsetBoundingBox($dx, $dy, $dz), false); + $list = $this->level->getCollisionCubes($this, $this->boundingBox->getOffsetBoundingBox($dx, $dy, $dz), false); foreach($list as $bb){ @@ -1063,11 +1063,11 @@ abstract class Entity extends Location implements Metadatable{ if($this->chunk instanceof FullChunk){ $this->chunk->removeEntity($this); } - $this->getLevel()->loadChunk($this->x >> 4, $this->z >> 4); - $this->chunk = $this->getLevel()->getChunk($this->x >> 4, $this->z >> 4); + $this->level->loadChunk($this->x >> 4, $this->z >> 4); + $this->chunk = $this->level->getChunk($this->x >> 4, $this->z >> 4); if(!$this->justCreated){ - $newChunk = $this->getLevel()->getUsingChunk($this->x >> 4, $this->z >> 4); + $newChunk = $this->level->getUsingChunk($this->x >> 4, $this->z >> 4); foreach($this->hasSpawned as $player){ if(!isset($newChunk[$player->getID()])){ $this->despawnFrom($player); @@ -1141,8 +1141,8 @@ abstract class Entity extends Location implements Metadatable{ $yaw = $pos->yaw; $pitch = $pos->pitch; } - $from = Position::fromObject($this, $this->getLevel()); - $to = Position::fromObject($pos, $pos instanceof Position ? $pos->getLevel() : $this->getLevel()); + $from = Position::fromObject($this, $this->level); + $to = Position::fromObject($pos, $pos instanceof Position ? $pos->getLevel() : $this->level); $this->server->getPluginManager()->callEvent($ev = new EntityTeleportEvent($this, $from, $to)); if($ev->isCancelled()){ return false; @@ -1166,7 +1166,7 @@ abstract class Entity extends Location implements Metadatable{ } public function spawnToAll(){ - foreach($this->getLevel()->getUsingChunk($this->x >> 4, $this->z >> 4) as $player){ + foreach($this->level->getUsingChunk($this->x >> 4, $this->z >> 4) as $player){ if(isset($player->id) and $player->spawned === true){ $this->spawnTo($player); } @@ -1187,7 +1187,7 @@ abstract class Entity extends Location implements Metadatable{ if($this->chunk instanceof FullChunk){ $this->chunk->removeEntity($this); } - if(($level = $this->getLevel()) instanceof Level){ + if(($level = $this->level) instanceof Level){ $level->removeEntity($this); } $this->despawnFromAll(); diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index c3867cf32..968eba58d 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -370,9 +370,7 @@ class Level implements ChunkManager, Metadatable{ * @return Player[] */ public function getUsingChunk($X, $Z){ - $index = Level::chunkHash($X, $Z); - - return isset($this->usedChunks[$index]) ? $this->usedChunks[$index] : []; + return isset($this->usedChunks[$index = "$X:$Z"]) ? $this->usedChunks[$index] : []; } /** @@ -1839,7 +1837,7 @@ class Level implements ChunkManager, Metadatable{ } public function cancelUnloadChunkRequest($x, $z){ - unset($this->unloadQueue[static::chunkHash($x, $z)]); + unset($this->unloadQueue[Level::chunkHash($x, $z)]); } public function unloadChunk($x, $z, $safe = true){ diff --git a/src/pocketmine/level/format/generic/BaseLevelProvider.php b/src/pocketmine/level/format/generic/BaseLevelProvider.php index 418b7339e..b393c26c6 100644 --- a/src/pocketmine/level/format/generic/BaseLevelProvider.php +++ b/src/pocketmine/level/format/generic/BaseLevelProvider.php @@ -105,7 +105,7 @@ abstract class BaseLevelProvider implements LevelProvider{ "Data" => $this->levelData ])); $buffer = $nbt->writeCompressed(); - @file_put_contents($this->getPath() . "level.dat", $buffer); + file_put_contents($this->getPath() . "level.dat", $buffer); } diff --git a/src/pocketmine/level/format/mcregion/McRegion.php b/src/pocketmine/level/format/mcregion/McRegion.php index 7ebb0ae70..ec700a056 100644 --- a/src/pocketmine/level/format/mcregion/McRegion.php +++ b/src/pocketmine/level/format/mcregion/McRegion.php @@ -98,7 +98,7 @@ class McRegion extends BaseLevelProvider{ "Data" => $levelData ])); $buffer = $nbt->writeCompressed(); - @file_put_contents($path . "level.dat", $buffer); + file_put_contents($path . "level.dat", $buffer); } public static function getRegionIndex($chunkX, $chunkZ, &$x, &$z){ diff --git a/src/pocketmine/nbt/tag/Enum.php b/src/pocketmine/nbt/tag/Enum.php index d18364d48..cd5d28586 100644 --- a/src/pocketmine/nbt/tag/Enum.php +++ b/src/pocketmine/nbt/tag/Enum.php @@ -168,6 +168,7 @@ class Enum extends NamedTag implements \ArrayAccess, \Countable{ public function write(NBT $nbt){ if(!isset($this->tagType)){ + $id = null; foreach($this as $tag){ if($tag instanceof Tag){ if(!isset($id)){ @@ -177,7 +178,7 @@ class Enum extends NamedTag implements \ArrayAccess, \Countable{ } } } - $this->tagType = @$id; + $this->tagType = $id; } $nbt->putByte($this->tagType); diff --git a/src/pocketmine/network/protocol/DataPacket.php b/src/pocketmine/network/protocol/DataPacket.php index 94cf586d0..3a13eebcd 100644 --- a/src/pocketmine/network/protocol/DataPacket.php +++ b/src/pocketmine/network/protocol/DataPacket.php @@ -58,7 +58,7 @@ abstract class DataPacket extends \stdClass{ $buffer = ""; for(; $len > 0; --$len, ++$this->offset){ - $buffer .= @$this->buffer{$this->offset}; + $buffer .= $this->buffer{$this->offset}; } return $buffer; diff --git a/src/pocketmine/network/protocol/ExplodePacket.php b/src/pocketmine/network/protocol/ExplodePacket.php index 7363ff4d2..410c9a9d2 100644 --- a/src/pocketmine/network/protocol/ExplodePacket.php +++ b/src/pocketmine/network/protocol/ExplodePacket.php @@ -43,8 +43,8 @@ class ExplodePacket extends DataPacket{ $this->putFloat($this->y); $this->putFloat($this->z); $this->putFloat($this->radius); - $this->putInt(@count($this->records)); - if(@count($this->records) > 0){ + $this->putInt(count($this->records)); + if(count($this->records) > 0){ foreach($this->records as $record){ $this->putByte($record->x); $this->putByte($record->y); diff --git a/src/pocketmine/network/rcon/RCON.php b/src/pocketmine/network/rcon/RCON.php index 69b088525..60a24aab6 100644 --- a/src/pocketmine/network/rcon/RCON.php +++ b/src/pocketmine/network/rcon/RCON.php @@ -59,12 +59,12 @@ class RCON{ return; } - @socket_set_block($this->socket); + socket_set_block($this->socket); for($n = 0; $n < $this->threads; ++$n){ $this->workers[$n] = new RCONInstance($this->socket, $this->password, $this->clientsPerThread); } - @socket_getsockname($this->socket, $addr, $port); + socket_getsockname($this->socket, $addr, $port); $this->server->getLogger()->info("RCON running on $addr:$port"); $this->server->getScheduler()->scheduleRepeatingTask(new CallbackTask([$this, "check"]), 3); } diff --git a/src/pocketmine/network/rcon/RCONInstance.php b/src/pocketmine/network/rcon/RCONInstance.php index 90f31e5c9..af6436666 100644 --- a/src/pocketmine/network/rcon/RCONInstance.php +++ b/src/pocketmine/network/rcon/RCONInstance.php @@ -57,7 +57,7 @@ class RCONInstance extends \Thread{ } private function readPacket($client, &$size, &$requestID, &$packetType, &$payload){ - @socket_set_nonblock($client); + socket_set_nonblock($client); $d = socket_read($client, 4); if($this->stop === true){ return false; @@ -66,7 +66,7 @@ class RCONInstance extends \Thread{ }elseif($d === "" or strlen($d) < 4){ return false; } - @socket_set_block($client); + socket_set_block($client); $size = Binary::readLInt($d); if($size < 0 or $size > 65535){ return false; @@ -131,7 +131,7 @@ class RCONInstance extends \Thread{ continue; } if($payload === $this->password){ - @socket_getpeername($client, $addr, $port); + socket_getpeername($client, $addr, $port); $this->response = "[INFO] Successful Rcon connection from: /$addr:$port"; $this->synchronized(function (){ $this->wait(); diff --git a/src/pocketmine/scheduler/AsyncTask.php b/src/pocketmine/scheduler/AsyncTask.php index fe8e8c29a..4c6341c4d 100644 --- a/src/pocketmine/scheduler/AsyncTask.php +++ b/src/pocketmine/scheduler/AsyncTask.php @@ -53,7 +53,7 @@ abstract class AsyncTask extends \Collectable{ * @return mixed */ public function getResult(){ - return @unserialize($this->result); + return unserialize($this->result); } /** @@ -67,7 +67,7 @@ abstract class AsyncTask extends \Collectable{ * @param mixed $result */ public function setResult($result){ - $this->result = @serialize($result); + $this->result = serialize($result); } public function setTaskId($taskId){ diff --git a/src/pocketmine/updater/AutoUpdater.php b/src/pocketmine/updater/AutoUpdater.php index 180dd1ad8..dbb85b905 100644 --- a/src/pocketmine/updater/AutoUpdater.php +++ b/src/pocketmine/updater/AutoUpdater.php @@ -58,7 +58,7 @@ class AutoUpdater{ protected function check(){ $response = Utils::getURL($this->endpoint . "?channel=" . $this->getChannel(), 4); - $response = @json_decode($response, true); + $response = json_decode($response, true); if(!is_array($response)){ return; } diff --git a/src/pocketmine/utils/Binary.php b/src/pocketmine/utils/Binary.php index 7248db4b3..528d614df 100644 --- a/src/pocketmine/utils/Binary.php +++ b/src/pocketmine/utils/Binary.php @@ -41,7 +41,7 @@ class Binary{ * @return mixed */ public static function readTriad($str){ - return @unpack("N", "\x00" . $str)[1]; + return unpack("N", "\x00" . $str)[1]; } /** @@ -236,7 +236,7 @@ class Binary{ * @return int */ public static function readShort($str, $signed = true){ - $unpacked = @unpack("n", $str)[1]; + $unpacked = unpack("n", $str)[1]; if($signed){ if(PHP_INT_SIZE === 8){ @@ -269,7 +269,7 @@ class Binary{ * @return int */ public static function readLShort($str, $signed = true){ - $unpacked = @unpack("v", $str)[1]; + $unpacked = unpack("v", $str)[1]; if($signed){ if(PHP_INT_SIZE === 8){ @@ -295,9 +295,9 @@ class Binary{ public static function readInt($str){ if(PHP_INT_SIZE === 8){ - return @unpack("N", $str)[1] << 32 >> 32; + return unpack("N", $str)[1] << 32 >> 32; }else{ - return @unpack("N", $str)[1]; + return unpack("N", $str)[1]; } } @@ -307,9 +307,9 @@ class Binary{ public static function readLInt($str){ if(PHP_INT_SIZE === 8){ - return @unpack("V", $str)[1] << 32 >> 32; + return unpack("V", $str)[1] << 32 >> 32; }else{ - return @unpack("V", $str)[1]; + return unpack("V", $str)[1]; } } @@ -318,7 +318,7 @@ class Binary{ } public static function readFloat($str){ - return ENDIANNESS === self::BIG_ENDIAN ? @unpack("f", $str)[1] : @unpack("f", strrev($str))[1]; + return ENDIANNESS === self::BIG_ENDIAN ? unpack("f", $str)[1] : unpack("f", strrev($str))[1]; } public static function writeFloat($value){ @@ -326,7 +326,7 @@ class Binary{ } public static function readLFloat($str){ - return ENDIANNESS === self::BIG_ENDIAN ? @unpack("f", strrev($str))[1] : @unpack("f", $str)[1]; + return ENDIANNESS === self::BIG_ENDIAN ? unpack("f", strrev($str))[1] : unpack("f", $str)[1]; } public static function writeLFloat($value){ @@ -338,7 +338,7 @@ class Binary{ } public static function readDouble($str){ - return ENDIANNESS === self::BIG_ENDIAN ? @unpack("d", $str)[1] : @unpack("d", strrev($str))[1]; + return ENDIANNESS === self::BIG_ENDIAN ? unpack("d", $str)[1] : unpack("d", strrev($str))[1]; } public static function writeDouble($value){ @@ -346,7 +346,7 @@ class Binary{ } public static function readLDouble($str){ - return ENDIANNESS === self::BIG_ENDIAN ? @unpack("d", strrev($str))[1] : @unpack("d", $str)[1]; + return ENDIANNESS === self::BIG_ENDIAN ? unpack("d", strrev($str))[1] : unpack("d", $str)[1]; } public static function writeLDouble($value){ @@ -355,7 +355,7 @@ class Binary{ public static function readLong($x){ if(PHP_INT_SIZE === 8){ - list(, $int1, $int2) = @unpack("N*", $x); + list(, $int1, $int2) = unpack("N*", $x); return ($int1 << 32) | $int2; }else{ $value = "0"; diff --git a/src/pocketmine/utils/Utils.php b/src/pocketmine/utils/Utils.php index 51ba74b73..c357da95d 100644 --- a/src/pocketmine/utils/Utils.php +++ b/src/pocketmine/utils/Utils.php @@ -231,7 +231,6 @@ class Utils{ //some entropy, but works ^^ $weakEntropy = [ is_array($startEntropy) ? implode($startEntropy) : $startEntropy, - serialize(@stat(__FILE__)), __DIR__, PHP_OS, microtime(), @@ -253,8 +252,8 @@ class Utils{ (string) getmygid(), (string) rand(), function_exists("zend_thread_id") ? ((string) zend_thread_id()) : microtime(), - function_exists("getrusage") ? @implode(getrusage()) : microtime(), - function_exists("sys_getloadavg") ? @implode(sys_getloadavg()) : microtime(), + function_exists("getrusage") ? implode(getrusage()) : microtime(), + function_exists("sys_getloadavg") ? implode(sys_getloadavg()) : microtime(), serialize(get_loaded_extensions()), sys_get_temp_dir(), (string) disk_free_space("."), diff --git a/src/raklib b/src/raklib index cc1bb8879..d5a195a74 160000 --- a/src/raklib +++ b/src/raklib @@ -1 +1 @@ -Subproject commit cc1bb88794ec46782853a86beed0a61ee18f2c90 +Subproject commit d5a195a742b2872d5edac82a7dafc00bbe28ee6a