1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-13 07:41:39 +03:00

Specialize intarray sorting

There is at least one report in the field of storing millions of
integers in arrays, so it seems like a good time to specialize
intarray's qsort function. In doing so, streamline the comparators:
Previously there were three, two for each direction for sorting
and one passed to qunique_arg. To preserve the early exit in the
case of descending input, pass the direction as an argument to
the comparator. This requires giving up duplicate detection, which
previously allowed skipping the qunique_arg() call. Testing showed
no regressions this way.

In passing, get rid of nearby checks that the input has at least
two elements, since preserving them would make some macros less
readable. These are not necessary for correctness, and seem like
premature optimizations.

Author: Andrey M. Borodin <x4mmm@yandex-team.ru>
Discussion: https://postgr.es/m/098A3E67-E4A6-4086-9C66-B1EAEB1DFE1C@yandex-team.ru
This commit is contained in:
John Naylor
2025-02-18 11:04:55 +07:00
parent 164bac92f0
commit 53d3daa491
2 changed files with 34 additions and 48 deletions

View File

@ -41,17 +41,17 @@ typedef struct
#define SORT(x) \ #define SORT(x) \
do { \ do { \
int _nelems_ = ARRNELEMS(x); \ int _nelems_ = ARRNELEMS(x); \
if (_nelems_ > 1) \ bool _ascending = true; \
isort(ARRPTR(x), _nelems_); \ isort(ARRPTR(x), _nelems_, &_ascending); \
} while(0) } while(0)
/* sort the elements of the array and remove duplicates */ /* sort the elements of the array and remove duplicates */
#define PREPAREARR(x) \ #define PREPAREARR(x) \
do { \ do { \
int _nelems_ = ARRNELEMS(x); \ int _nelems_ = ARRNELEMS(x); \
if (_nelems_ > 1) \ bool _ascending = true; \
if (isort(ARRPTR(x), _nelems_)) \ isort(ARRPTR(x), _nelems_, &_ascending); \
(x) = _int_unique(x); \ (x) = _int_unique(x); \
} while(0) } while(0)
/* "wish" function */ /* "wish" function */
@ -109,7 +109,7 @@ typedef struct
/* /*
* useful functions * useful functions
*/ */
bool isort(int32 *a, int len); void isort(int32 *a, size_t len, void *arg);
ArrayType *new_intArrayType(int num); ArrayType *new_intArrayType(int num);
ArrayType *copy_intArrayType(ArrayType *a); ArrayType *copy_intArrayType(ArrayType *a);
ArrayType *resize_intArrayType(ArrayType *a, int num); ArrayType *resize_intArrayType(ArrayType *a, int num);
@ -176,16 +176,12 @@ bool execconsistent(QUERYTYPE *query, ArrayType *array, bool calcnot);
bool gin_bool_consistent(QUERYTYPE *query, bool *check); bool gin_bool_consistent(QUERYTYPE *query, bool *check);
bool query_has_required_values(QUERYTYPE *query); 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 */ /* sort, either ascending or descending */
#define QSORT(a, direction) \ #define QSORT(a, direction) \
do { \ do { \
int _nelems_ = ARRNELEMS(a); \ int _nelems_ = ARRNELEMS(a); \
if (_nelems_ > 1) \ bool _ascending = (direction) ? true : false; \
qsort((void*) ARRPTR(a), _nelems_, sizeof(int32), \ isort(ARRPTR(a), _nelems_, &_ascending); \
(direction) ? compASC : compDESC ); \
} while(0) } while(0)
#endif /* ___INT_H__ */ #endif /* ___INT_H__ */

View File

@ -186,36 +186,38 @@ rt__int_size(ArrayType *a, float *size)
*size = (float) ARRNELEMS(a); *size = (float) ARRNELEMS(a);
} }
/* qsort_arg comparison function for isort() */ /* comparison function for isort() and _int_unique() */
static int static inline int
isort_cmp(const void *a, const void *b, void *arg) isort_cmp(const void *a, const void *b, void *arg)
{ {
int32 aval = *((const int32 *) a); int32 aval = *((const int32 *) a);
int32 bval = *((const int32 *) b); int32 bval = *((const int32 *) b);
if (aval < bval) if (*((bool *) arg))
return -1; {
if (aval > bval) /* compare for ascending order */
return 1; if (aval < bval)
return -1;
/* if (aval > bval)
* Report if we have any duplicates. If there are equal keys, qsort must return 1;
* compare them at some point, else it wouldn't know whether one should go }
* before or after the other. else
*/ {
*((bool *) arg) = true; if (aval > bval)
return -1;
if (aval < bval)
return 1;
}
return 0; return 0;
} }
/* Sort the given data (len >= 2). Return true if any duplicates found */ #define ST_SORT isort
bool #define ST_ELEMENT_TYPE int32
isort(int32 *a, int len) #define ST_COMPARE(a, b, ascending) isort_cmp(a, b, ascending)
{ #define ST_COMPARE_ARG_TYPE void
bool r = false; #define ST_SCOPE
#define ST_DEFINE
qsort_arg(a, len, sizeof(int32), isort_cmp, &r); #include "lib/sort_template.h"
return r;
}
/* Create a new int array with room for "num" elements */ /* Create a new int array with room for "num" elements */
ArrayType * ArrayType *
@ -311,10 +313,10 @@ ArrayType *
_int_unique(ArrayType *r) _int_unique(ArrayType *r)
{ {
int num = ARRNELEMS(r); int num = ARRNELEMS(r);
bool duplicates_found; /* not used */ bool ascending = true;
num = qunique_arg(ARRPTR(r), num, sizeof(int), isort_cmp, num = qunique_arg(ARRPTR(r), num, sizeof(int), isort_cmp,
&duplicates_found); &ascending);
return resize_intArrayType(r, num); return resize_intArrayType(r, num);
} }
@ -393,15 +395,3 @@ int_to_intset(int32 elem)
aa[0] = elem; aa[0] = elem;
return result; 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);
}