Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 32cf7f7

Browse files
committed
Improve performance of float overflow checks in btree_gist
The current code could do unnecessary calls to isinf() (two for the argument values all the time while one could be sufficient in some cases). zero_is_valid was never used but the result value was still checked on 0 in the first position of the check. This is similar to 607f8ce. btree_gist has just copy-pasted the code doing those checks from the backend float4/8 code, as of the macro CHECKFLOATVAL(), to do the work. Author: Haiying Tang Discussion: https://postgr.es/m/OS0PR01MB611358E3A7BC3C2F874AC36BFBF39@OS0PR01MB6113.jpnprd01.prod.outlook.com
1 parent 2576dcf commit 32cf7f7

File tree

3 files changed

+8
-21
lines changed

3 files changed

+8
-21
lines changed

contrib/btree_gist/btree_float4.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "btree_gist.h"
77
#include "btree_utils_num.h"
8+
#include "utils/float.h"
89

910
typedef struct float4key
1011
{
@@ -98,7 +99,8 @@ float4_dist(PG_FUNCTION_ARGS)
9899
float4 r;
99100

100101
r = a - b;
101-
CHECKFLOATVAL(r, isinf(a) || isinf(b), true);
102+
if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
103+
float_overflow_error();
102104

103105
PG_RETURN_FLOAT4(Abs(r));
104106
}

contrib/btree_gist/btree_float8.c

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

66
#include "btree_gist.h"
77
#include "btree_utils_num.h"
8+
#include "utils/float.h"
89

910
typedef struct float8key
1011
{
@@ -76,8 +77,8 @@ gbt_float8_dist(const void *a, const void *b, FmgrInfo *flinfo)
7677
float8 r;
7778

7879
r = arg1 - arg2;
79-
CHECKFLOATVAL(r, isinf(arg1) || isinf(arg2), true);
80-
80+
if (unlikely(isinf(r)) && !isinf(arg1) && !isinf(arg2))
81+
float_overflow_error();
8182
return Abs(r);
8283
}
8384

@@ -106,7 +107,8 @@ float8_dist(PG_FUNCTION_ARGS)
106107
float8 r;
107108

108109
r = a - b;
109-
CHECKFLOATVAL(r, isinf(a) || isinf(b), true);
110+
if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
111+
float_overflow_error();
110112

111113
PG_RETURN_FLOAT8(Abs(r));
112114
}

contrib/btree_gist/btree_utils_num.h

-17
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,6 @@ typedef struct
8989

9090
#define GET_FLOAT_DISTANCE(t, arg1, arg2) Abs( ((float8) *((const t *) (arg1))) - ((float8) *((const t *) (arg2))) )
9191

92-
/*
93-
* check to see if a float4/8 val has underflowed or overflowed
94-
* borrowed from src/backend/utils/adt/float.c
95-
*/
96-
#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid) \
97-
do { \
98-
if (isinf(val) && !(inf_is_valid)) \
99-
ereport(ERROR, \
100-
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), \
101-
errmsg("value out of range: overflow"))); \
102-
\
103-
if ((val) == 0.0 && !(zero_is_valid)) \
104-
ereport(ERROR, \
105-
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), \
106-
errmsg("value out of range: underflow"))); \
107-
} while(0)
108-
10992

11093
extern Interval *abs_interval(Interval *a);
11194

0 commit comments

Comments
 (0)