Bug #79023 [Com]: very slow hash('crc32b', ...)

From: Date: Tue, 31 Dec 2019 04:42:00 +0000
Subject: Bug #79023 [Com]: very slow hash('crc32b', ...)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-224629@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79023&edit=1 ID: 79023 Comment by: bugreports at gmail dot com Reported by: bugzilla77 at gmail dot com Summary: very slow hash('crc32b', ...) Status: Open Type: Bug Package: hash related Operating System: Windows 10 PHP Version: 7.4.1 Block user comment: N Private report: N New Comment: crc32 could be way faster with a native/avx implementation like base64 got in PHP 7.3 looking at the output of mysqld "InnoDB: Using SSE2 crc32 instructions" currently it's even slower than sha1 and xxhash should be in the official hash-extension https://create.stephan-brumme.com/xxhash/ https://github.com/stuartherbert/php-xxhash md4: 0.15917 joaat: 0.27944 md5: 0.28036 sha1: 0.38227 crc32: 0.46766 sha3-224: 0.55059 sha3-256: 0.61051 crc32b: 0.64266 crc32c: 0.64455 adler32: 0.70149 sha3-384: 0.75485 sha224: 1.02762 sha256: 1.02943 sha3-512: 1.07797 sha384: 1.13733 sha512/256: 1.143 sha512/224: 1.14454 sha512: 1.14697 whirlpool: 1.76455 Previous Comments: ------------------------------------------------------------------------ [2019-12-30 17:31:16] nikic@php.net Created https://github.com/php/php-src/pull/5044 to improve hash() performance. With that and a larger tripcount I get: 0.31190896034241 sec 0.48243403434753 sec 0.46064209938049 sec Getting full parity isn't really possible because a) hash() is dynamic, it needs to lookup the algorithm and dispatch to it and b) hash() returns a string, while crc32() returns an integer. ------------------------------------------------------------------------ [2019-12-24 10:32:09] cmb@php.net Related To: Bug #79024 ------------------------------------------------------------------------ [2019-12-24 10:30:53] cmb@php.net Firstly, crc32($x) doesn't call hash('crc32b', $x), so the assumption that the latter should be equally fast as the former (or even faster) is wrong. Actually, these functions share only the actual hashing primitives. Since hash() has to first map the given string to the desired algorithm, and uses function pointers subsequently, it is supposed to be somewhat slower. However, hash() still uses classic ZPP, while crc32() uses the newer fast ZPP macros, what causes an additional performance penalty. Basically, the same applies to sha1() and md5(). ------------------------------------------------------------------------ [2019-12-24 00:07:44] bugzilla77 at gmail dot com Description: ------------ very slow hash('crc32b', ...) Test script: --------------- <?php $i=2000000; $x=sha1(microtime(1).mt_rand()); $time_a=microtime(true); for($z=0;$z<$i;$z++){ $a=crc32($x); } $time_a=microtime(true)-$time_a; $time_b=microtime(true); for($z=0;$z<$i;$z++){ $b=hash('crc32b',$x); } $time_b=microtime(true)-$time_b; $time_c=microtime(true); for($z=0;$z<$i;$z++){ $c=hash('crc32b',$x,true); } $time_c=microtime(true)-$time_c; print($time_a.' sec'); print('<br/>'); print($time_b.' sec'); print('<br/>'); print($time_c.' sec'); ?> Expected result: ---------------- 0.34135699272156 sec <=0.34135699272156 sec <=0.34135699272156 sec Actual result: -------------- 0.34135699272156 sec 0.77139401435852 sec 0.80676794052124 sec ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=79023&edit=1

« previous php.bugs (#224629) next »