Replaced usages of Math::floorFloat() with (int) floor() and Math::ceilFloat() with (int) ceil()

Once upon a time, these userland functions were faster than calling builtins, but not anymore. According to my test the Math functions are twice slower in PHP 7.2 with typehints and 50% slower without typehints.

Inlining is slightly faster than using builtins, but the difference is very small - not worth making the code look any more ugly than it does already.
This commit is contained in:
Dylan K. Taylor
2018-05-28 18:54:35 +01:00
parent 71fdd59c4c
commit 4f8f334436
3 changed files with 42 additions and 42 deletions

View File

@ -1178,7 +1178,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
} }
if($this->onGround){ if($this->onGround){
$friction *= $this->level->getBlockAt(Math::floorFloat($this->x), Math::floorFloat($this->y) - 1, Math::floorFloat($this->z))->getFrictionFactor(); $friction *= $this->level->getBlockAt((int) floor($this->x), (int) floor($this->y - 1), (int) floor($this->z))->getFrictionFactor();
} }
$this->motion->x *= $friction; $this->motion->x *= $friction;
@ -1190,9 +1190,9 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
return false; return false;
} }
$floorX = Math::floorFloat($x); $floorX = (int) floor($x);
$floorY = Math::floorFloat($y); $floorY = (int) floor($y);
$floorZ = Math::floorFloat($z); $floorZ = (int) floor($z);
$diffX = $x - $floorX; $diffX = $x - $floorX;
$diffY = $y - $floorY; $diffY = $y - $floorY;
@ -1457,7 +1457,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
} }
public function isUnderwater() : bool{ public function isUnderwater() : bool{
$block = $this->level->getBlockAt(Math::floorFloat($this->x), Math::floorFloat($y = ($this->y + $this->getEyeHeight())), Math::floorFloat($this->z)); $block = $this->level->getBlockAt((int) floor($this->x), (int) floor($y = ($this->y + $this->getEyeHeight())), (int) floor($this->z));
if($block instanceof Water){ if($block instanceof Water){
$f = ($block->y + 1) - ($block->getFluidHeightPercent() - 0.1111111); $f = ($block->y + 1) - ($block->getFluidHeightPercent() - 0.1111111);
@ -1468,7 +1468,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
} }
public function isInsideOfSolid() : bool{ public function isInsideOfSolid() : bool{
$block = $this->level->getBlockAt(Math::floorFloat($this->x), Math::floorFloat($y = ($this->y + $this->getEyeHeight())), Math::floorFloat($this->z)); $block = $this->level->getBlockAt((int) floor($this->x), (int) floor($y = ($this->y + $this->getEyeHeight())), (int) floor($this->z));
return $block->isSolid() and !$block->isTransparent() and $block->collidesWithBB($this->getBoundingBox()); return $block->isSolid() and !$block->isTransparent() and $block->collidesWithBB($this->getBoundingBox());
} }
@ -1676,12 +1676,12 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
if($this->blocksAround === null){ if($this->blocksAround === null){
$inset = 0.001; //Offset against floating-point errors $inset = 0.001; //Offset against floating-point errors
$minX = Math::floorFloat($this->boundingBox->minX + $inset); $minX = (int) floor($this->boundingBox->minX + $inset);
$minY = Math::floorFloat($this->boundingBox->minY + $inset); $minY = (int) floor($this->boundingBox->minY + $inset);
$minZ = Math::floorFloat($this->boundingBox->minZ + $inset); $minZ = (int) floor($this->boundingBox->minZ + $inset);
$maxX = Math::floorFloat($this->boundingBox->maxX - $inset); $maxX = (int) floor($this->boundingBox->maxX - $inset);
$maxY = Math::floorFloat($this->boundingBox->maxY - $inset); $maxY = (int) floor($this->boundingBox->maxY - $inset);
$maxZ = Math::floorFloat($this->boundingBox->maxZ - $inset); $maxZ = (int) floor($this->boundingBox->maxZ - $inset);
$this->blocksAround = []; $this->blocksAround = [];

View File

@ -164,12 +164,12 @@ class Explosion{
} }
$explosionSize = $this->size * 2; $explosionSize = $this->size * 2;
$minX = Math::floorFloat($this->source->x - $explosionSize - 1); $minX = (int) floor($this->source->x - $explosionSize - 1);
$maxX = Math::ceilFloat($this->source->x + $explosionSize + 1); $maxX = (int) ceil($this->source->x + $explosionSize + 1);
$minY = Math::floorFloat($this->source->y - $explosionSize - 1); $minY = (int) floor($this->source->y - $explosionSize - 1);
$maxY = Math::ceilFloat($this->source->y + $explosionSize + 1); $maxY = (int) ceil($this->source->y + $explosionSize + 1);
$minZ = Math::floorFloat($this->source->z - $explosionSize - 1); $minZ = (int) floor($this->source->z - $explosionSize - 1);
$maxZ = Math::ceilFloat($this->source->z + $explosionSize + 1); $maxZ = (int) ceil($this->source->z + $explosionSize + 1);
$explosionBB = new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ); $explosionBB = new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ);

View File

@ -1099,12 +1099,12 @@ class Level implements ChunkManager, Metadatable{
* @return Block[] * @return Block[]
*/ */
public function getCollisionBlocks(AxisAlignedBB $bb, bool $targetFirst = false) : array{ public function getCollisionBlocks(AxisAlignedBB $bb, bool $targetFirst = false) : array{
$minX = Math::floorFloat($bb->minX - 1); $minX = (int) floor($bb->minX - 1);
$minY = Math::floorFloat($bb->minY - 1); $minY = (int) floor($bb->minY - 1);
$minZ = Math::floorFloat($bb->minZ - 1); $minZ = (int) floor($bb->minZ - 1);
$maxX = Math::floorFloat($bb->maxX + 1); $maxX = (int) floor($bb->maxX + 1);
$maxY = Math::floorFloat($bb->maxY + 1); $maxY = (int) floor($bb->maxY + 1);
$maxZ = Math::floorFloat($bb->maxZ + 1); $maxZ = (int) floor($bb->maxZ + 1);
$collides = []; $collides = [];
@ -1162,12 +1162,12 @@ class Level implements ChunkManager, Metadatable{
* @return AxisAlignedBB[] * @return AxisAlignedBB[]
*/ */
public function getCollisionCubes(Entity $entity, AxisAlignedBB $bb, bool $entities = true) : array{ public function getCollisionCubes(Entity $entity, AxisAlignedBB $bb, bool $entities = true) : array{
$minX = Math::floorFloat($bb->minX - 1); $minX = (int) floor($bb->minX - 1);
$minY = Math::floorFloat($bb->minY - 1); $minY = (int) floor($bb->minY - 1);
$minZ = Math::floorFloat($bb->minZ - 1); $minZ = (int) floor($bb->minZ - 1);
$maxX = Math::floorFloat($bb->maxX + 1); $maxX = (int) floor($bb->maxX + 1);
$maxY = Math::floorFloat($bb->maxY + 1); $maxY = (int) floor($bb->maxY + 1);
$maxZ = Math::floorFloat($bb->maxZ + 1); $maxZ = (int) floor($bb->maxZ + 1);
$collides = []; $collides = [];
@ -1903,10 +1903,10 @@ class Level implements ChunkManager, Metadatable{
$nearby = []; $nearby = [];
if($entity === null or $entity->canCollide){ if($entity === null or $entity->canCollide){
$minX = Math::floorFloat($bb->minX - 2) >> 4; $minX = ((int) floor($bb->minX - 2)) >> 4;
$maxX = Math::floorFloat($bb->maxX + 2) >> 4; $maxX = ((int) floor($bb->maxX + 2)) >> 4;
$minZ = Math::floorFloat($bb->minZ - 2) >> 4; $minZ = ((int) floor($bb->minZ - 2)) >> 4;
$maxZ = Math::floorFloat($bb->maxZ + 2) >> 4; $maxZ = ((int) floor($bb->maxZ + 2)) >> 4;
for($x = $minX; $x <= $maxX; ++$x){ for($x = $minX; $x <= $maxX; ++$x){
for($z = $minZ; $z <= $maxZ; ++$z){ for($z = $minZ; $z <= $maxZ; ++$z){
@ -1934,10 +1934,10 @@ class Level implements ChunkManager, Metadatable{
public function getNearbyEntities(AxisAlignedBB $bb, Entity $entity = null) : array{ public function getNearbyEntities(AxisAlignedBB $bb, Entity $entity = null) : array{
$nearby = []; $nearby = [];
$minX = Math::floorFloat($bb->minX - 2) >> 4; $minX = ((int) floor($bb->minX - 2)) >> 4;
$maxX = Math::floorFloat($bb->maxX + 2) >> 4; $maxX = ((int) floor($bb->maxX + 2)) >> 4;
$minZ = Math::floorFloat($bb->minZ - 2) >> 4; $minZ = ((int) floor($bb->minZ - 2)) >> 4;
$maxZ = Math::floorFloat($bb->maxZ + 2) >> 4; $maxZ = ((int) floor($bb->maxZ + 2)) >> 4;
for($x = $minX; $x <= $maxX; ++$x){ for($x = $minX; $x <= $maxX; ++$x){
for($z = $minZ; $z <= $maxZ; ++$z){ for($z = $minZ; $z <= $maxZ; ++$z){
@ -1965,10 +1965,10 @@ class Level implements ChunkManager, Metadatable{
public function getNearestEntity(Vector3 $pos, float $maxDistance, string $entityType = Entity::class, bool $includeDead = false) : ?Entity{ public function getNearestEntity(Vector3 $pos, float $maxDistance, string $entityType = Entity::class, bool $includeDead = false) : ?Entity{
assert(is_a($entityType, Entity::class, true)); assert(is_a($entityType, Entity::class, true));
$minX = Math::floorFloat($pos->x - $maxDistance) >> 4; $minX = ((int) floor($pos->x - $maxDistance)) >> 4;
$maxX = Math::floorFloat($pos->x + $maxDistance) >> 4; $maxX = ((int) floor($pos->x + $maxDistance)) >> 4;
$minZ = Math::floorFloat($pos->z - $maxDistance) >> 4; $minZ = ((int) floor($pos->z - $maxDistance)) >> 4;
$maxZ = Math::floorFloat($pos->z + $maxDistance) >> 4; $maxZ = ((int) floor($pos->z + $maxDistance)) >> 4;
$currentTargetDistSq = $maxDistance ** 2; $currentTargetDistSq = $maxDistance ** 2;