From efd1cb010809678e080a647b1581dc5cc61da959 Mon Sep 17 00:00:00 2001 From: "Andrey M. Borodin" Date: Sat, 18 May 2024 23:02:50 +0500 Subject: [PATCH v4] Use specialized sort facilities --- contrib/intarray/_int.h | 8 ++--- contrib/intarray/_int_tool.c | 12 ------- src/backend/utils/sort/qsort_interruptible.c | 34 ++++++++++++++++++++ src/include/port.h | 1 + 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/contrib/intarray/_int.h b/contrib/intarray/_int.h index 0352cbd368..fb965cb035 100644 --- a/contrib/intarray/_int.h +++ b/contrib/intarray/_int.h @@ -42,7 +42,7 @@ typedef struct do { \ int _nelems_ = ARRNELEMS(x); \ if (_nelems_ > 1) \ - isort(ARRPTR(x), _nelems_); \ + sort_int32(ARRPTR(x), _nelems_, true); \ } while(0) /* sort the elements of the array and remove duplicates */ @@ -176,16 +176,12 @@ bool execconsistent(QUERYTYPE *query, ArrayType *array, bool calcnot); bool gin_bool_consistent(QUERYTYPE *query, bool *check); bool query_has_required_values(QUERYTYPE *query); -int compASC(const void *a, const void *b); -int compDESC(const void *a, const void *b); - /* sort, either ascending or descending */ #define QSORT(a, direction) \ do { \ int _nelems_ = ARRNELEMS(a); \ if (_nelems_ > 1) \ - qsort((void*) ARRPTR(a), _nelems_, sizeof(int32), \ - (direction) ? compASC : compDESC ); \ + sort_int32(ARRPTR(a), _nelems_, direction); \ } while(0) #endif /* ___INT_H__ */ diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c index c85280c842..e83c6aadc6 100644 --- a/contrib/intarray/_int_tool.c +++ b/contrib/intarray/_int_tool.c @@ -393,15 +393,3 @@ int_to_intset(int32 elem) aa[0] = elem; return result; } - -int -compASC(const void *a, const void *b) -{ - return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b); -} - -int -compDESC(const void *a, const void *b) -{ - return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a); -} diff --git a/src/backend/utils/sort/qsort_interruptible.c b/src/backend/utils/sort/qsort_interruptible.c index f179b25624..b2d984acc2 100644 --- a/src/backend/utils/sort/qsort_interruptible.c +++ b/src/backend/utils/sort/qsort_interruptible.c @@ -14,3 +14,37 @@ #define ST_DEFINE #define ST_CHECK_FOR_INTERRUPTS #include "lib/sort_template.h" + +static inline int +sort_int32_cmp(int32* a, int32* b, bool* ascending) +{ + if (*ascending) + { + if (*a < *b) + return -1; + if (*a > *b) + return 1; + } + else + { + if (*a < *b) + return 1; + if (*a > *b) + return -1; + } + return 0; +} + +#define ST_SORT sort_int32_impl +#define ST_ELEMENT_TYPE int32 +#define ST_COMPARE(a, b, ascending) sort_int32_cmp(a, b, ascending) +#define ST_COMPARE_ARG_TYPE bool +#define ST_SCOPE static +#define ST_DECLARE +#define ST_DEFINE +#include "lib/sort_template.h" + +void sort_int32(int32 *base, size_t nel, bool ascending) +{ + sort_int32_impl(base, nel, &ascending); +} diff --git a/src/include/port.h b/src/include/port.h index ba9ab0d34f..72dc42daa6 100644 --- a/src/include/port.h +++ b/src/include/port.h @@ -443,6 +443,7 @@ extern char *strsep(char **stringp, const char *delim); extern void pg_qsort(void *base, size_t nel, size_t elsize, int (*cmp) (const void *, const void *)); extern int pg_qsort_strcmp(const void *a, const void *b); +extern void sort_int32(int32 *base, size_t nel, bool ascending); #define qsort(a,b,c,d) pg_qsort(a,b,c,d) -- 2.39.5 (Apple Git-154)