mirror of
https://github.com/postgres/postgres.git
synced 2025-11-22 12:22:45 +03:00
Use new overflow-safe integer comparison functions.
Commit 6b80394781 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
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
#include <limits.h>
|
||||
|
||||
#include "catalog/pg_type.h"
|
||||
#include "common/int.h"
|
||||
#include "libpq/pqformat.h"
|
||||
#include "nodes/miscnodes.h"
|
||||
#include "nodes/value.h"
|
||||
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
|
||||
Oid v1 = *((const Oid *) p1);
|
||||
Oid v2 = *((const Oid *) p2);
|
||||
|
||||
if (v1 < v2)
|
||||
return -1;
|
||||
if (v1 > v2)
|
||||
return 1;
|
||||
return 0;
|
||||
return pg_cmp_u32(v1, v2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "access/gist.h"
|
||||
#include "access/heaptoast.h"
|
||||
#include "access/reloptions.h"
|
||||
#include "common/int.h"
|
||||
#include "lib/qunique.h"
|
||||
#include "port/pg_bitutils.h"
|
||||
#include "tsearch/ts_utils.h"
|
||||
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
|
||||
int32 a = *((const int32 *) va);
|
||||
int32 b = *((const int32 *) vb);
|
||||
|
||||
if (a == b)
|
||||
return 0;
|
||||
return (a > b) ? 1 : -1;
|
||||
return pg_cmp_s32(a, b);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
|
||||
const SPLITCOST *a = (const SPLITCOST *) va;
|
||||
const SPLITCOST *b = (const SPLITCOST *) vb;
|
||||
|
||||
if (a->cost == b->cost)
|
||||
return 0;
|
||||
else
|
||||
return (a->cost > b->cost) ? 1 : -1;
|
||||
return pg_cmp_s32(a->cost, b->cost);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
#include "access/gist.h"
|
||||
#include "access/stratnum.h"
|
||||
#include "common/int.h"
|
||||
#include "tsearch/ts_utils.h"
|
||||
#include "utils/builtins.h"
|
||||
|
||||
@@ -156,10 +157,8 @@ typedef struct
|
||||
static int
|
||||
comparecost(const void *a, const void *b)
|
||||
{
|
||||
if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
|
||||
return 0;
|
||||
else
|
||||
return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
|
||||
return pg_cmp_s32(((const SPLITCOST *) a)->cost,
|
||||
((const SPLITCOST *) b)->cost);
|
||||
}
|
||||
|
||||
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
#include "postgres.h"
|
||||
|
||||
#include "common/int.h"
|
||||
#include "libpq/pqformat.h"
|
||||
#include "nodes/miscnodes.h"
|
||||
#include "tsearch/ts_locale.h"
|
||||
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
|
||||
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
|
||||
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
|
||||
|
||||
if (apos == bpos)
|
||||
return 0;
|
||||
return (apos > bpos) ? 1 : -1;
|
||||
return pg_cmp_s32(apos, bpos);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "catalog/namespace.h"
|
||||
#include "catalog/pg_type.h"
|
||||
#include "commands/trigger.h"
|
||||
#include "common/int.h"
|
||||
#include "executor/spi.h"
|
||||
#include "funcapi.h"
|
||||
#include "lib/qunique.h"
|
||||
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
|
||||
int a = *((const int *) va);
|
||||
int b = *((const int *) vb);
|
||||
|
||||
if (a == b)
|
||||
return 0;
|
||||
return (a > b) ? 1 : -1;
|
||||
return pg_cmp_s32(a, b);
|
||||
}
|
||||
|
||||
static int
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "access/multixact.h"
|
||||
#include "access/transam.h"
|
||||
#include "access/xact.h"
|
||||
#include "common/int.h"
|
||||
#include "libpq/pqformat.h"
|
||||
#include "utils/builtins.h"
|
||||
#include "utils/xid8.h"
|
||||
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
|
||||
TransactionId xid1 = *(const TransactionId *) arg1;
|
||||
TransactionId xid2 = *(const TransactionId *) arg2;
|
||||
|
||||
if (xid1 > xid2)
|
||||
return 1;
|
||||
if (xid1 < xid2)
|
||||
return -1;
|
||||
return 0;
|
||||
return pg_cmp_u32(xid1, xid2);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
3
src/backend/utils/cache/relcache.c
vendored
3
src/backend/utils/cache/relcache.c
vendored
@@ -69,6 +69,7 @@
|
||||
#include "commands/policy.h"
|
||||
#include "commands/publicationcmds.h"
|
||||
#include "commands/trigger.h"
|
||||
#include "common/int.h"
|
||||
#include "miscadmin.h"
|
||||
#include "nodes/makefuncs.h"
|
||||
#include "nodes/nodeFuncs.h"
|
||||
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
|
||||
const AttrDefault *ada = (const AttrDefault *) a;
|
||||
const AttrDefault *adb = (const AttrDefault *) b;
|
||||
|
||||
return ada->adnum - adb->adnum;
|
||||
return pg_cmp_s16(ada->adnum, adb->adnum);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
5
src/backend/utils/cache/syscache.c
vendored
5
src/backend/utils/cache/syscache.c
vendored
@@ -29,6 +29,7 @@
|
||||
#include "catalog/pg_shdepend_d.h"
|
||||
#include "catalog/pg_shdescription_d.h"
|
||||
#include "catalog/pg_shseclabel_d.h"
|
||||
#include "common/int.h"
|
||||
#include "lib/qunique.h"
|
||||
#include "utils/catcache.h"
|
||||
#include "utils/lsyscache.h"
|
||||
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
|
||||
Oid oa = *((const Oid *) a);
|
||||
Oid ob = *((const Oid *) b);
|
||||
|
||||
if (oa == ob)
|
||||
return 0;
|
||||
return (oa > ob) ? 1 : -1;
|
||||
return pg_cmp_u32(oa, ob);
|
||||
}
|
||||
|
||||
8
src/backend/utils/cache/typcache.c
vendored
8
src/backend/utils/cache/typcache.c
vendored
@@ -57,6 +57,7 @@
|
||||
#include "catalog/pg_range.h"
|
||||
#include "catalog/pg_type.h"
|
||||
#include "commands/defrem.h"
|
||||
#include "common/int.h"
|
||||
#include "executor/executor.h"
|
||||
#include "lib/dshash.h"
|
||||
#include "optimizer/optimizer.h"
|
||||
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
|
||||
const EnumItem *l = (const EnumItem *) left;
|
||||
const EnumItem *r = (const EnumItem *) right;
|
||||
|
||||
if (l->enum_oid < r->enum_oid)
|
||||
return -1;
|
||||
else if (l->enum_oid > r->enum_oid)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
return pg_cmp_u32(l->enum_oid, r->enum_oid);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -46,6 +46,7 @@
|
||||
#include "postgres.h"
|
||||
|
||||
#include "common/hashfn.h"
|
||||
#include "common/int.h"
|
||||
#include "storage/ipc.h"
|
||||
#include "storage/predicate.h"
|
||||
#include "storage/proc.h"
|
||||
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
|
||||
|
||||
/* Note: reverse order */
|
||||
if (ra->kind->release_phase == rb->kind->release_phase)
|
||||
{
|
||||
if (ra->kind->release_priority == rb->kind->release_priority)
|
||||
return 0;
|
||||
else if (ra->kind->release_priority > rb->kind->release_priority)
|
||||
return -1;
|
||||
else
|
||||
return 1;
|
||||
}
|
||||
return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
|
||||
else if (ra->kind->release_phase > rb->kind->release_phase)
|
||||
return -1;
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user