diff options
author | Nathan Bossart | 2024-02-16 20:05:36 +0000 |
---|---|---|
committer | Nathan Bossart | 2024-02-16 20:05:36 +0000 |
commit | 3b42bdb47169c617cb79123c407a19d16458b49a (patch) | |
tree | 90b3448bf3d59ab3bdc5996f2bb43fc83bcfe458 /contrib | |
parent | 6b80394781c8de17fe7cae6996476088af3c319f (diff) |
Use new overflow-safe integer comparison functions.
Commit 6b80394781 introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators to help ensure transitivity. Many of these comparator
functions should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Andres Freund, FabrÃzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/hstore/hstore_gist.c | 4 | ||||
-rw-r--r-- | contrib/intarray/_int_tool.c | 9 | ||||
-rw-r--r-- | contrib/intarray/_intbig_gist.c | 4 | ||||
-rw-r--r-- | contrib/pg_stat_statements/pg_stat_statements.c | 8 | ||||
-rw-r--r-- | contrib/pg_trgm/trgm_op.c | 8 |
5 files changed, 13 insertions, 20 deletions
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c index fe343739eb0..a3b08af3850 100644 --- a/contrib/hstore/hstore_gist.c +++ b/contrib/hstore/hstore_gist.c @@ -7,6 +7,7 @@ #include "access/reloptions.h" #include "access/stratnum.h" #include "catalog/pg_type.h" +#include "common/int.h" #include "hstore.h" #include "utils/pg_crc.h" @@ -356,7 +357,8 @@ typedef struct static int comparecost(const void *a, const void *b) { - return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost; + return pg_cmp_s32(((const SPLITCOST *) a)->cost, + ((const SPLITCOST *) b)->cost); } diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c index 68f624e085c..c85280c8422 100644 --- a/contrib/intarray/_int_tool.c +++ b/contrib/intarray/_int_tool.c @@ -7,6 +7,7 @@ #include "_int.h" #include "catalog/pg_type.h" +#include "common/int.h" #include "lib/qunique.h" /* arguments are assumed sorted & unique-ified */ @@ -396,15 +397,11 @@ int_to_intset(int32 elem) int compASC(const void *a, const void *b) { - if (*(const int32 *) a == *(const int32 *) b) - return 0; - return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1; + return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b); } int compDESC(const void *a, const void *b) { - if (*(const int32 *) a == *(const int32 *) b) - return 0; - return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1; + return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a); } diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c index 8c6c4b5d051..9699fbf3b4f 100644 --- a/contrib/intarray/_intbig_gist.c +++ b/contrib/intarray/_intbig_gist.c @@ -9,6 +9,7 @@ #include "access/gist.h" #include "access/reloptions.h" #include "access/stratnum.h" +#include "common/int.h" #include "port/pg_bitutils.h" #define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key)) @@ -312,7 +313,8 @@ typedef struct static int comparecost(const void *a, const void *b) { - return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost; + return pg_cmp_s32(((const SPLITCOST *) a)->cost, + ((const SPLITCOST *) b)->cost); } diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c index 8c6a3a2d087..67cec865ba1 100644 --- a/contrib/pg_stat_statements/pg_stat_statements.c +++ b/contrib/pg_stat_statements/pg_stat_statements.c @@ -50,6 +50,7 @@ #include "access/parallel.h" #include "catalog/pg_authid.h" #include "common/hashfn.h" +#include "common/int.h" #include "executor/instrument.h" #include "funcapi.h" #include "jit/jit.h" @@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b) int l = ((const LocationLen *) a)->location; int r = ((const LocationLen *) b)->location; - if (l < r) - return -1; - else if (l > r) - return +1; - else - return 0; + return pg_cmp_s32(l, r); } diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c index 49d4497b4f3..c509d15ee40 100644 --- a/contrib/pg_trgm/trgm_op.c +++ b/contrib/pg_trgm/trgm_op.c @@ -6,6 +6,7 @@ #include <ctype.h> #include "catalog/pg_type.h" +#include "common/int.h" #include "lib/qunique.h" #include "miscadmin.h" #include "trgm.h" @@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2) if (cmp != 0) return cmp; - if (p1->index < p2->index) - return -1; - else if (p1->index == p2->index) - return 0; - else - return 1; + return pg_cmp_s32(p1->index, p2->index); } /* |