diff --git a/src/test/modules/test_sort_perf/Makefile b/src/test/modules/test_sort_perf/Makefile index ab7f8e24b0..e70187f3a6 100644 --- a/src/test/modules/test_sort_perf/Makefile +++ b/src/test/modules/test_sort_perf/Makefile @@ -6,12 +6,15 @@ DATA = test_sort_perf--1.0.sql first: all -test_sort_perf.o: test_sort_object_include.c test_sort_itemptr_include.c +#test_sort_perf.o: test_sort_object_include.c test_sort_itemptr_include.c test_sort_int64_include.c +test_sort_perf.o: test_sort_int64_include.c -test_sort_object_include.c: make-object-tests.sh - ./make-object-tests.sh > test_sort_object_include.c -test_sort_itemptr_include.c: make-itemptr-tests.sh - ./make-itemptr-tests.sh > test_sort_itemptr_include.c +# test_sort_object_include.c: make-object-tests.sh +# ./make-object-tests.sh > test_sort_object_include.c +# test_sort_itemptr_include.c: make-itemptr-tests.sh +# ./make-itemptr-tests.sh > test_sort_itemptr_include.c +test_sort_int64_include.c: make-int32-tests.sh + ./make-int32-tests.sh > test_sort_int32_include.c ifdef USE_PGXS PG_CONFIG = pg_config diff --git a/src/test/modules/test_sort_perf/make-int32-tests.sh b/src/test/modules/test_sort_perf/make-int32-tests.sh new file mode 100755 index 0000000000..a861ef608a --- /dev/null +++ b/src/test/modules/test_sort_perf/make-int32-tests.sh @@ -0,0 +1,151 @@ +#!/bin/sh + +cat < + +/* static int itemptr_comparator(const void *a, const void *b) { @@ -27,14 +33,106 @@ itemptr_comparator(const void *a, const void *b) if (oa > ob) return 1; return 0; +}*/ + + +// standard comparators + +// comparator for qsort_arg() +// XXX rewritten from the version in nbtutils.c +static int +btint4fastcmp(const void * x, const void * y) +{ + int32 *a = (int32 *) x; + int32 *b = (int32 *) y; + + if (*a > *b) + return 1; + else if (*a == *b) + return 0; + else + return -1; } +// qsort with inlined comparator +#define ST_SORT qsort_int32 +#define ST_ELEMENT_TYPE Datum +#define ST_COMPARE(a, b) (btint4fastcmp(a, b)) +#define ST_SCOPE static +#define ST_DEFINE +#define ST_DECLARE +#include "lib/sort_template.h" + +// qsort with branchless comparator +#define ST_SORT qsort_int32_branchless +#define ST_ELEMENT_TYPE Datum +#define ST_COMPARE(a, b) ((int64) *(a) - (int64) *(b)) +#define ST_COMPARE_RET_TYPE int64 +#define ST_SCOPE static +#define ST_DEFINE +#define ST_DECLARE +#include "lib/sort_template.h" + + +// SQL-callable comparators + +typedef struct BTSortArrayContext +{ + FmgrInfo flinfo; + Oid collation; + bool reverse; +} BTSortArrayContext; + +// comparator for qsort arg +static int +_bt_compare_array_elements(const void *a, const void *b, void *arg) +{ + Datum da = *((const Datum *) a); + Datum db = *((const Datum *) b); + BTSortArrayContext *cxt = (BTSortArrayContext *) arg; + int32 compare; + + compare = DatumGetInt32(FunctionCall2Coll(&cxt->flinfo, + cxt->collation, + da, db)); + if (cxt->reverse) + INVERT_COMPARE_RESULT(compare); + return compare; +} + + +/* Define a specialized sort function for _bt_sort_array_elements. */ +#define ST_SORT qsort_bt_array_elements +//#define ST_UNIQUE unique_bt_array_elements +#define ST_ELEMENT_TYPE Datum +#define ST_COMPARE(a, b, cxt) \ + DatumGetInt32(FunctionCall2Coll(&cxt->flinfo, cxt->collation, *a, *b)) +#define ST_COMPARE_ARG_TYPE BTSortArrayContext +#define ST_SCOPE static +#define ST_DEFINE +#include "lib/sort_template.h" + +// inline the reversal +#define ST_SORT qsort_bt_array_elements_reverse +//#define ST_UNIQUE unique_bt_array_elements_reverse +#define ST_ELEMENT_TYPE Datum +#define ST_COMPARE(a, b, cxt) \ + (0 - (DatumGetInt32(FunctionCall2Coll(&cxt->flinfo, cxt->collation, *a, *b)))) +#define ST_COMPARE_RET_TYPE int64 +#define ST_COMPARE_ARG_TYPE BTSortArrayContext +#define ST_SCOPE static +#define ST_DEFINE +#include "lib/sort_template.h" + + PG_MODULE_MAGIC; /* include the generated code */ -#include "test_sort_object_include.c" -#include "test_sort_itemptr_include.c" +// #include "test_sort_object_include.c" +// #include "test_sort_itemptr_include.c" +#include "test_sort_int32_include.c" +/* PG_FUNCTION_INFO_V1(test_sort_object); PG_FUNCTION_INFO_V1(test_sort_itemptr); @@ -53,3 +151,15 @@ test_sort_itemptr(PG_FUNCTION_ARGS) PG_RETURN_NULL(); } +*/ + + +PG_FUNCTION_INFO_V1(test_sort_int32); +Datum +test_sort_int32(PG_FUNCTION_ARGS) +{ + const int32 nmegs = PG_GETARG_INT32(0); + + do_sort_int32(nmegs); + PG_RETURN_NULL(); +}