Content-Length: 888707 | pFad | http://github.com/postgrespro/postgres/commit/3b42bdb47169c617cb79123c407a19d16458b49a

9F Use new overflow-safe integer comparison functions. · postgrespro/postgres@3b42bdb · GitHub
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

File tree

34 files changed

+80
-159
lines changed

34 files changed

+80
-159
lines changed

contrib/hstore/hstore_gist.c

Lines changed: 3 additions & 1 deletion
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

Lines changed: 3 additions & 6 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 2 additions & 6 deletions
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

Lines changed: 2 additions & 6 deletions
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

Lines changed: 2 additions & 6 deletions
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

Lines changed: 3 additions & 7 deletions
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

Lines changed: 2 additions & 6 deletions
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 2 additions & 1 deletion
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

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/postgrespro/postgres/commit/3b42bdb47169c617cb79123c407a19d16458b49a

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy