0001-Optimize-pg_popcount32-64_slow-lookups.patch

application/octet-stream

Filename: 0001-Optimize-pg_popcount32-64_slow-lookups.patch
Type: application/octet-stream
Part: 0
Message: Popcount optimization for the slow-path lookups

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch 0001
Subject: Optimize pg_popcount32/64_slow() lookups
File+
contrib/pg_tde/src/libkmip 1 0
src/port/pg_bitutils.c 12 18
From a8d1105d90ce4d1e04626d7680f7338f04704c14 Mon Sep 17 00:00:00 2001
From: Andrew Pogrebnoi <absourd.noise@gmail.com>
Date: Fri, 5 Dec 2025 10:45:52 +0200
Subject: [PATCH] Optimize pg_popcount32/64_slow() lookups

This commit makes lookups into pg_number_of_ones[] branchless. Which optimizes the slow-path of words' popcount.
---
 contrib/pg_tde/src/libkmip |  1 +
 src/port/pg_bitutils.c     | 30 ++++++++++++------------------
 2 files changed, 13 insertions(+), 18 deletions(-)
 create mode 160000 contrib/pg_tde/src/libkmip

diff --git a/contrib/pg_tde/src/libkmip b/contrib/pg_tde/src/libkmip
new file mode 160000
index 00000000000..f3f21ceb32b
--- /dev/null
+++ b/contrib/pg_tde/src/libkmip
@@ -0,0 +1 @@
+Subproject commit f3f21ceb32bef8ce8fb36e25c6ae4831f7689e02
diff --git a/src/port/pg_bitutils.c b/src/port/pg_bitutils.c
index 5677525693d..4c482c5f218 100644
--- a/src/port/pg_bitutils.c
+++ b/src/port/pg_bitutils.c
@@ -350,15 +350,10 @@ pg_popcount32_slow(uint32 word)
 #ifdef HAVE__BUILTIN_POPCOUNT
 	return __builtin_popcount(word);
 #else							/* !HAVE__BUILTIN_POPCOUNT */
-	int			result = 0;
-
-	while (word != 0)
-	{
-		result += pg_number_of_ones[word & 255];
-		word >>= 8;
-	}
-
-	return result;
+	return pg_number_of_ones[word >> 24] +
+		   pg_number_of_ones[(word >> 16) & 0xFF] +
+		   pg_number_of_ones[(word >> 8) & 0xFF] +
+		   pg_number_of_ones[word & 0xFF];
 #endif							/* HAVE__BUILTIN_POPCOUNT */
 }
 
@@ -378,15 +373,14 @@ pg_popcount64_slow(uint64 word)
 #error "cannot find integer of the same size as uint64_t"
 #endif
 #else							/* !HAVE__BUILTIN_POPCOUNT */
-	int			result = 0;
-
-	while (word != 0)
-	{
-		result += pg_number_of_ones[word & 255];
-		word >>= 8;
-	}
-
-	return result;
+	return pg_number_of_ones[word >> 56] +
+		   pg_number_of_ones[(word >> 48) & 0xFF] +
+		   pg_number_of_ones[(word >> 40) & 0xFF] +
+		   pg_number_of_ones[(word >> 32) & 0xFF] +
+		   pg_number_of_ones[(word >> 24) & 0xFF] +
+		   pg_number_of_ones[(word >> 16) & 0xFF] +
+		   pg_number_of_ones[(word >> 8) & 0xFF] +
+		   pg_number_of_ones[word >> 0 & 0xFF];
 #endif							/* HAVE__BUILTIN_POPCOUNT */
 }
 
-- 
2.43.0