From b73f679c39671fe3b01652864dd67d8d6a732327 Mon Sep 17 00:00:00 2001 From: David Geier Date: Wed, 12 Nov 2025 14:27:13 +0100 Subject: [PATCH v3 4/6] Faster qunique() comparator in generate_trgm() --- contrib/pg_trgm/trgm_op.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c index 6af120fa1ad..d616cf3845f 100644 --- a/contrib/pg_trgm/trgm_op.c +++ b/contrib/pg_trgm/trgm_op.c @@ -139,6 +139,14 @@ CMPTRGM_UNSIGNED(const void *a, const void *b) : CMPPCHAR_UNS(a, b, 2)); } +static inline int +CMPTRGM_EQ(const void *a, const void *b) +{ + char *aa = (char *)a; + char *bb = (char *)b; + return aa[0] != bb[0] || aa[1] != bb[1] || aa[2] != bb[2] ? 1 : 0; +} + /* * This gets called on the first call. It replaces the function pointer so * that subsequent calls are routed directly to the chosen implementation. @@ -438,15 +446,11 @@ generate_trgm(char *str, int slen) if (len > 1) { if (GetDefaultCharSignedness()) - { trigram_qsort_signed((void *) GETARR(trg), len, sizeof(trgm)); - len = qunique(GETARR(trg), len, sizeof(trgm), CMPTRGM_SIGNED); - } else - { trigram_qsort_unsigned((void *) GETARR(trg), len, sizeof(trgm)); - len = qunique(GETARR(trg), len, sizeof(trgm), CMPTRGM_UNSIGNED); - } + + len = qunique(GETARR(trg), len, sizeof(trgm), CMPTRGM_EQ); } SET_VARSIZE(trg, CALCGTSIZE(ARRKEY, len)); @@ -994,15 +998,11 @@ generate_wildcard_trgm(const char *str, int slen) if (len > 1) { if (GetDefaultCharSignedness()) - { trigram_qsort_signed((void *) GETARR(trg), len, sizeof(trgm)); - len = qunique(GETARR(trg), len, sizeof(trgm), CMPTRGM_SIGNED); - } else - { trigram_qsort_unsigned((void *) GETARR(trg), len, sizeof(trgm)); - len = qunique(GETARR(trg), len, sizeof(trgm), CMPTRGM_UNSIGNED); - } + + len = qunique(GETARR(trg), len, sizeof(trgm), CMPTRGM_EQ); } SET_VARSIZE(trg, CALCGTSIZE(ARRKEY, len)); -- 2.51.0