From 55a1731da34f22f1f301ac285412d99ccc7acf73 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 20 Oct 2017 19:23:04 +0100 Subject: [PATCH] Remove redundant asserting function in Binary If this was an inline assert, it would be harmless. Since it's in a function, it contributes to a 20% performance loss when using these functions on a large scale regardless of whether assertions are enabled or not. Additionally, there's no need to assert that we have enough bytes since unpack() will raise warnings if there isn't, which will trigger exceptions, and for readByte(), an undefined offset notice will be raised, which will also trigger an exception. TL;DR: This is simply wasting CPU time for no good reason. --- src/pocketmine/utils/Binary.php | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/pocketmine/utils/Binary.php b/src/pocketmine/utils/Binary.php index cfada9503..5677549c7 100644 --- a/src/pocketmine/utils/Binary.php +++ b/src/pocketmine/utils/Binary.php @@ -68,11 +68,6 @@ class Binary{ return self::readLLong(self::writeLong($value)); } - - private static function checkLength($str, $expect){ - assert(($len = strlen($str)) === $expect, "Expected $expect bytes, got $len"); - } - /** * Reads a byte boolean * @@ -100,7 +95,6 @@ class Binary{ * @return int */ public static function readByte(string $c) : int{ - self::checkLength($c, 1); return ord($c{0}); } @@ -131,7 +125,6 @@ class Binary{ * @return int */ public static function readShort(string $str) : int{ - self::checkLength($str, 2); return unpack("n", $str)[1]; } @@ -143,7 +136,6 @@ class Binary{ * @return int */ public static function readSignedShort(string $str) : int{ - self::checkLength($str, 2); return self::signShort(unpack("n", $str)[1]); } @@ -166,7 +158,6 @@ class Binary{ * @return int */ public static function readLShort(string $str) : int{ - self::checkLength($str, 2); return unpack("v", $str)[1]; } @@ -178,7 +169,6 @@ class Binary{ * @return int */ public static function readSignedLShort(string $str) : int{ - self::checkLength($str, 2); return self::signShort(unpack("v", $str)[1]); } @@ -200,7 +190,6 @@ class Binary{ * @return int */ public static function readTriad(string $str) : int{ - self::checkLength($str, 3); return unpack("N", "\x00" . $str)[1]; } @@ -221,7 +210,6 @@ class Binary{ * @return int */ public static function readLTriad(string $str) : int{ - self::checkLength($str, 3); return unpack("V", $str . "\x00")[1]; } @@ -242,7 +230,6 @@ class Binary{ * @return int */ public static function readInt(string $str) : int{ - self::checkLength($str, 4); return self::signInt(unpack("N", $str)[1]); } @@ -263,7 +250,6 @@ class Binary{ * @return int */ public static function readLInt(string $str) : int{ - self::checkLength($str, 4); return self::signInt(unpack("V", $str)[1]); } @@ -284,7 +270,6 @@ class Binary{ * @return float */ public static function readFloat(string $str) : float{ - self::checkLength($str, 4); return unpack("G", $str)[1]; } @@ -317,7 +302,6 @@ class Binary{ * @return float */ public static function readLFloat(string $str) : float{ - self::checkLength($str, 4); return unpack("g", $str)[1]; } @@ -360,7 +344,6 @@ class Binary{ * @return float */ public static function readDouble(string $str) : float{ - self::checkLength($str, 8); return unpack("E", $str)[1]; } @@ -381,7 +364,6 @@ class Binary{ * @return float */ public static function readLDouble(string $str) : float{ - self::checkLength($str, 8); return unpack("e", $str)[1]; } @@ -401,7 +383,6 @@ class Binary{ * @return int */ public static function readLong(string $x) : int{ - self::checkLength($x, 8); $int = unpack("N*", $x); return ($int[1] << 32) | $int[2]; }