Micro-optimizations

This commit is contained in:
Shoghi Cervantes 2014-10-15 10:44:01 +02:00
parent 7b7b91ea0d
commit 9b85abd75e
16 changed files with 63 additions and 68 deletions

View File

@ -1145,9 +1145,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->lastPitch = $to->pitch; $this->lastPitch = $to->pitch;
$ev = new PlayerMoveEvent($this, $from, $to); $ev = new PlayerMoveEvent($this, $from, $to);
if($revert){
$ev->setCancelled();
}
$this->server->getPluginManager()->callEvent($ev); $this->server->getPluginManager()->callEvent($ev);

View File

@ -54,7 +54,7 @@ class DroppedItem extends Entity{
protected function initEntity(){ protected function initEntity(){
$this->namedtag->id = new String("id", "Item"); $this->namedtag->id = new String("id", "Item");
$this->setMaxHealth(5); $this->setMaxHealth(5);
$this->setHealth(@$this->namedtag["Health"]); $this->setHealth($this->namedtag["Health"]);
if(isset($this->namedtag->Age)){ if(isset($this->namedtag->Age)){
$this->age = $this->namedtag["Age"]; $this->age = $this->namedtag["Age"];
} }

View File

@ -211,7 +211,7 @@ abstract class Entity extends Location implements Metadatable{
$this->invulnerable = $this->namedtag["Invulnerable"] > 0 ? true : false; $this->invulnerable = $this->namedtag["Invulnerable"] > 0 ? true : false;
$this->chunk->addEntity($this); $this->chunk->addEntity($this);
$this->getLevel()->addEntity($this); $this->level->addEntity($this);
$this->initEntity(); $this->initEntity();
$this->lastUpdate = $this->server->getTick(); $this->lastUpdate = $this->server->getTick();
$this->server->getPluginManager()->callEvent(new EntitySpawnEvent($this)); $this->server->getPluginManager()->callEvent(new EntitySpawnEvent($this));
@ -384,17 +384,17 @@ abstract class Entity extends Location implements Metadatable{
$diffY = $y - $j; $diffY = $y - $j;
$diffZ = $z - $k; $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; return false;
}else{ }else{
$flag = !$this->getLevel()->isFullBlock(new Vector3($i - 1, $j, $k)); $flag = !$this->level->isFullBlock(new Vector3($i - 1, $j, $k));
$flag1 = !$this->getLevel()->isFullBlock(new Vector3($i + 1, $j, $k)); $flag1 = !$this->level->isFullBlock(new Vector3($i + 1, $j, $k));
//$flag2 = !$this->getLevel()->isFullBlock(new Vector3($i, $j - 1, $k)); //$flag2 = !$this->level->isFullBlock(new Vector3($i, $j - 1, $k));
$flag3 = !$this->getLevel()->isFullBlock(new Vector3($i, $j + 1, $k)); $flag3 = !$this->level->isFullBlock(new Vector3($i, $j + 1, $k));
$flag4 = !$this->getLevel()->isFullBlock(new Vector3($i, $j, $k - 1)); $flag4 = !$this->level->isFullBlock(new Vector3($i, $j, $k - 1));
$flag5 = !$this->getLevel()->isFullBlock(new Vector3($i, $j, $k + 1)); $flag5 = !$this->level->isFullBlock(new Vector3($i, $j, $k + 1));
$direction = 3; //UP! $direction = 3; //UP!
$limit = 9999; $limit = 9999;
@ -703,12 +703,12 @@ abstract class Entity extends Location implements Metadatable{
protected function switchLevel(Level $targetLevel){ protected function switchLevel(Level $targetLevel){
if($this->isValid()){ 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()){ if($ev->isCancelled()){
return false; return false;
} }
$this->getLevel()->removeEntity($this); $this->level->removeEntity($this);
$this->chunk->removeEntity($this); $this->chunk->removeEntity($this);
$this->despawnFromAll(); $this->despawnFromAll();
if($this instanceof Player){ if($this instanceof Player){
@ -716,21 +716,21 @@ abstract class Entity extends Location implements Metadatable{
$X = null; $X = null;
$Z = null; $Z = null;
Level::getXZ($index, $X, $Z); Level::getXZ($index, $X, $Z);
foreach($this->getLevel()->getChunkEntities($X, $Z) as $entity){ foreach($this->level->getChunkEntities($X, $Z) as $entity){
$entity->despawnFrom($this); $entity->despawnFrom($this);
} }
} }
$this->getLevel()->freeAllChunks($this); $this->level->freeAllChunks($this);
} }
} }
$this->setLevel($targetLevel, $this instanceof Player ? true : false); //Hard reference $this->setLevel($targetLevel, $this instanceof Player ? true : false); //Hard reference
$this->getLevel()->addEntity($this); $this->level->addEntity($this);
if($this instanceof Player){ if($this instanceof Player){
$this->usedChunks = []; $this->usedChunks = [];
$pk = new SetTimePacket(); $pk = new SetTimePacket();
$pk->time = $this->getLevel()->getTime(); $pk->time = $this->level->getTime();
$pk->started = $this->getLevel()->stopTime == false; $pk->started = $this->level->stopTime == false;
$this->dataPacket($pk); $this->dataPacket($pk);
} }
$this->chunk = null; $this->chunk = null;
@ -739,15 +739,15 @@ abstract class Entity extends Location implements Metadatable{
} }
public function getPosition(){ 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(){ 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(){ 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){ if($block instanceof Water){
$f = ($pos->y + 1) - ($block->getFluidHeightPercent() - 0.1111111); $f = ($pos->y + 1) - ($block->getFluidHeightPercent() - 0.1111111);
@ -758,7 +758,7 @@ abstract class Entity extends Location implements Metadatable{
} }
public function isInsideOfSolid(){ 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(); $bb = $block->getBoundingBox();
@ -811,7 +811,7 @@ abstract class Entity extends Location implements Metadatable{
/*$sneakFlag = $this->onGround and $this instanceof Player; /*$sneakFlag = $this->onGround and $this instanceof Player;
if($sneakFlag){ 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){ if($dx < $mov and $dx >= -$mov){
$dx = 0; $dx = 0;
}elseif($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){ if($dz < $mov and $dz >= -$mov){
$dz = 0; $dz = 0;
}elseif($dz > 0){ }elseif($dz > 0){
@ -834,7 +834,7 @@ abstract class Entity extends Location implements Metadatable{
//TODO: big messy loop //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){ foreach($list as $bb){
@ -1063,11 +1063,11 @@ abstract class Entity extends Location implements Metadatable{
if($this->chunk instanceof FullChunk){ if($this->chunk instanceof FullChunk){
$this->chunk->removeEntity($this); $this->chunk->removeEntity($this);
} }
$this->getLevel()->loadChunk($this->x >> 4, $this->z >> 4); $this->level->loadChunk($this->x >> 4, $this->z >> 4);
$this->chunk = $this->getLevel()->getChunk($this->x >> 4, $this->z >> 4); $this->chunk = $this->level->getChunk($this->x >> 4, $this->z >> 4);
if(!$this->justCreated){ 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){ foreach($this->hasSpawned as $player){
if(!isset($newChunk[$player->getID()])){ if(!isset($newChunk[$player->getID()])){
$this->despawnFrom($player); $this->despawnFrom($player);
@ -1141,8 +1141,8 @@ abstract class Entity extends Location implements Metadatable{
$yaw = $pos->yaw; $yaw = $pos->yaw;
$pitch = $pos->pitch; $pitch = $pos->pitch;
} }
$from = Position::fromObject($this, $this->getLevel()); $from = Position::fromObject($this, $this->level);
$to = Position::fromObject($pos, $pos instanceof Position ? $pos->getLevel() : $this->getLevel()); $to = Position::fromObject($pos, $pos instanceof Position ? $pos->getLevel() : $this->level);
$this->server->getPluginManager()->callEvent($ev = new EntityTeleportEvent($this, $from, $to)); $this->server->getPluginManager()->callEvent($ev = new EntityTeleportEvent($this, $from, $to));
if($ev->isCancelled()){ if($ev->isCancelled()){
return false; return false;
@ -1166,7 +1166,7 @@ abstract class Entity extends Location implements Metadatable{
} }
public function spawnToAll(){ 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){ if(isset($player->id) and $player->spawned === true){
$this->spawnTo($player); $this->spawnTo($player);
} }
@ -1187,7 +1187,7 @@ abstract class Entity extends Location implements Metadatable{
if($this->chunk instanceof FullChunk){ if($this->chunk instanceof FullChunk){
$this->chunk->removeEntity($this); $this->chunk->removeEntity($this);
} }
if(($level = $this->getLevel()) instanceof Level){ if(($level = $this->level) instanceof Level){
$level->removeEntity($this); $level->removeEntity($this);
} }
$this->despawnFromAll(); $this->despawnFromAll();

View File

@ -370,9 +370,7 @@ class Level implements ChunkManager, Metadatable{
* @return Player[] * @return Player[]
*/ */
public function getUsingChunk($X, $Z){ public function getUsingChunk($X, $Z){
$index = Level::chunkHash($X, $Z); return isset($this->usedChunks[$index = "$X:$Z"]) ? $this->usedChunks[$index] : [];
return isset($this->usedChunks[$index]) ? $this->usedChunks[$index] : [];
} }
/** /**
@ -1839,7 +1837,7 @@ class Level implements ChunkManager, Metadatable{
} }
public function cancelUnloadChunkRequest($x, $z){ 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){ public function unloadChunk($x, $z, $safe = true){

View File

@ -105,7 +105,7 @@ abstract class BaseLevelProvider implements LevelProvider{
"Data" => $this->levelData "Data" => $this->levelData
])); ]));
$buffer = $nbt->writeCompressed(); $buffer = $nbt->writeCompressed();
@file_put_contents($this->getPath() . "level.dat", $buffer); file_put_contents($this->getPath() . "level.dat", $buffer);
} }

View File

@ -98,7 +98,7 @@ class McRegion extends BaseLevelProvider{
"Data" => $levelData "Data" => $levelData
])); ]));
$buffer = $nbt->writeCompressed(); $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){ public static function getRegionIndex($chunkX, $chunkZ, &$x, &$z){

View File

@ -168,6 +168,7 @@ class Enum extends NamedTag implements \ArrayAccess, \Countable{
public function write(NBT $nbt){ public function write(NBT $nbt){
if(!isset($this->tagType)){ if(!isset($this->tagType)){
$id = null;
foreach($this as $tag){ foreach($this as $tag){
if($tag instanceof Tag){ if($tag instanceof Tag){
if(!isset($id)){ if(!isset($id)){
@ -177,7 +178,7 @@ class Enum extends NamedTag implements \ArrayAccess, \Countable{
} }
} }
} }
$this->tagType = @$id; $this->tagType = $id;
} }
$nbt->putByte($this->tagType); $nbt->putByte($this->tagType);

View File

@ -58,7 +58,7 @@ abstract class DataPacket extends \stdClass{
$buffer = ""; $buffer = "";
for(; $len > 0; --$len, ++$this->offset){ for(; $len > 0; --$len, ++$this->offset){
$buffer .= @$this->buffer{$this->offset}; $buffer .= $this->buffer{$this->offset};
} }
return $buffer; return $buffer;

View File

@ -43,8 +43,8 @@ class ExplodePacket extends DataPacket{
$this->putFloat($this->y); $this->putFloat($this->y);
$this->putFloat($this->z); $this->putFloat($this->z);
$this->putFloat($this->radius); $this->putFloat($this->radius);
$this->putInt(@count($this->records)); $this->putInt(count($this->records));
if(@count($this->records) > 0){ if(count($this->records) > 0){
foreach($this->records as $record){ foreach($this->records as $record){
$this->putByte($record->x); $this->putByte($record->x);
$this->putByte($record->y); $this->putByte($record->y);

View File

@ -59,12 +59,12 @@ class RCON{
return; return;
} }
@socket_set_block($this->socket); socket_set_block($this->socket);
for($n = 0; $n < $this->threads; ++$n){ for($n = 0; $n < $this->threads; ++$n){
$this->workers[$n] = new RCONInstance($this->socket, $this->password, $this->clientsPerThread); $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->getLogger()->info("RCON running on $addr:$port");
$this->server->getScheduler()->scheduleRepeatingTask(new CallbackTask([$this, "check"]), 3); $this->server->getScheduler()->scheduleRepeatingTask(new CallbackTask([$this, "check"]), 3);
} }

View File

@ -57,7 +57,7 @@ class RCONInstance extends \Thread{
} }
private function readPacket($client, &$size, &$requestID, &$packetType, &$payload){ private function readPacket($client, &$size, &$requestID, &$packetType, &$payload){
@socket_set_nonblock($client); socket_set_nonblock($client);
$d = socket_read($client, 4); $d = socket_read($client, 4);
if($this->stop === true){ if($this->stop === true){
return false; return false;
@ -66,7 +66,7 @@ class RCONInstance extends \Thread{
}elseif($d === "" or strlen($d) < 4){ }elseif($d === "" or strlen($d) < 4){
return false; return false;
} }
@socket_set_block($client); socket_set_block($client);
$size = Binary::readLInt($d); $size = Binary::readLInt($d);
if($size < 0 or $size > 65535){ if($size < 0 or $size > 65535){
return false; return false;
@ -131,7 +131,7 @@ class RCONInstance extends \Thread{
continue; continue;
} }
if($payload === $this->password){ if($payload === $this->password){
@socket_getpeername($client, $addr, $port); socket_getpeername($client, $addr, $port);
$this->response = "[INFO] Successful Rcon connection from: /$addr:$port"; $this->response = "[INFO] Successful Rcon connection from: /$addr:$port";
$this->synchronized(function (){ $this->synchronized(function (){
$this->wait(); $this->wait();

View File

@ -53,7 +53,7 @@ abstract class AsyncTask extends \Collectable{
* @return mixed * @return mixed
*/ */
public function getResult(){ public function getResult(){
return @unserialize($this->result); return unserialize($this->result);
} }
/** /**
@ -67,7 +67,7 @@ abstract class AsyncTask extends \Collectable{
* @param mixed $result * @param mixed $result
*/ */
public function setResult($result){ public function setResult($result){
$this->result = @serialize($result); $this->result = serialize($result);
} }
public function setTaskId($taskId){ public function setTaskId($taskId){

View File

@ -58,7 +58,7 @@ class AutoUpdater{
protected function check(){ protected function check(){
$response = Utils::getURL($this->endpoint . "?channel=" . $this->getChannel(), 4); $response = Utils::getURL($this->endpoint . "?channel=" . $this->getChannel(), 4);
$response = @json_decode($response, true); $response = json_decode($response, true);
if(!is_array($response)){ if(!is_array($response)){
return; return;
} }

View File

@ -41,7 +41,7 @@ class Binary{
* @return mixed * @return mixed
*/ */
public static function readTriad($str){ 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 * @return int
*/ */
public static function readShort($str, $signed = true){ public static function readShort($str, $signed = true){
$unpacked = @unpack("n", $str)[1]; $unpacked = unpack("n", $str)[1];
if($signed){ if($signed){
if(PHP_INT_SIZE === 8){ if(PHP_INT_SIZE === 8){
@ -269,7 +269,7 @@ class Binary{
* @return int * @return int
*/ */
public static function readLShort($str, $signed = true){ public static function readLShort($str, $signed = true){
$unpacked = @unpack("v", $str)[1]; $unpacked = unpack("v", $str)[1];
if($signed){ if($signed){
if(PHP_INT_SIZE === 8){ if(PHP_INT_SIZE === 8){
@ -295,9 +295,9 @@ class Binary{
public static function readInt($str){ public static function readInt($str){
if(PHP_INT_SIZE === 8){ if(PHP_INT_SIZE === 8){
return @unpack("N", $str)[1] << 32 >> 32; return unpack("N", $str)[1] << 32 >> 32;
}else{ }else{
return @unpack("N", $str)[1]; return unpack("N", $str)[1];
} }
} }
@ -307,9 +307,9 @@ class Binary{
public static function readLInt($str){ public static function readLInt($str){
if(PHP_INT_SIZE === 8){ if(PHP_INT_SIZE === 8){
return @unpack("V", $str)[1] << 32 >> 32; return unpack("V", $str)[1] << 32 >> 32;
}else{ }else{
return @unpack("V", $str)[1]; return unpack("V", $str)[1];
} }
} }
@ -318,7 +318,7 @@ class Binary{
} }
public static function readFloat($str){ 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){ public static function writeFloat($value){
@ -326,7 +326,7 @@ class Binary{
} }
public static function readLFloat($str){ 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){ public static function writeLFloat($value){
@ -338,7 +338,7 @@ class Binary{
} }
public static function readDouble($str){ 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){ public static function writeDouble($value){
@ -346,7 +346,7 @@ class Binary{
} }
public static function readLDouble($str){ 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){ public static function writeLDouble($value){
@ -355,7 +355,7 @@ class Binary{
public static function readLong($x){ public static function readLong($x){
if(PHP_INT_SIZE === 8){ if(PHP_INT_SIZE === 8){
list(, $int1, $int2) = @unpack("N*", $x); list(, $int1, $int2) = unpack("N*", $x);
return ($int1 << 32) | $int2; return ($int1 << 32) | $int2;
}else{ }else{
$value = "0"; $value = "0";

View File

@ -231,7 +231,6 @@ class Utils{
//some entropy, but works ^^ //some entropy, but works ^^
$weakEntropy = [ $weakEntropy = [
is_array($startEntropy) ? implode($startEntropy) : $startEntropy, is_array($startEntropy) ? implode($startEntropy) : $startEntropy,
serialize(@stat(__FILE__)),
__DIR__, __DIR__,
PHP_OS, PHP_OS,
microtime(), microtime(),
@ -253,8 +252,8 @@ class Utils{
(string) getmygid(), (string) getmygid(),
(string) rand(), (string) rand(),
function_exists("zend_thread_id") ? ((string) zend_thread_id()) : microtime(), function_exists("zend_thread_id") ? ((string) zend_thread_id()) : microtime(),
function_exists("getrusage") ? @implode(getrusage()) : microtime(), function_exists("getrusage") ? implode(getrusage()) : microtime(),
function_exists("sys_getloadavg") ? @implode(sys_getloadavg()) : microtime(), function_exists("sys_getloadavg") ? implode(sys_getloadavg()) : microtime(),
serialize(get_loaded_extensions()), serialize(get_loaded_extensions()),
sys_get_temp_dir(), sys_get_temp_dir(),
(string) disk_free_space("."), (string) disk_free_space("."),

@ -1 +1 @@
Subproject commit cc1bb88794ec46782853a86beed0a61ee18f2c90 Subproject commit d5a195a742b2872d5edac82a7dafc00bbe28ee6a