Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:10430 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 10705 invoked by uid 1010); 14 Jun 2004 08:29:48 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 10511 invoked by uid 1007); 14 Jun 2004 08:29:47 -0000 To: internals@lists.php.net Date: Mon, 14 Jun 2004 11:28:57 +0300 References: <40CC1D3C.9070605@php.net> Organization: none Content-Type: text/plain; format=flowed; delsp=yes; charset=koi8-r MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID: User-Agent: Opera M2/7.50 (Win32, build 3778) X-Posted-By: 217.23.116.150 Subject: Re: crc32() improvements From: valyala@tut.by ("Alexander Valyalkin") On Sun, 13 Jun 2004 11:24:12 +0200, Ard Biesheuvel wrote: > > First of all, crc32tab is no longer in the .text segment, so it will not > be shared between forked processes, taking more memory space than > necessary. Each process will have to initialise it as well, so the init > loop will run more than once. > > Secondly, compiler optimisation is always more effective working on data > known to be constant. So removing 'const' from cr32tab is likely to > produce slower code. > > Also, you used long int instead of int, which means your code will break > on 64-bit platforms. > >> 2) Speed is improved on large amount of data. > > I seriously doubt that, as the tight main loop of the function didn't > change that much. Using 'register' won't help you here (Windows cl.exe > officialy ignores it altogether, GCC is smart enough to figure it out > for itself) > Thank you for good explanation and comments. Now I understood that the current crc32 implementation is better than mine. But it consists some ugly bugs (read my comments): PHP_NAMED_FUNCTION(php_if_crc32) { unsigned int crc = ~0; char *p; int len, nr; /*!!! is special sense in [len] var here? Remove it! */ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &p, &nr) == FAILURE) { return; } /* !!! there is no error check nr < 0 */ len = 0 ; /* !!! remove it! */ for (len += nr; nr--; ++p) { CRC32(crc, *p); } RETVAL_LONG(~crc); } Below is corrected function with speed improvement in main cycle (up to 40% on my compiler) =========cut======== PHP_NAMED_FUNCTION(php_if_crc32) { unsigned int crc = ~0; unsigned char *p; int nr; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", (char **)&p, &nr) == FAILURE || nr < 0) return; if (nr == 0) RETVAL_LONG(0); do { crc = (crc >> 8) ^ crc32tab[(unsigned char)crc ^ *p++]; } while (--nr); RETVAL_LONG(~crc); } =========cut========= And do not forget to remove CRC32() definition in crc32.h -- Using Opera's revolutionary e-mail client: http://www.opera.com/m2/