Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gZw2m-0000NE-UK for pgsql-hackers@arkaria.postgresql.org; Thu, 20 Dec 2018 10:59:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gZw2l-0001ag-MF for pgsql-hackers@arkaria.postgresql.org; Thu, 20 Dec 2018 10:59:07 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gZw2l-0001Ti-A1 for pgsql-hackers@lists.postgresql.org; Thu, 20 Dec 2018 10:59:07 +0000 Received: from hermes.doblenet.net ([213.27.130.15]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gZw2i-0007ys-5P for pgsql-hackers@lists.postgresql.org; Thu, 20 Dec 2018 10:59:06 +0000 Subject: Re: Using POPCNT and other advanced bit manipulation instructions To: David Rowley , PostgreSQL Hackers Cc: Thomas Munro References: From: Jose Luis Tallon Message-ID: Date: Thu, 20 Dec 2018 11:58:43 +0100 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 20/12/18 6:53, David Rowley wrote: > Back in 2016 [1] there was some discussion about using the POPCNT > instruction to improve the performance of counting the number of bits > set in a word. Improving this helps various cases, such as > bms_num_members and also things like counting the allvisible and > frozen pages in the visibility map. > > [snip] > > I've put together a very rough patch to implement using POPCNT and the > leading and trailing 0-bit instructions to improve the performance of > bms_next_member() and bms_prev_member(). The correct function should > be determined on the first call to each function by way of setting a > function pointer variable to the most suitable supported > implementation. I've not yet gone through and removed all the > number_of_ones[] arrays to replace with a pg_popcount*() call. IMVHO: Please do not disregard potential optimization by the compiler around those calls.. o_0  That might explain the reduced performance improvement observed. Not that I can see any obvious alternative to your implementation right away ... > That > seems to have mostly been done in Thomas' patch [3], part of which > I've used for the visibilitymap.c code changes. If this patch proves > to be possible, then I'll look at including the other changes Thomas > made in his patch too. > > What I'm really looking for by posting now are reasons why we can't do > this. I'm also interested in getting some testing done on older > machines, particularly machines with processors that are from before > 2007, both AMD and Intel. I can offer a 2005-vintage Opteron 2216 rev3 (bought late 2007) to test on. Feel free to toss me some test code. cpuinfo flags:    fpu de tsc msr pae mce cx8 apic mca cmov pat clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt lm 3dnowext 3dnow rep_good nopl extd_apicid eagerfpu pni cx16 hypervisor lahf_lm cmp_legacy 3dnowprefetch vmmcall > 2007-2008 seems to be around the time both > AMD and Intel added support for POPCNT and LZCNT, going by [4]. Thanks