Performance improvements in blocks and Entities

This commit is contained in:
Shoghi Cervantes 2014-08-24 14:08:17 +02:00
parent 84ce5f1c73
commit 01ebe74974
9 changed files with 145 additions and 147 deletions

View File

@ -687,7 +687,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$chunkZ = $Z + $centerZ; $chunkZ = $Z + $centerZ;
$index = Level::chunkHash($chunkX, $chunkZ); $index = Level::chunkHash($chunkX, $chunkZ);
if(!isset($this->usedChunks[$index])){ if(!isset($this->usedChunks[$index])){
if($this->getLevel()->isChunkPopulated($chunkX, $chunkZ)){ if($this->level->isChunkPopulated($chunkX, $chunkZ)){
$newOrder[$index] = $distance; $newOrder[$index] = $distance;
}else{ }else{
$generateQueue->insert([$chunkX, $chunkZ], $distance); $generateQueue->insert([$chunkX, $chunkZ], $distance);

View File

@ -527,142 +527,142 @@ abstract class Block extends Position implements Metadatable{
public static function init(){ public static function init(){
if(count(self::$list) === 0){ if(count(self::$list) === 0){
self::$list = array( self::$list = array(
self::AIR => new Air(), self::AIR => Air::class,
self::STONE => new Stone(), self::STONE => Stone::class,
self::GRASS => new Grass(), self::GRASS => Grass::class,
self::DIRT => new Dirt(), self::DIRT => Dirt::class,
self::COBBLESTONE => new Cobblestone(), self::COBBLESTONE => Cobblestone::class,
self::PLANKS => new Planks(), self::PLANKS => Planks::class,
self::SAPLING => new Sapling(), self::SAPLING => Sapling::class,
self::BEDROCK => new Bedrock(), self::BEDROCK => Bedrock::class,
self::WATER => new Water(), self::WATER => Water::class,
self::STILL_WATER => new StillWater(), self::STILL_WATER => StillWater::class,
self::LAVA => new Lava(), self::LAVA => Lava::class,
self::STILL_LAVA => new StillLava(), self::STILL_LAVA => StillLava::class,
self::SAND => new Sand(), self::SAND => Sand::class,
self::GRAVEL => new Gravel(), self::GRAVEL => Gravel::class,
self::GOLD_ORE => new GoldOre(), self::GOLD_ORE => GoldOre::class,
self::IRON_ORE => new IronOre(), self::IRON_ORE => IronOre::class,
self::COAL_ORE => new CoalOre(), self::COAL_ORE => CoalOre::class,
self::WOOD => new Wood(), self::WOOD => Wood::class,
self::LEAVES => new Leaves(), self::LEAVES => Leaves::class,
self::SPONGE => new Sponge(), self::SPONGE => Sponge::class,
self::GLASS => new Glass(), self::GLASS => Glass::class,
self::LAPIS_ORE => new LapisOre(), self::LAPIS_ORE => LapisOre::class,
self::LAPIS_BLOCK => new Lapis(), self::LAPIS_BLOCK => Lapis::class,
self::SANDSTONE => new Sandstone(), self::SANDSTONE => Sandstone::class,
self::BED_BLOCK => new Bed(), self::BED_BLOCK => Bed::class,
self::COBWEB => new Cobweb(), self::COBWEB => Cobweb::class,
self::TALL_GRASS => new TallGrass(), self::TALL_GRASS => TallGrass::class,
self::DEAD_BUSH => new DeadBush(), self::DEAD_BUSH => DeadBush::class,
self::WOOL => new Wool(), self::WOOL => Wool::class,
self::DANDELION => new Dandelion(), self::DANDELION => Dandelion::class,
self::POPPY => new CyanFlower(), self::POPPY => CyanFlower::class,
self::BROWN_MUSHROOM => new BrownMushroom(), self::BROWN_MUSHROOM => BrownMushroom::class,
self::RED_MUSHROOM => new RedMushroom(), self::RED_MUSHROOM => RedMushroom::class,
self::GOLD_BLOCK => new Gold(), self::GOLD_BLOCK => Gold::class,
self::IRON_BLOCK => new Iron(), self::IRON_BLOCK => Iron::class,
self::DOUBLE_SLAB => new DoubleSlab(), self::DOUBLE_SLAB => DoubleSlab::class,
self::SLAB => new Slab(), self::SLAB => Slab::class,
self::BRICKS_BLOCK => new Bricks(), self::BRICKS_BLOCK => Bricks::class,
self::TNT => new TNT(), self::TNT => TNT::class,
self::BOOKSHELF => new Bookshelf(), self::BOOKSHELF => Bookshelf::class,
self::MOSS_STONE => new MossStone(), self::MOSS_STONE => MossStone::class,
self::OBSIDIAN => new Obsidian(), self::OBSIDIAN => Obsidian::class,
self::TORCH => new Torch(), self::TORCH => Torch::class,
self::FIRE => new Fire(), self::FIRE => Fire::class,
self::MONSTER_SPAWNER => new MonsterSpawner(), self::MONSTER_SPAWNER => MonsterSpawner::class,
self::WOOD_STAIRS => new WoodStairs(), self::WOOD_STAIRS => WoodStairs::class,
self::CHEST => new Chest(), self::CHEST => Chest::class,
self::DIAMOND_ORE => new DiamondOre(), self::DIAMOND_ORE => DiamondOre::class,
self::DIAMOND_BLOCK => new Diamond(), self::DIAMOND_BLOCK => Diamond::class,
self::WORKBENCH => new Workbench(), self::WORKBENCH => Workbench::class,
self::WHEAT_BLOCK => new Wheat(), self::WHEAT_BLOCK => Wheat::class,
self::FARMLAND => new Farmland(), self::FARMLAND => Farmland::class,
self::FURNACE => new Furnace(), self::FURNACE => Furnace::class,
self::BURNING_FURNACE => new BurningFurnace(), self::BURNING_FURNACE => BurningFurnace::class,
self::SIGN_POST => new SignPost(), self::SIGN_POST => SignPost::class,
self::WOOD_DOOR_BLOCK => new WoodDoor(), self::WOOD_DOOR_BLOCK => WoodDoor::class,
self::LADDER => new Ladder(), self::LADDER => Ladder::class,
self::COBBLESTONE_STAIRS => new CobblestoneStairs(), self::COBBLESTONE_STAIRS => CobblestoneStairs::class,
self::WALL_SIGN => new WallSign(), self::WALL_SIGN => WallSign::class,
self::IRON_DOOR_BLOCK => new IronDoor(), self::IRON_DOOR_BLOCK => IronDoor::class,
self::REDSTONE_ORE => new RedstoneOre(), self::REDSTONE_ORE => RedstoneOre::class,
self::GLOWING_REDSTONE_ORE => new GlowingRedstoneOre(), self::GLOWING_REDSTONE_ORE => GlowingRedstoneOre::class,
self::SNOW_LAYER => new SnowLayer(), self::SNOW_LAYER => SnowLayer::class,
self::ICE => new Ice(), self::ICE => Ice::class,
self::SNOW_BLOCK => new Snow(), self::SNOW_BLOCK => Snow::class,
self::CACTUS => new Cactus(), self::CACTUS => Cactus::class,
self::CLAY_BLOCK => new Clay(), self::CLAY_BLOCK => Clay::class,
self::SUGARCANE_BLOCK => new Sugarcane(), self::SUGARCANE_BLOCK => Sugarcane::class,
self::FENCE => new Fence(), self::FENCE => Fence::class,
self::PUMPKIN => new Pumpkin(), self::PUMPKIN => Pumpkin::class,
self::NETHERRACK => new Netherrack(), self::NETHERRACK => Netherrack::class,
self::SOUL_SAND => new SoulSand(), self::SOUL_SAND => SoulSand::class,
self::GLOWSTONE_BLOCK => new Glowstone(), self::GLOWSTONE_BLOCK => Glowstone::class,
self::LIT_PUMPKIN => new LitPumpkin(), self::LIT_PUMPKIN => LitPumpkin::class,
self::CAKE_BLOCK => new Cake(), self::CAKE_BLOCK => Cake::class,
self::TRAPDOOR => new Trapdoor(), self::TRAPDOOR => Trapdoor::class,
self::STONE_BRICKS => new StoneBricks(), self::STONE_BRICKS => StoneBricks::class,
self::IRON_BARS => new IronBars(), self::IRON_BARS => IronBars::class,
self::GLASS_PANE => new GlassPane(), self::GLASS_PANE => GlassPane::class,
self::MELON_BLOCK => new Melon(), self::MELON_BLOCK => Melon::class,
self::PUMPKIN_STEM => new PumpkinStem(), self::PUMPKIN_STEM => PumpkinStem::class,
self::MELON_STEM => new MelonStem(), self::MELON_STEM => MelonStem::class,
self::FENCE_GATE => new FenceGate(), self::FENCE_GATE => FenceGate::class,
self::BRICK_STAIRS => new BrickStairs(), self::BRICK_STAIRS => BrickStairs::class,
self::STONE_BRICK_STAIRS => new StoneBrickStairs(), self::STONE_BRICK_STAIRS => StoneBrickStairs::class,
self::MYCELIUM => new Mycelium(), self::MYCELIUM => Mycelium::class,
self::NETHER_BRICKS => new NetherBrick(), self::NETHER_BRICKS => NetherBrick::class,
self::NETHER_BRICKS_STAIRS => new NetherBrickStairs(), self::NETHER_BRICKS_STAIRS => NetherBrickStairs::class,
self::END_PORTAL => new EndPortal(), self::END_PORTAL => EndPortal::class,
self::END_STONE => new EndStone(), self::END_STONE => EndStone::class,
self::SANDSTONE_STAIRS => new SandstoneStairs(), self::SANDSTONE_STAIRS => SandstoneStairs::class,
self::EMERALD_ORE => new EmeraldOre(), self::EMERALD_ORE => EmeraldOre::class,
self::EMERALD_BLOCK => new Emerald(), self::EMERALD_BLOCK => Emerald::class,
self::SPRUCE_WOOD_STAIRS => new SpruceWoodStairs(), self::SPRUCE_WOOD_STAIRS => SpruceWoodStairs::class,
self::BIRCH_WOOD_STAIRS => new BirchWoodStairs(), self::BIRCH_WOOD_STAIRS => BirchWoodStairs::class,
self::JUNGLE_WOOD_STAIRS => new JungleWoodStairs(), self::JUNGLE_WOOD_STAIRS => JungleWoodStairs::class,
self::STONE_WALL => new StoneWall(), self::STONE_WALL => StoneWall::class,
self::CARROT_BLOCK => new Carrot(), self::CARROT_BLOCK => Carrot::class,
self::POTATO_BLOCK => new Potato(), self::POTATO_BLOCK => Potato::class,
self::QUARTZ_BLOCK => new Quartz(), self::QUARTZ_BLOCK => Quartz::class,
self::QUARTZ_STAIRS => new QuartzStairs(), self::QUARTZ_STAIRS => QuartzStairs::class,
self::DOUBLE_WOOD_SLAB => new DoubleWoodSlab(), self::DOUBLE_WOOD_SLAB => DoubleWoodSlab::class,
self::WOOD_SLAB => new WoodSlab(), self::WOOD_SLAB => WoodSlab::class,
self::STAINED_CLAY => new StainedClay(), self::STAINED_CLAY => StainedClay::class,
self::LEAVES2 => new Leaves2(), self::LEAVES2 => Leaves2::class,
self::WOOD2 => new Wood2(), self::WOOD2 => Wood2::class,
self::ACACIA_WOOD_STAIRS => new AcaciaWoodStairs(), self::ACACIA_WOOD_STAIRS => AcaciaWoodStairs::class,
self::DARK_OAK_WOOD_STAIRS => new DarkOakWoodStairs(), self::DARK_OAK_WOOD_STAIRS => DarkOakWoodStairs::class,
self::HAY_BALE => new HayBale(), self::HAY_BALE => HayBale::class,
self::CARPET => new Carpet(), self::CARPET => Carpet::class,
self::HARDENED_CLAY => new HardenedClay(), self::HARDENED_CLAY => HardenedClay::class,
self::COAL_BLOCK => new Coal(), self::COAL_BLOCK => Coal::class,
self::PODZOL => new Podzol(), self::PODZOL => Podzol::class,
self::BEETROOT_BLOCK => new Beetroot(), self::BEETROOT_BLOCK => Beetroot::class,
self::STONECUTTER => new Stonecutter(), self::STONECUTTER => Stonecutter::class,
self::GLOWING_OBSIDIAN => new GlowingObsidian(), self::GLOWING_OBSIDIAN => GlowingObsidian::class,
self::NETHER_REACTOR => new NetherReactor(), self::NETHER_REACTOR => NetherReactor::class,
); );
} }
} }
@ -676,11 +676,12 @@ abstract class Block extends Position implements Metadatable{
*/ */
public static function get($id, $meta = 0, Position $pos = null){ public static function get($id, $meta = 0, Position $pos = null){
if(isset(self::$list[$id])){ if(isset(self::$list[$id])){
$block = clone self::$list[$id]; $block = self::$list[$id];
$block->setDamage($meta); $block = new $block($meta);
}else{ }else{
$block = new Generic($id, $meta); $block = new Generic($id, $meta);
} }
if($pos instanceof Position){ if($pos instanceof Position){
$block->position($pos); $block->position($pos);
} }

View File

@ -1055,7 +1055,7 @@ abstract class Entity extends Position implements Metadatable{
$level->removeEntity($this); $level->removeEntity($this);
} }
$this->despawnFromAll(); $this->despawnFromAll();
$this->level->release(); unset($this->level);
} }
} }

View File

@ -449,7 +449,7 @@ class Item{
self::BOW => new Bow(), self::BOW => new Bow(),
); );
foreach(Block::$list as $id => $class){ foreach(Block::$list as $id => $class){
self::$list[$id] = new ItemBlock($class); self::$list[$id] = new ItemBlock(new $class);
} }
} }

View File

@ -676,7 +676,6 @@ class Level implements ChunkManager, Metadatable{
} }
} }
//TODO: fix this
foreach($this->getCollidingEntities($bb->expand(0.25, 0.25, 0.25), $entity) as $ent){ foreach($this->getCollidingEntities($bb->expand(0.25, 0.25, 0.25), $entity) as $ent){
$collides[] = clone $ent->boundingBox; $collides[] = clone $ent->boundingBox;
} }
@ -1368,7 +1367,8 @@ class Level implements ChunkManager, Metadatable{
* @return bool * @return bool
*/ */
public function isChunkGenerated($x, $z){ public function isChunkGenerated($x, $z){
return $this->provider->isChunkGenerated($x, $z); $chunk = $this->getChunkAt($x, $z);
return $chunk instanceof FullChunk ? $chunk->isGenerated() : false;
} }
/** /**
@ -1378,7 +1378,8 @@ class Level implements ChunkManager, Metadatable{
* @return bool * @return bool
*/ */
public function isChunkPopulated($x, $z){ public function isChunkPopulated($x, $z){
return $this->provider->isChunkPopulated($x, $z); $chunk = $this->getChunkAt($x, $z);
return $chunk instanceof FullChunk ? $chunk->isPopulated() : false;
} }
/** /**

View File

@ -25,7 +25,7 @@ use pocketmine\math\Vector3 as Vector3;
class Position extends Vector3{ class Position extends Vector3{
/** @var \WeakRef<Level> */ /** @var Level */
public $level = null; public $level = null;
/** /**
@ -35,32 +35,26 @@ class Position extends Vector3{
* @param Level $level * @param Level $level
* @param bool $strong * @param bool $strong
*/ */
public function __construct($x = 0, $y = 0, $z = 0, Level $level, $strong = false){ public function __construct($x = 0, $y = 0, $z = 0, Level $level = null, $strong = false){
$this->x = $x; $this->x = $x;
$this->y = $y; $this->y = $y;
$this->z = $z; $this->z = $z;
$this->level = new \WeakRef($level); $this->level = $level;
if($strong === true){
$this->level->acquire();
}
} }
public static function fromObject(Vector3 $pos, Level $level, $strong = false){ public static function fromObject(Vector3 $pos, Level $level = null, $strong = false){
return new Position($pos->x, $pos->y, $pos->z, $level, $strong); return new Position($pos->x, $pos->y, $pos->z, $level);
} }
/** /**
* @return Level * @return Level
*/ */
public function getLevel(){ public function getLevel(){
return $this->level->get(); return $this->level;
} }
public function setLevel(Level $level, $strong = false){ public function setLevel(Level $level, $strong = false){
$this->level = new \WeakRef($level); $this->level = $level;
if($strong === true){
$this->level->acquire();
}
} }
/** /**
@ -69,27 +63,31 @@ class Position extends Vector3{
* @return bool * @return bool
*/ */
public function isValid(){ public function isValid(){
return isset($this->level) and $this->level->valid(); return isset($this->level) and $this->level instanceof Level;
} }
/** /**
* Marks the level reference as strong so it won't be collected * Marks the level reference as strong so it won't be collected
* by the garbage collector. * by the garbage collector.
* *
* @deprecated
*
* @return bool * @return bool
*/ */
public function setStrong(){ public function setStrong(){
return $this->level->acquire(); return false;
} }
/** /**
* Marks the level reference as weak so it won't have effect against * Marks the level reference as weak so it won't have effect against
* the garbage collector decision. * the garbage collector decision.
* *
* @deprecated
*
* @return bool * @return bool
*/ */
public function setWeak(){ public function setWeak(){
return $this->level->release(); return false;
} }
/** /**
@ -107,7 +105,7 @@ class Position extends Vector3{
throw new \RuntimeException("Undefined Level reference"); throw new \RuntimeException("Undefined Level reference");
} }
return Position::fromObject(parent::getSide($side, $step), $this->getLevel()); return Position::fromObject(parent::getSide($side, $step), $this->level);
} }
/** /**

View File

@ -51,7 +51,7 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{
/** @var int[256] */ /** @var int[256] */
protected $biomeColors; protected $biomeColors;
/** @var \WeakRef<LevelProvider> */ /** @var LevelProvider */
protected $level; protected $level;
protected $x; protected $x;

View File

@ -117,7 +117,7 @@ abstract class Tile extends Position{
if(($level = $this->getLevel()) instanceof Level){ if(($level = $this->getLevel()) instanceof Level){
$level->removeTile($this); $level->removeTile($this);
} }
$this->level->release(); unset($this->level);
} }
} }

View File

@ -228,9 +228,7 @@ class Utils{
PHP_SAPI, PHP_SAPI,
(string) PHP_INT_MAX . "." . PHP_INT_SIZE, (string) PHP_INT_MAX . "." . PHP_INT_SIZE,
serialize($_SERVER), serialize($_SERVER),
serialize(get_defined_constants()),
get_current_user(), get_current_user(),
serialize(ini_get_all()),
(string) memory_get_usage() . "." . memory_get_peak_usage(), (string) memory_get_usage() . "." . memory_get_peak_usage(),
php_uname(), php_uname(),
phpversion(), phpversion(),
@ -266,7 +264,7 @@ class Utils{
$strongEntropyValues = array( $strongEntropyValues = array(
is_array($startEntropy) ? hash("sha512", $startEntropy[($rounds + $drop) % count($startEntropy)], true) : hash("sha512", $startEntropy, true), //Get a random index of the startEntropy, or just read it is_array($startEntropy) ? hash("sha512", $startEntropy[($rounds + $drop) % count($startEntropy)], true) : hash("sha512", $startEntropy, true), //Get a random index of the startEntropy, or just read it
file_exists("/dev/urandom") ? fread(fopen("/dev/urandom", "rb"), 64) : str_repeat("\x00", 64), file_exists("/dev/urandom") ? fread(fopen("/dev/urandom", "rb"), 64) : str_repeat("\x00", 64),
(function_exists("openssl_random_pseudo_bytes") and version_compare(PHP_VERSION, "5.3.4", ">=")) ? openssl_random_pseudo_bytes(64) : str_repeat("\x00", 64), function_exists("openssl_random_pseudo_bytes") ? openssl_random_pseudo_bytes(64) : str_repeat("\x00", 64),
function_exists("mcrypt_create_iv") ? mcrypt_create_iv(64, MCRYPT_DEV_URANDOM) : str_repeat("\x00", 64), function_exists("mcrypt_create_iv") ? mcrypt_create_iv(64, MCRYPT_DEV_URANDOM) : str_repeat("\x00", 64),
$value, $value,
); );