Speeding up GIST index creation for hstore

Started by Kirill Reshke24 days ago3 messageshackers
Jump to latest
#1Kirill Reshke
reshkekirill@gmail.com

scrolling through an old iPad I reopened [0]/messages/by-id/CAJ3gD9dXb=+B2pb+U+LdX-WuEjU7S53gV-r5jOCrZpLmCaou1w@mail.gmail.com thread. It has some
optimizations for tsvectors GiST speedup. Lurking through contrib
modules, I noticed hstore call sites, so I tried to speedup them
alike.

Below, diff replaces a simple bit loop with XOR's bitcount and uses
pg_popcount for calculatiuon a signature (sizebitvec).

--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -9,6 +9,7 @@
 #include "catalog/pg_type.h"
 #include "common/int.h"
 #include "hstore.h"
+#include "port/pg_bitutils.h"
 #include "utils/pg_crc.h"
 /* gist_hstore_ops opclass options */
@@ -252,15 +253,7 @@
 static int32
 sizebitvec(BITVECP sign, int siglen)
 {
- int32 size = 0,
- i;
-
- LOOPBYTE(siglen)
- {
- size += SUMBIT(sign);
- sign = (BITVECP) (((char *) sign) + 1);
- }
- return size;
+ return pg_popcount(sign, siglen);
 }

static int
@@ -269,11 +262,8 @@
int i,
dist = 0;

- LOOPBIT(siglen)
- {
- if (GETBIT(a, i) != GETBIT(b, i))
- dist++;
- }
+ LOOPBYTE(siglen)
+ dist += pg_number_of_ones[(unsigned char) (a[i] ^ b[i])];
  return dist;
 }

Note that ltree's sizebitvec optimization already calls pg_popcount,
likewize intarray, pg_trgm, and text search for GiST (tsquery gist
does something different for some reason). Said that, almost all
speedup comes from hemdistsign optimization alone.

On this example
CREATE EXTENSION IF NOT EXISTS hstore;

CREATE TABLE t AS
SELECT hstore(ARRAY['a','b','c','d'],
ARRAY[i::text, (i*3)::text, (i*7)::text, (i*11)::text]) AS h
FROM generate_series(1,50000) as s(i);

\timing on
CREATE INDEX ON t USING gist (h gist_hstore_ops);

Index creating runtime goes from 1454.871- 1488.210 ms to 274.969 -
295.313 ms (about 6 times faster) (4 runs)

[0]: /messages/by-id/CAJ3gD9dXb=+B2pb+U+LdX-WuEjU7S53gV-r5jOCrZpLmCaou1w@mail.gmail.com

--
Best regards,
Kirill Reshke

#2Roman Khapov
rkhapov@yandex-team.ru
In reply to: Kirill Reshke (#1)
Re: Speeding up GIST index creation for hstore

Hi!

Overall the patch looks correct and consistent
with how intarray, pg_trgm and ltree already
do it, so I have no objections on the approach.

Index creating runtime goes from 1454.871- 1488.210 ms to 274.969 -
295.313 ms (about 6 times faster) (4 runs)

I benchmarked the patch too, on my ARM64 maxOS
machine I got next results:

baseline patched
CREATE INDEX ~392 ms ~233 ms (1.7x)

Index search shows no regressions (within noise).

Seems like the 6x number appears to be machine-dependent,
on my laptop the absolute time of the patched version similar
yours (~233 vs ~275-295 ms), but the baseline was 390ms rather
than 1450 ms, giving ~1.6-1.7x instead of 6x.

--
Best regards,
Roman Khapov

#3Kirill Reshke
reshkekirill@gmail.com
In reply to: Roman Khapov (#2)
Re: Speeding up GIST index creation for hstore

On Sat, 31 Oct 2026 at 21:35, Roman Khapov <rkhapov@yandex-team.ru> wrote:

Hi!

Overall the patch looks correct and consistent
with how intarray, pg_trgm and ltree already
do it, so I have no objections on the approach.

Index creating runtime goes from 1454.871- 1488.210 ms to 274.969 -
295.313 ms (about 6 times faster) (4 runs)

I benchmarked the patch too, on my ARM64 maxOS
machine I got next results:

baseline patched
CREATE INDEX ~392 ms ~233 ms (1.7x)

Index search shows no regressions (within noise).

Seems like the 6x number appears to be machine-dependent,
on my laptop the absolute time of the patched version similar
yours (~233 vs ~275-295 ms), but the baseline was 390ms rather
than 1450 ms, giving ~1.6-1.7x instead of 6x.

--
Best regards,
Roman Khapov

Looks very platform-dependent for me,

On AMD EPYC Genoa :

compiler | timing | speed up

GCC 12.4 | 1778 → 279 | 6,35
GCC 13.3 | 1464 → 283 | 5,17
GCC 14.2 | 1350 → 277 | 4,87
Clang 16.0 | 865 → 263 | 3,29
Clang 18.1 | 756 → 265 | 2,85
Clang 19.1 | 760 → 263 | 2,89

On my Mac M4 it takes 257 -> 170 ms

--
Best regards,
Kirill Reshke