Skip to content

Commit 3b42bdb

Browse files
Use new overflow-safe integer comparison functions.
Commit 6b80394 introduced integer comparison functions designed to be as efficient as possible while avoiding overflow. This commit makes use of these functions in many of the in-tree qsort() comparators to help ensure transitivity. Many of these comparator functions should also see a small performance boost. Author: Mats Kindahl Reviewed-by: Andres Freund, Fabrízio de Royes Mello Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
1 parent 6b80394 commit 3b42bdb

34 files changed

+80
-159
lines changed

contrib/hstore/hstore_gist.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "access/reloptions.h"
88
#include "access/stratnum.h"
99
#include "catalog/pg_type.h"
10+
#include "common/int.h"
1011
#include "hstore.h"
1112
#include "utils/pg_crc.h"
1213

@@ -356,7 +357,8 @@ typedef struct
356357
static int
357358
comparecost(const void *a, const void *b)
358359
{
359-
return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
360+
return pg_cmp_s32(((const SPLITCOST *) a)->cost,
361+
((const SPLITCOST *) b)->cost);
360362
}
361363

362364

contrib/intarray/_int_tool.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "_int.h"
99
#include "catalog/pg_type.h"
10+
#include "common/int.h"
1011
#include "lib/qunique.h"
1112

1213
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
396397
int
397398
compASC(const void *a, const void *b)
398399
{
399-
if (*(const int32 *) a == *(const int32 *) b)
400-
return 0;
401-
return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
400+
return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
402401
}
403402

404403
int
405404
compDESC(const void *a, const void *b)
406405
{
407-
if (*(const int32 *) a == *(const int32 *) b)
408-
return 0;
409-
return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
406+
return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
410407
}

contrib/intarray/_intbig_gist.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "access/gist.h"
1010
#include "access/reloptions.h"
1111
#include "access/stratnum.h"
12+
#include "common/int.h"
1213
#include "port/pg_bitutils.h"
1314

1415
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
312313
static int
313314
comparecost(const void *a, const void *b)
314315
{
315-
return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
316+
return pg_cmp_s32(((const SPLITCOST *) a)->cost,
317+
((const SPLITCOST *) b)->cost);
316318
}
317319

318320

contrib/pg_stat_statements/pg_stat_statements.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "access/parallel.h"
5151
#include "catalog/pg_authid.h"
5252
#include "common/hashfn.h"
53+
#include "common/int.h"
5354
#include "executor/instrument.h"
5455
#include "funcapi.h"
5556
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
30073008
int l = ((const LocationLen *) a)->location;
30083009
int r = ((const LocationLen *) b)->location;
30093010

3010-
if (l < r)
3011-
return -1;
3012-
else if (l > r)
3013-
return +1;
3014-
else
3015-
return 0;
3011+
return pg_cmp_s32(l, r);
30163012
}

contrib/pg_trgm/trgm_op.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <ctype.h>
77

88
#include "catalog/pg_type.h"
9+
#include "common/int.h"
910
#include "lib/qunique.h"
1011
#include "miscadmin.h"
1112
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
433434
if (cmp != 0)
434435
return cmp;
435436

436-
if (p1->index < p2->index)
437-
return -1;
438-
else if (p1->index == p2->index)
439-
return 0;
440-
else
441-
return 1;
437+
return pg_cmp_s32(p1->index, p2->index);
442438
}
443439

444440
/*

src/backend/access/nbtree/nbtinsert.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "access/nbtxlog.h"
2020
#include "access/transam.h"
2121
#include "access/xloginsert.h"
22+
#include "common/int.h"
2223
#include "common/pg_prng.h"
2324
#include "lib/qunique.h"
2425
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
30133014
BlockNumber b1 = *((BlockNumber *) arg1);
30143015
BlockNumber b2 = *((BlockNumber *) arg2);
30153016

3016-
if (b1 < b2)
3017-
return -1;
3018-
else if (b1 > b2)
3019-
return 1;
3020-
3021-
return 0;
3017+
return pg_cmp_u32(b1, b2);
30223018
}

src/backend/access/nbtree/nbtpage.c

+3-7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "access/transam.h"
2929
#include "access/xlog.h"
3030
#include "access/xloginsert.h"
31+
#include "common/int.h"
3132
#include "miscadmin.h"
3233
#include "storage/indexfsm.h"
3334
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
14661467
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
14671468
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
14681469

1469-
if (indexdelete1->id > indexdelete2->id)
1470-
return 1;
1471-
if (indexdelete1->id < indexdelete2->id)
1472-
return -1;
1470+
Assert(indexdelete1->id != indexdelete2->id);
14731471

1474-
Assert(false);
1475-
1476-
return 0;
1472+
return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
14771473
}
14781474

14791475
/*

src/backend/access/nbtree/nbtsplitloc.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "postgres.h"
1616

1717
#include "access/nbtree.h"
18+
#include "common/int.h"
1819
#include "storage/lmgr.h"
1920

2021
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
596597
SplitPoint *split1 = (SplitPoint *) arg1;
597598
SplitPoint *split2 = (SplitPoint *) arg2;
598599

599-
if (split1->curdelta > split2->curdelta)
600-
return 1;
601-
if (split1->curdelta < split2->curdelta)
602-
return -1;
603-
604-
return 0;
600+
return pg_cmp_s16(split1->curdelta, split2->curdelta);
605601
}
606602

607603
/*

src/backend/access/spgist/spgdoinsert.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "access/spgist_private.h"
2020
#include "access/spgxlog.h"
2121
#include "access/xloginsert.h"
22+
#include "common/int.h"
2223
#include "common/pg_prng.h"
2324
#include "miscadmin.h"
2425
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
110111
static int
111112
cmpOffsetNumbers(const void *a, const void *b)
112113
{
113-
if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
114-
return 0;
115-
return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
114+
return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
116115
}
117116

118117
/*

src/backend/access/spgist/spgtextproc.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "postgres.h"
4141

4242
#include "access/spgist.h"
43+
#include "common/int.h"
4344
#include "catalog/pg_type.h"
4445
#include "mb/pg_wchar.h"
4546
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
325326
const spgNodePtr *aa = (const spgNodePtr *) a;
326327
const spgNodePtr *bb = (const spgNodePtr *) b;
327328

328-
return aa->c - bb->c;
329+
return pg_cmp_s16(aa->c, bb->c);
329330
}
330331

331332
Datum

src/backend/backup/basebackup_incremental.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "common/blkreftable.h"
2828
#include "common/parse_manifest.h"
2929
#include "common/hashfn.h"
30+
#include "common/int.h"
3031
#include "postmaster/walsummarizer.h"
3132

3233
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
994995
BlockNumber aa = *(BlockNumber *) a;
995996
BlockNumber bb = *(BlockNumber *) b;
996997

997-
if (aa > bb)
998-
return 1;
999-
else if (aa == bb)
1000-
return 0;
1001-
else
1002-
return -1;
998+
return pg_cmp_u32(aa, bb);
1003999
}

src/backend/backup/walsummary.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "access/xlog_internal.h"
1919
#include "backup/walsummary.h"
20+
#include "common/int.h"
2021
#include "utils/wait_event.h"
2122

2223
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
355356
WalSummaryFile *ws1 = lfirst(a);
356357
WalSummaryFile *ws2 = lfirst(b);
357358

358-
if (ws1->start_lsn < ws2->start_lsn)
359-
return -1;
360-
if (ws1->start_lsn > ws2->start_lsn)
361-
return 1;
362-
return 0;
359+
return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
363360
}

src/backend/catalog/heap.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include "catalog/storage.h"
5757
#include "commands/tablecmds.h"
5858
#include "commands/typecmds.h"
59+
#include "common/int.h"
5960
#include "miscadmin.h"
6061
#include "nodes/nodeFuncs.h"
6162
#include "optimizer/optimizer.h"
@@ -2762,11 +2763,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
27622763
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
27632764
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
27642765

2765-
if (v1 < v2)
2766-
return -1;
2767-
if (v1 > v2)
2768-
return 1;
2769-
return 0;
2766+
return pg_cmp_s16(v1, v2);
27702767
}
27712768

27722769
/*

src/backend/nodes/list.c

+3-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
#include "postgres.h"
1919

20+
#include "common/int.h"
2021
#include "nodes/pg_list.h"
2122
#include "port/pg_bitutils.h"
2223
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
16921693
int v1 = lfirst_int(p1);
16931694
int v2 = lfirst_int(p2);
16941695

1695-
if (v1 < v2)
1696-
return -1;
1697-
if (v1 > v2)
1698-
return 1;
1699-
return 0;
1696+
return pg_cmp_s32(v1, v2);
17001697
}
17011698

17021699
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
17081705
Oid v1 = lfirst_oid(p1);
17091706
Oid v2 = lfirst_oid(p2);
17101707

1711-
if (v1 < v2)
1712-
return -1;
1713-
if (v1 > v2)
1714-
return 1;
1715-
return 0;
1708+
return pg_cmp_u32(v1, v2);
17161709
}

src/backend/nodes/tidbitmap.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
#include "access/htup_details.h"
4444
#include "common/hashfn.h"
45+
#include "common/int.h"
4546
#include "nodes/bitmapset.h"
4647
#include "nodes/tidbitmap.h"
4748
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
14251426
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
14261427
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
14271428

1428-
if (l < r)
1429-
return -1;
1430-
else if (l > r)
1431-
return 1;
1432-
return 0;
1429+
return pg_cmp_u32(l, r);
14331430
}
14341431

14351432
/*

src/backend/parser/parse_agg.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "catalog/pg_aggregate.h"
1919
#include "catalog/pg_constraint.h"
2020
#include "catalog/pg_type.h"
21+
#include "common/int.h"
2122
#include "nodes/makefuncs.h"
2223
#include "nodes/nodeFuncs.h"
2324
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
17601761
int la = list_length((const List *) lfirst(a));
17611762
int lb = list_length((const List *) lfirst(b));
17621763

1763-
return (la > lb) ? 1 : (la == lb) ? 0 : -1;
1764+
return pg_cmp_s32(la, lb);
17641765
}
17651766

17661767
/* list_sort comparator to sort sub-lists by length and contents */

src/backend/postmaster/autovacuum.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
#include "catalog/pg_database.h"
7979
#include "commands/dbcommands.h"
8080
#include "commands/vacuum.h"
81+
#include "common/int.h"
8182
#include "lib/ilist.h"
8283
#include "libpq/pqsignal.h"
8384
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
11201121
static int
11211122
db_comparator(const void *a, const void *b)
11221123
{
1123-
if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
1124-
return 0;
1125-
else
1126-
return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
1124+
return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
1125+
((const avl_dbase *) b)->adl_score);
11271126
}
11281127

11291128
/*

src/backend/replication/logical/reorderbuffer.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
#include "access/xact.h"
9292
#include "access/xlog_internal.h"
9393
#include "catalog/catalog.h"
94+
#include "common/int.h"
9495
#include "lib/binaryheap.h"
9596
#include "miscadmin.h"
9697
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
51195120
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
51205121
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
51215122

5122-
if (a->lsn < b->lsn)
5123-
return -1;
5124-
else if (a->lsn > b->lsn)
5125-
return 1;
5126-
return 0;
5123+
return pg_cmp_u64(a->lsn, b->lsn);
51275124
}
51285125

51295126
/*

0 commit comments

Comments
 (0)