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){
$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;
@ -1190,9 +1190,9 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
return false;
}
$floorX = Math::floorFloat($x);
$floorY = Math::floorFloat($y);
$floorZ = Math::floorFloat($z);
$floorX = (int) floor($x);
$floorY = (int) floor($y);
$floorZ = (int) floor($z);
$diffX = $x - $floorX;
$diffY = $y - $floorY;
@ -1457,7 +1457,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
}
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){
$f = ($block->y + 1) - ($block->getFluidHeightPercent() - 0.1111111);
@ -1468,7 +1468,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
}
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());
}
@ -1676,12 +1676,12 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
if($this->blocksAround === null){
$inset = 0.001; //Offset against floating-point errors
$minX = Math::floorFloat($this->boundingBox->minX + $inset);
$minY = Math::floorFloat($this->boundingBox->minY + $inset);
$minZ = Math::floorFloat($this->boundingBox->minZ + $inset);
$maxX = Math::floorFloat($this->boundingBox->maxX - $inset);
$maxY = Math::floorFloat($this->boundingBox->maxY - $inset);
$maxZ = Math::floorFloat($this->boundingBox->maxZ - $inset);
$minX = (int) floor($this->boundingBox->minX + $inset);
$minY = (int) floor($this->boundingBox->minY + $inset);
$minZ = (int) floor($this->boundingBox->minZ + $inset);
$maxX = (int) floor($this->boundingBox->maxX - $inset);
$maxY = (int) floor($this->boundingBox->maxY - $inset);
$maxZ = (int) floor($this->boundingBox->maxZ - $inset);
$this->blocksAround = [];