Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rrJyp-00GrQ6-Sg for pgsql-hackers@arkaria.postgresql.org; Mon, 01 Apr 2024 15:53:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rrJyn-00GLmb-9i for pgsql-hackers@arkaria.postgresql.org; Mon, 01 Apr 2024 15:53:49 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rrJym-00GLmT-RM for pgsql-hackers@lists.postgresql.org; Mon, 01 Apr 2024 15:53:48 +0000 Received: from mail-qk1-x72e.google.com ([2607:f8b0:4864:20::72e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rrJye-007dOS-Ko for pgsql-hackers@lists.postgresql.org; Mon, 01 Apr 2024 15:53:47 +0000 Received: by mail-qk1-x72e.google.com with SMTP id af79cd13be357-78be2f42d82so65322685a.0 for ; Mon, 01 Apr 2024 08:53:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711986820; x=1712591620; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=LOAlBo/BO2pH24YD09UYhaGS7iQ0SjBt0oAC1dgLEdU=; b=jIRzRDfJ5Ii8BV3XgP2LqVCdrai+ur855CzZCAbryO8OoZCiFvi5xIz/PokYzm2iVB r0cztLAgWXcRpWo5StlFo51lHi5mFmhttKm2zLgbAYNZy4ktG9ncRdbCU7QvdJrT+ws8 u+zLJPH/z1/prT3nfunOu6JzRnur7AG4AnWyg0PdjM74yYseX/AxZXKNeJHspkgG/owY mTMIX48E0z+Ck9mrnzGT8lL7xjqHpOzJf58/lsohpisZdApG8Uzh/aomThuiWmyDsQkL mvC2hqjk9EMbJi9U0dL0KhNuZw1EpsSQtS0XIBeGgVbNm63LCa21dBgkkoEL+x5YQEKc c6ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711986820; x=1712591620; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=LOAlBo/BO2pH24YD09UYhaGS7iQ0SjBt0oAC1dgLEdU=; b=uITTtc/J80w5Bz+/bm/u7TLuIOP8ojHnFDMc2oVKVx3/g+d5FzxQruFriULIhleytA fc0IhaYGK1UiIueolX/X2E3Kynwn+vJW4D/5KtWoC7WJDSD/JvBPPlDHIEr2Jc8GjXbA lR5vvbF8iy4kCQzLBqOvQFdwEbfvbcwzjvArpDgQ8n+irMZo9OoTYXPtAYkhEcvVYAUC lq9EmyBkO/SCBKTisXHQjQH8njboGwPgbO+Uh6f4GZEBbTHtRGFLeXegZSXR71y7WMZo XBDRoLkSF+q28V0MqKtQnkLuRZtzKkEaU73R0Vq4fIJtkGLOi8K2OGtcCKb9gWmATFcX D2vQ== X-Forwarded-Encrypted: i=1; AJvYcCU+CpIXGVuzaTbW9LbYTPpZKVwoCUFZe/Xh0MoRyKOnzON7wLJbUOgsJF042ODK1yUuaaz7Em2L80vdcf4u0u51pZ2cYVkIrcqd2f3UlFFJMuTV X-Gm-Message-State: AOJu0Yy6+GrLVPU8Plqiz7DAOPxZL00Xk685vPXbL8u4k6k2zVl+s6d9 sMoh6VrsFFJIy5d1VAV6DlRUk7w9FUJ8EhaFrIRGl35GJyTJY7tH X-Google-Smtp-Source: AGHT+IHXx/xwPjRGPExCHYoAw9zVGIAgOleASgiqBtm9sAPzqQBmjeDofY7uJuwg4OUryHWnNRN9lA== X-Received: by 2002:a05:620a:2791:b0:78b:e6b1:b770 with SMTP id g17-20020a05620a279100b0078be6b1b770mr2390058qkp.41.1711986820287; Mon, 01 Apr 2024 08:53:40 -0700 (PDT) Received: from nathanxps13 (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id c1-20020a05620a134100b0078a2d1fe684sm3543799qkl.27.2024.04.01.08.53.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Apr 2024 08:53:39 -0700 (PDT) Date: Mon, 1 Apr 2024 10:53:38 -0500 From: Nathan Bossart To: Alvaro Herrera Cc: "Amonson, Paul D" , Tom Lane , David Rowley , Andres Freund , "Shankaran, Akash" , Noah Misch , Matthias van de Meent , "pgsql-hackers@lists.postgresql.org" Subject: Re: Popcount optimization using AVX512 Message-ID: <20240401155338.GA2296072@nathanxps13> References: <20240401011708.GA2128739@nathanxps13> <202404011106.y4fci35kzdqt@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <202404011106.y4fci35kzdqt@alvherre.pgsql> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, Apr 01, 2024 at 01:06:12PM +0200, Alvaro Herrera wrote: > On 2024-Mar-31, Nathan Bossart wrote: >> + popcnt = _mm512_reduce_add_epi64(accum); >> + return popcnt + pg_popcount_fast(buf, bytes); > > Hmm, doesn't this arrangement cause an extra function call to > pg_popcount_fast to be used here? Given the level of micro-optimization > being used by this code, I would have thought that you'd have tried to > avoid that. (At least, maybe avoid the call if bytes is 0, no?) Yes, it does. I did another benchmark on very small arrays and can see the overhead. This is the time in milliseconds to run pg_popcount() on an array 1 billion times: size (bytes) HEAD AVX512-POPCNT 1 1707.685 3480.424 2 1926.694 4606.182 4 3210.412 5284.506 8 1920.703 3640.968 16 2936.91 4045.586 32 3627.956 5538.418 64 5347.213 3748.212 I suspect that anything below 64 bytes will see this regression, as that is the earliest point where there are enough bytes for ZMM registers. We could avoid the call if there are no remaining bytes, but the numbers for the smallest arrays probably wouldn't improve much, and that might actually add some overhead due to branching. The other option to avoid this overhead is to put most of pg_bitutils.c into its header file so that we can inline the call. Reviewing the current callers of pg_popcount(), IIUC the only ones that are passing very small arrays are the bit_count() implementations and a call in the syslogger for a single byte. I don't know how much to worry about the overhead for bit_count() since there's presumably a bunch of other overhead, and the syslogger one could probably be fixed via an inline function that pulled the value from pg_number_of_ones (which would probably be an improvement over the status quo, anyway). But this is all to save a couple of nanoseconds... -- Nathan Bossart Amazon Web Services: https://aws.amazon.com