Use pg_bitutils for HyperLogLog.
authorJeff Davis <jdavis@postgresql.org>
Thu, 30 Jul 2020 15:44:58 +0000 (08:44 -0700)
committerJeff Davis <jdavis@postgresql.org>
Thu, 30 Jul 2020 16:14:23 +0000 (09:14 -0700)
Using pg_leftmost_one_post32() yields substantial performance benefits.

Backpatching to version 13 because HLL is used for HashAgg
improvements in 9878b643, which was also backpatched to 13.

Reviewed-by: Peter Geoghegan
Discussion: https://postgr.es/m/CAH2-WzkGvDKVDo+0YvfvZ+1CE=iCi88DCOGFF3i1hTGGaxcKPw@mail.gmail.com
Backpatch-through: 13

src/backend/lib/hyperloglog.c

index a5cc1f8b83b4eb9eeb36a4d50178e799f7f0975e..351fed8186fb2d7ce331f0244df43e142a08327d 100644 (file)
@@ -49,6 +49,7 @@
 #include <math.h>
 
 #include "lib/hyperloglog.h"
+#include "port/pg_bitutils.h"
 
 #define POW_2_32           (4294967296.0)
 #define NEG_POW_2_32       (-4294967296.0)
@@ -242,11 +243,13 @@ rho(uint32 x, uint8 b)
 {
    uint8       j = 1;
 
-   while (j <= b && !(x & 0x80000000))
-   {
-       j++;
-       x <<= 1;
-   }
+   if (x == 0)
+       return b + 1;
+
+   j = 32 - pg_leftmost_one_pos32(x);
+
+   if (j > b)
+       return b + 1;
 
    return j;
 }