From abbca3581624b70709bfcfcaad2343a2d78df78b Mon Sep 17 00:00:00 2001
From: Stepan Neretin <sncfmgg@gmail.com>
Date: Tue, 11 Jun 2024 12:00:11 +0700
Subject: [PATCH v42 07/12] Consolidate and optimize int16 array sorting

This commit optimizes int16 array sorting by consolidating the
compare_int16 function into a single location and removing duplicate
definitions across multiple files. Additionally, it introduces a
sorting template to enhance sorting performance.

  Changes:
- Consolidated the compare_int16 function into a single location.
- Removed duplicate definitions of compare_int16.
- Introduced a sorting template for int16 arrays to improve performance.
---
 src/backend/catalog/pg_publication.c | 11 ----------
 src/backend/commands/statscmds.c     | 12 ----------
 src/backend/utils/adt/numutils.c     | 33 ++++++++++++++++++++++++++++
 src/include/utils/builtins.h         |  1 +
 4 files changed, 34 insertions(+), 23 deletions(-)

diff --git a/src/backend/catalog/pg_publication.c b/src/backend/catalog/pg_publication.c
index faf70ec8c7..8518582b76 100644
--- a/src/backend/catalog/pg_publication.c
+++ b/src/backend/catalog/pg_publication.c
@@ -476,17 +476,6 @@ publication_add_relation(Oid pubid, PublicationRelInfo *pri,
 	return myself;
 }
 
-/* qsort comparator for attnums */
-static int
-compare_int16(const void *a, const void *b)
-{
-	int			av = *(const int16 *) a;
-	int			bv = *(const int16 *) b;
-
-	/* this can't overflow if int is wider than int16 */
-	return (av - bv);
-}
-
 /*
  * Translate a list of column names to an array of attribute numbers
  * and a Bitmapset with them; verify that each attribute is appropriate
diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c
index 1db3ef69d2..14d9b035b7 100644
--- a/src/backend/commands/statscmds.c
+++ b/src/backend/commands/statscmds.c
@@ -43,18 +43,6 @@ static char *ChooseExtendedStatisticName(const char *name1, const char *name2,
 										 const char *label, Oid namespaceid);
 static char *ChooseExtendedStatisticNameAddition(List *exprs);
 
-
-/* qsort comparator for the attnums in CreateStatistics */
-static int
-compare_int16(const void *a, const void *b)
-{
-	int			av = *(const int16 *) a;
-	int			bv = *(const int16 *) b;
-
-	/* this can't overflow if int is wider than int16 */
-	return (av - bv);
-}
-
 /*
  *		CREATE STATISTICS
  */
diff --git a/src/backend/utils/adt/numutils.c b/src/backend/utils/adt/numutils.c
index adc1e8a4cb..f538d1be2b 100644
--- a/src/backend/utils/adt/numutils.c
+++ b/src/backend/utils/adt/numutils.c
@@ -1312,3 +1312,36 @@ pg_ultostr(char *str, uint32 value)
 
 	return str + len;
 }
+
+/*
+ * compare_int16: Compare two int16 values.
+ *
+ * This function compares two int16 values passed as pointers. It first
+ * dereferences the pointers to obtain the actual int16 values, then
+ * subtracts one from the other to determine their relative ordering.
+ * Note:
+ *   - This function assumes that the underlying architecture represents
+ *     int16 values using a two's complement representation.
+ *   - It does not perform overflow checking, assuming that 'int' is
+ *     wider than 'int16'.
+ */
+static int
+compare_int16(const void *a, const void *b)
+{
+	int			av = *(const int16 *) a;
+	int			bv = *(const int16 *) b;
+
+	/* this can't overflow if int is wider than int16 */
+	return (av - bv);
+}
+
+ /* 
+ * Instantiating a Sorting Template for int16 Arrays
+ * enhancing speed performance.
+ */
+#define ST_SORT sort_int_16_arr
+#define ST_ELEMENT_TYPE int16
+#define ST_COMPARE(a, b) compare_int16(a, b)
+#define ST_SCOPE extern
+#define ST_DEFINE
+#include <lib/sort_template.h>
\ No newline at end of file
diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h
index 359c570f23..8ddbb8b142 100644
--- a/src/include/utils/builtins.h
+++ b/src/include/utils/builtins.h
@@ -42,6 +42,7 @@ extern uint64 hex_decode_safe(const char *src, size_t len, char *dst,
 
 /* int.c */
 extern int2vector *buildint2vector(const int16 *int2s, int n);
+extern void sort_int_16_arr(int16 *arr, size_t n);
 
 /* name.c */
 extern void namestrcpy(Name name, const char *str);
-- 
2.34.1

