Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51159 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 49025 invoked from network); 31 Dec 2010 16:08:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 31 Dec 2010 16:08:25 -0000 Authentication-Results: pb1.pair.com header.from=pierre.php@gmail.com; sender-id=pass; domainkeys=bad Authentication-Results: pb1.pair.com smtp.mail=pierre.php@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.42 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: pierre.php@gmail.com X-Host-Fingerprint: 209.85.214.42 mail-bw0-f42.google.com Received: from [209.85.214.42] ([209.85.214.42:41265] helo=mail-bw0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id BE/29-01063-7FFFD1D4 for ; Fri, 31 Dec 2010 11:08:24 -0500 Received: by bwz13 with SMTP id 13so11854984bwz.29 for ; Fri, 31 Dec 2010 08:08:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:in-reply-to :references:date:message-id:subject:from:to:cc:content-type; bh=zxFiDSMCLLXY/LPoitTUQEAyf77c+wxsA2BUZ2yl5QI=; b=eA4xWeNMKR6Em+0BDsQnxHWtOmnF9usvmpoLKaF6/FVXpQvxpm0Mm7n0FwxEk5g3Le Jws1/VPc3zxai06QK34823GFtVSx4puy8L60XxQU6/c9ujMWWy7ufg381HmmOxWPgs03 FsxZDgdVyg4o+wjLOXUCisKNrkJfGye5T8cZs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=NhrGCtvG8tWq9ZMHF8ZBMW8Kvv/4yLpIZHlZ4swC23+7e5bxR1mJTqPWgDvPemO5uK Z+N29f1Ku0yS5gU3isJAWrTYhAIjATBNKOPqMuKc530hYZ9Ej1M5wy/TvwQi1+EFSw0W 8NQ53c6QQ2W0pWR6CMaVL4DRXYd+mBfrG24JE= MIME-Version: 1.0 Received: by 10.204.117.10 with SMTP id o10mr6528974bkq.10.1293811700173; Fri, 31 Dec 2010 08:08:20 -0800 (PST) Received: by 10.204.52.129 with HTTP; Fri, 31 Dec 2010 08:08:20 -0800 (PST) In-Reply-To: References: Date: Fri, 31 Dec 2010 17:08:20 +0100 Message-ID: To: Marcin Babij Cc: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] Zend engine's hashtable performance tweaks From: pierre.php@gmail.com (Pierre Joye) hi, Did you forget to attach the patch? Attach it as .txt so the list won't reject it. Cheers, On Fri, Dec 31, 2010 at 3:40 PM, Marcin Babij wrote: > Hello, > I work for social network company, where we were running optimization > project. One of it's results is patch to Zend engine's Hashtable, which we > want to share and ask you for comments and improvements. > > Why we do this? > We run profiling on our production servers and found out that zend_hash_* > functions take 10-20% CPU time of request. So there is some room for easy > improvements. > > What was done? > - Hash function in zend_hash.h was rebuilt and became much faster, without > losing the most important properties. > - Hashtable implementation was changed from Simple chaining to Open > addressing with linear probing, but with linked bucket, not included in hash > array, which causes: > -- Bucket structure to lose 2 pointers. > -- Searching works similar, but don't have to jump with pointers stored in > different memory locations, inserting, deleting and rehashing don't need to > update linked list, but must search for first empty bucket, which is fast, > because it scans continuous memory. > -- Load factor decreases from 1.0 to 0.5-0.75 to make less collisions and > faster hashtable, which in turn increases memory footprint a little. > - Open addressing doesn't change significantly performance, but next thing > was to create new array (arEmpty), which is of size nTableSize bytes, which > keeps track of used/empty buckets and makes inserting and rehashing much > faster. In future it can be tested as bit-array with size of nTableSize/8 > bytes. > - More macros were added to replace repetitive constructs. > - New constants were added to allow: > -- Creating new hashtables of size at least X (where 4 and 8 are > reasonable), which makes no rehashing and reallocing memory while changing > size to 2 and then to 4. > -- For small tables it's better to extend them by a factor of 4 times, not > 2, to make rehashing cost smaller for most hashtables, of cost of little > higher memory consumption. > -- For large tables it's better to have other load factor, closer to 1, > while for small tables it's better to use load factor closer to 0.5. > - APC was patched to take changes in Bucket structure into account. > > How was it tested? > It was tested with make test, where one more (comparing to original sources) > test fails, but it's most probably because > http://bugs.php.net/bug.php?id=48858 - IMO test is badly constructed (is too > simple) and any change of hashing function makes it fail. Also it was tested > on our testing environment and production servers against >30mln requests to > our site, with 120requests/s at peak on Xeon @ 2.50GHz with 8GB RAM running > Debian Linux. > > What is the gain? > After tests CPU usage dropped by about 4% -6%. > Memory footprint goes up just by few percent. > > What can be done in future? > - Make new constants configurable by php.ini. > - Test if changing arEmpty from byte-array to bit-array helps on > performance. > - Tweak default constants' values using some real-live benchmarks. > - Prove (or modify and prove) hash function to have property, that it has no > collisions if two keys don't differ on no more than 6 bytes, which will lead > to memcmp omit first (or last) 6 bytes of key. Also simpler thing may be > proven, that is it has no collisions if two keys are not longer than 6 > bytes, which will make most string keys omit memcpy at all. > > The patch was created and tested against php-5.3.0, apc-3.1.3p1, then merged > with php-5.3.4, apc-3.1.6 without conflicts, and for these last versions > patches are attached. Also, it shouldn't conflict with > http://wiki.php.net/rfc/performanceimprovements . > > -- > Marcin Babij > nasza-klasa.pl > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > -- Pierre @pierrejoye | http://blog.thepimp.net | http://www.libgd.org