sort_template.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*-------------------------------------------------------------------------
  2. *
  3. * sort_template.h
  4. *
  5. * A template for a sort algorithm that supports varying degrees of
  6. * specialization.
  7. *
  8. * Copyright (c) 2021-2022, PostgreSQL Global Development Group
  9. * Portions Copyright (c) 1992-1994, Regents of the University of California
  10. *
  11. * Usage notes:
  12. *
  13. * To generate functions specialized for a type, the following parameter
  14. * macros should be #define'd before this file is included.
  15. *
  16. * - ST_SORT - the name of a sort function to be generated
  17. * - ST_ELEMENT_TYPE - type of the referenced elements
  18. * - ST_DECLARE - if defined the functions and types are declared
  19. * - ST_DEFINE - if defined the functions and types are defined
  20. * - ST_SCOPE - scope (e.g. extern, static inline) for functions
  21. * - ST_CHECK_FOR_INTERRUPTS - if defined the sort is interruptible
  22. *
  23. * Instead of ST_ELEMENT_TYPE, ST_ELEMENT_TYPE_VOID can be defined. Then
  24. * the generated functions will automatically gain an "element_size"
  25. * parameter. This allows us to generate a traditional qsort function.
  26. *
  27. * One of the following macros must be defined, to show how to compare
  28. * elements. The first two options are arbitrary expressions depending
  29. * on whether an extra pass-through argument is desired, and the third
  30. * option should be defined if the sort function should receive a
  31. * function pointer at runtime.
  32. *
  33. * - ST_COMPARE(a, b) - a simple comparison expression
  34. * - ST_COMPARE(a, b, arg) - variant that takes an extra argument
  35. * - ST_COMPARE_RUNTIME_POINTER - sort function takes a function pointer
  36. *
  37. * To say that the comparator and therefore also sort function should
  38. * receive an extra pass-through argument, specify the type of the
  39. * argument.
  40. *
  41. * - ST_COMPARE_ARG_TYPE - type of extra argument
  42. *
  43. * The prototype of the generated sort function is:
  44. *
  45. * void ST_SORT(ST_ELEMENT_TYPE *data, size_t n,
  46. * [size_t element_size,]
  47. * [ST_SORT_compare_function compare,]
  48. * [ST_COMPARE_ARG_TYPE *arg]);
  49. *
  50. * ST_SORT_compare_function is a function pointer of the following type:
  51. *
  52. * int (*)(const ST_ELEMENT_TYPE *a, const ST_ELEMENT_TYPE *b,
  53. * [ST_COMPARE_ARG_TYPE *arg])
  54. *
  55. * HISTORY
  56. *
  57. * Modifications from vanilla NetBSD source:
  58. * - Add do ... while() macro fix
  59. * - Remove __inline, _DIAGASSERTs, __P
  60. * - Remove ill-considered "swap_cnt" switch to insertion sort, in favor
  61. * of a simple check for presorted input.
  62. * - Take care to recurse on the smaller partition, to bound stack usage
  63. * - Convert into a header that can generate specialized functions
  64. *
  65. * IDENTIFICATION
  66. * src/include/lib/sort_template.h
  67. *
  68. *-------------------------------------------------------------------------
  69. */
  70. /* $NetBSD: qsort.c,v 1.13 2003/08/07 16:43:42 agc Exp $ */
  71. /*-
  72. * Copyright (c) 1992, 1993
  73. * The Regents of the University of California. All rights reserved.
  74. *
  75. * Redistribution and use in source and binary forms, with or without
  76. * modification, are permitted provided that the following conditions
  77. * are met:
  78. * 1. Redistributions of source code must retain the above copyright
  79. * notice, this list of conditions and the following disclaimer.
  80. * 2. Redistributions in binary form must reproduce the above copyright
  81. * notice, this list of conditions and the following disclaimer in the
  82. * documentation and/or other materials provided with the distribution.
  83. * 3. Neither the name of the University nor the names of its contributors
  84. * may be used to endorse or promote products derived from this software
  85. * without specific prior written permission.
  86. *
  87. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  88. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  89. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  90. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  91. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  92. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  93. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  94. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  95. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  96. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  97. * SUCH DAMAGE.
  98. */
  99. /*
  100. * Qsort routine based on J. L. Bentley and M. D. McIlroy,
  101. * "Engineering a sort function",
  102. * Software--Practice and Experience 23 (1993) 1249-1265.
  103. *
  104. * We have modified their original by adding a check for already-sorted
  105. * input, which seems to be a win per discussions on pgsql-hackers around
  106. * 2006-03-21.
  107. *
  108. * Also, we recurse on the smaller partition and iterate on the larger one,
  109. * which ensures we cannot recurse more than log(N) levels (since the
  110. * partition recursed to is surely no more than half of the input). Bentley
  111. * and McIlroy explicitly rejected doing this on the grounds that it's "not
  112. * worth the effort", but we have seen crashes in the field due to stack
  113. * overrun, so that judgment seems wrong.
  114. */
  115. #define ST_MAKE_PREFIX(a) CppConcat(a,_)
  116. #define ST_MAKE_NAME(a,b) ST_MAKE_NAME_(ST_MAKE_PREFIX(a),b)
  117. #define ST_MAKE_NAME_(a,b) CppConcat(a,b)
  118. /*
  119. * If the element type is void, we'll also need an element_size argument
  120. * because we don't know the size.
  121. */
  122. #ifdef ST_ELEMENT_TYPE_VOID
  123. #define ST_ELEMENT_TYPE void
  124. #define ST_SORT_PROTO_ELEMENT_SIZE , size_t element_size
  125. #define ST_SORT_INVOKE_ELEMENT_SIZE , element_size
  126. #else
  127. #define ST_SORT_PROTO_ELEMENT_SIZE
  128. #define ST_SORT_INVOKE_ELEMENT_SIZE
  129. #endif
  130. /*
  131. * If the user wants to be able to pass in compare functions at runtime,
  132. * we'll need to make that an argument of the sort and med3 functions.
  133. */
  134. #ifdef ST_COMPARE_RUNTIME_POINTER
  135. /*
  136. * The type of the comparator function pointer that ST_SORT will take, unless
  137. * you've already declared a type name manually and want to use that instead of
  138. * having a new one defined.
  139. */
  140. #ifndef ST_COMPARATOR_TYPE_NAME
  141. #define ST_COMPARATOR_TYPE_NAME ST_MAKE_NAME(ST_SORT, compare_function)
  142. #endif
  143. #define ST_COMPARE compare
  144. #ifndef ST_COMPARE_ARG_TYPE
  145. #define ST_SORT_PROTO_COMPARE , ST_COMPARATOR_TYPE_NAME compare
  146. #define ST_SORT_INVOKE_COMPARE , compare
  147. #else
  148. #define ST_SORT_PROTO_COMPARE , ST_COMPARATOR_TYPE_NAME compare
  149. #define ST_SORT_INVOKE_COMPARE , compare
  150. #endif
  151. #else
  152. #define ST_SORT_PROTO_COMPARE
  153. #define ST_SORT_INVOKE_COMPARE
  154. #endif
  155. /*
  156. * If the user wants to use a compare function or expression that takes an
  157. * extra argument, we'll need to make that an argument of the sort, compare and
  158. * med3 functions.
  159. */
  160. #ifdef ST_COMPARE_ARG_TYPE
  161. #define ST_SORT_PROTO_ARG , ST_COMPARE_ARG_TYPE *arg
  162. #define ST_SORT_INVOKE_ARG , arg
  163. #else
  164. #define ST_SORT_PROTO_ARG
  165. #define ST_SORT_INVOKE_ARG
  166. #endif
  167. #ifdef ST_DECLARE
  168. #ifdef ST_COMPARE_RUNTIME_POINTER
  169. typedef int (*ST_COMPARATOR_TYPE_NAME) (const ST_ELEMENT_TYPE *,
  170. const ST_ELEMENT_TYPE * ST_SORT_PROTO_ARG);
  171. #endif
  172. /* Declare the sort function. Note optional arguments at end. */
  173. ST_SCOPE void ST_SORT(ST_ELEMENT_TYPE * first, size_t n
  174. ST_SORT_PROTO_ELEMENT_SIZE
  175. ST_SORT_PROTO_COMPARE
  176. ST_SORT_PROTO_ARG);
  177. #endif
  178. #ifdef ST_DEFINE
  179. /* sort private helper functions */
  180. #define ST_MED3 ST_MAKE_NAME(ST_SORT, med3)
  181. #define ST_SWAP ST_MAKE_NAME(ST_SORT, swap)
  182. #define ST_SWAPN ST_MAKE_NAME(ST_SORT, swapn)
  183. /* Users expecting to run very large sorts may need them to be interruptible. */
  184. #ifdef ST_CHECK_FOR_INTERRUPTS
  185. #define DO_CHECK_FOR_INTERRUPTS() CHECK_FOR_INTERRUPTS()
  186. #else
  187. #define DO_CHECK_FOR_INTERRUPTS()
  188. #endif
  189. /*
  190. * Create wrapper macros that know how to invoke compare, med3 and sort with
  191. * the right arguments.
  192. */
  193. #ifdef ST_COMPARE_RUNTIME_POINTER
  194. #define DO_COMPARE(a_, b_) ST_COMPARE((a_), (b_) ST_SORT_INVOKE_ARG)
  195. #elif defined(ST_COMPARE_ARG_TYPE)
  196. #define DO_COMPARE(a_, b_) ST_COMPARE((a_), (b_), arg)
  197. #else
  198. #define DO_COMPARE(a_, b_) ST_COMPARE((a_), (b_))
  199. #endif
  200. #define DO_MED3(a_, b_, c_) \
  201. ST_MED3((a_), (b_), (c_) \
  202. ST_SORT_INVOKE_COMPARE \
  203. ST_SORT_INVOKE_ARG)
  204. #define DO_SORT(a_, n_) \
  205. ST_SORT((a_), (n_) \
  206. ST_SORT_INVOKE_ELEMENT_SIZE \
  207. ST_SORT_INVOKE_COMPARE \
  208. ST_SORT_INVOKE_ARG)
  209. /*
  210. * If we're working with void pointers, we'll use pointer arithmetic based on
  211. * uint8, and use the runtime element_size to step through the array and swap
  212. * elements. Otherwise we'll work with ST_ELEMENT_TYPE.
  213. */
  214. #ifndef ST_ELEMENT_TYPE_VOID
  215. #define ST_POINTER_TYPE ST_ELEMENT_TYPE
  216. #define ST_POINTER_STEP 1
  217. #define DO_SWAPN(a_, b_, n_) ST_SWAPN((a_), (b_), (n_))
  218. #define DO_SWAP(a_, b_) ST_SWAP((a_), (b_))
  219. #else
  220. #define ST_POINTER_TYPE uint8
  221. #define ST_POINTER_STEP element_size
  222. #define DO_SWAPN(a_, b_, n_) ST_SWAPN((a_), (b_), (n_))
  223. #define DO_SWAP(a_, b_) DO_SWAPN((a_), (b_), element_size)
  224. #endif
  225. /*
  226. * Find the median of three values. Currently, performance seems to be best
  227. * if the comparator is inlined here, but the med3 function is not inlined
  228. * in the qsort function.
  229. */
  230. static pg_noinline ST_ELEMENT_TYPE *
  231. ST_MED3(ST_ELEMENT_TYPE * a,
  232. ST_ELEMENT_TYPE * b,
  233. ST_ELEMENT_TYPE * c
  234. ST_SORT_PROTO_COMPARE
  235. ST_SORT_PROTO_ARG)
  236. {
  237. return DO_COMPARE(a, b) < 0 ?
  238. (DO_COMPARE(b, c) < 0 ? b : (DO_COMPARE(a, c) < 0 ? c : a))
  239. : (DO_COMPARE(b, c) > 0 ? b : (DO_COMPARE(a, c) < 0 ? a : c));
  240. }
  241. static inline void
  242. ST_SWAP(ST_POINTER_TYPE * a, ST_POINTER_TYPE * b)
  243. {
  244. ST_POINTER_TYPE tmp = *a;
  245. *a = *b;
  246. *b = tmp;
  247. }
  248. static inline void
  249. ST_SWAPN(ST_POINTER_TYPE * a, ST_POINTER_TYPE * b, size_t n)
  250. {
  251. for (size_t i = 0; i < n; ++i)
  252. ST_SWAP(&a[i], &b[i]);
  253. }
  254. /*
  255. * Sort an array.
  256. */
  257. ST_SCOPE void
  258. ST_SORT(ST_ELEMENT_TYPE * data, size_t n
  259. ST_SORT_PROTO_ELEMENT_SIZE
  260. ST_SORT_PROTO_COMPARE
  261. ST_SORT_PROTO_ARG)
  262. {
  263. ST_POINTER_TYPE *a = (ST_POINTER_TYPE *) data,
  264. *pa,
  265. *pb,
  266. *pc,
  267. *pd,
  268. *pl,
  269. *pm,
  270. *pn;
  271. size_t d1,
  272. d2;
  273. int r,
  274. presorted;
  275. loop:
  276. DO_CHECK_FOR_INTERRUPTS();
  277. if (n < 7)
  278. {
  279. for (pm = a + ST_POINTER_STEP; pm < a + n * ST_POINTER_STEP;
  280. pm += ST_POINTER_STEP)
  281. for (pl = pm; pl > a && DO_COMPARE(pl - ST_POINTER_STEP, pl) > 0;
  282. pl -= ST_POINTER_STEP)
  283. DO_SWAP(pl, pl - ST_POINTER_STEP);
  284. return;
  285. }
  286. presorted = 1;
  287. for (pm = a + ST_POINTER_STEP; pm < a + n * ST_POINTER_STEP;
  288. pm += ST_POINTER_STEP)
  289. {
  290. DO_CHECK_FOR_INTERRUPTS();
  291. if (DO_COMPARE(pm - ST_POINTER_STEP, pm) > 0)
  292. {
  293. presorted = 0;
  294. break;
  295. }
  296. }
  297. if (presorted)
  298. return;
  299. pm = a + (n / 2) * ST_POINTER_STEP;
  300. if (n > 7)
  301. {
  302. pl = a;
  303. pn = a + (n - 1) * ST_POINTER_STEP;
  304. if (n > 40)
  305. {
  306. size_t d = (n / 8) * ST_POINTER_STEP;
  307. pl = DO_MED3(pl, pl + d, pl + 2 * d);
  308. pm = DO_MED3(pm - d, pm, pm + d);
  309. pn = DO_MED3(pn - 2 * d, pn - d, pn);
  310. }
  311. pm = DO_MED3(pl, pm, pn);
  312. }
  313. DO_SWAP(a, pm);
  314. pa = pb = a + ST_POINTER_STEP;
  315. pc = pd = a + (n - 1) * ST_POINTER_STEP;
  316. for (;;)
  317. {
  318. while (pb <= pc && (r = DO_COMPARE(pb, a)) <= 0)
  319. {
  320. if (r == 0)
  321. {
  322. DO_SWAP(pa, pb);
  323. pa += ST_POINTER_STEP;
  324. }
  325. pb += ST_POINTER_STEP;
  326. DO_CHECK_FOR_INTERRUPTS();
  327. }
  328. while (pb <= pc && (r = DO_COMPARE(pc, a)) >= 0)
  329. {
  330. if (r == 0)
  331. {
  332. DO_SWAP(pc, pd);
  333. pd -= ST_POINTER_STEP;
  334. }
  335. pc -= ST_POINTER_STEP;
  336. DO_CHECK_FOR_INTERRUPTS();
  337. }
  338. if (pb > pc)
  339. break;
  340. DO_SWAP(pb, pc);
  341. pb += ST_POINTER_STEP;
  342. pc -= ST_POINTER_STEP;
  343. }
  344. pn = a + n * ST_POINTER_STEP;
  345. d1 = Min(pa - a, pb - pa);
  346. DO_SWAPN(a, pb - d1, d1);
  347. d1 = Min(pd - pc, pn - pd - ST_POINTER_STEP);
  348. DO_SWAPN(pb, pn - d1, d1);
  349. d1 = pb - pa;
  350. d2 = pd - pc;
  351. if (d1 <= d2)
  352. {
  353. /* Recurse on left partition, then iterate on right partition */
  354. if (d1 > ST_POINTER_STEP)
  355. DO_SORT(a, d1 / ST_POINTER_STEP);
  356. if (d2 > ST_POINTER_STEP)
  357. {
  358. /* Iterate rather than recurse to save stack space */
  359. /* DO_SORT(pn - d2, d2 / ST_POINTER_STEP) */
  360. a = pn - d2;
  361. n = d2 / ST_POINTER_STEP;
  362. goto loop;
  363. }
  364. }
  365. else
  366. {
  367. /* Recurse on right partition, then iterate on left partition */
  368. if (d2 > ST_POINTER_STEP)
  369. DO_SORT(pn - d2, d2 / ST_POINTER_STEP);
  370. if (d1 > ST_POINTER_STEP)
  371. {
  372. /* Iterate rather than recurse to save stack space */
  373. /* DO_SORT(a, d1 / ST_POINTER_STEP) */
  374. n = d1 / ST_POINTER_STEP;
  375. goto loop;
  376. }
  377. }
  378. }
  379. #endif
  380. #undef DO_CHECK_FOR_INTERRUPTS
  381. #undef DO_COMPARE
  382. #undef DO_MED3
  383. #undef DO_SORT
  384. #undef DO_SWAP
  385. #undef DO_SWAPN
  386. #undef ST_CHECK_FOR_INTERRUPTS
  387. #undef ST_COMPARATOR_TYPE_NAME
  388. #undef ST_COMPARE
  389. #undef ST_COMPARE_ARG_TYPE
  390. #undef ST_COMPARE_RUNTIME_POINTER
  391. #undef ST_ELEMENT_TYPE
  392. #undef ST_ELEMENT_TYPE_VOID
  393. #undef ST_MAKE_NAME
  394. #undef ST_MAKE_NAME_
  395. #undef ST_MAKE_PREFIX
  396. #undef ST_MED3
  397. #undef ST_POINTER_STEP
  398. #undef ST_POINTER_TYPE
  399. #undef ST_SCOPE
  400. #undef ST_SORT
  401. #undef ST_SORT_INVOKE_ARG
  402. #undef ST_SORT_INVOKE_COMPARE
  403. #undef ST_SORT_INVOKE_ELEMENT_SIZE
  404. #undef ST_SORT_PROTO_ARG
  405. #undef ST_SORT_PROTO_COMPARE
  406. #undef ST_SORT_PROTO_ELEMENT_SIZE
  407. #undef ST_SWAP
  408. #undef ST_SWAPN