Improve performance of float overflow checks in btree_gist
authorMichael Paquier <michael@paquier.xyz>
Thu, 19 Aug 2021 01:42:44 +0000 (10:42 +0900)
committerMichael Paquier <michael@paquier.xyz>
Thu, 19 Aug 2021 01:42:44 +0000 (10:42 +0900)
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

contrib/btree_gist/btree_float4.c
contrib/btree_gist/btree_float8.c
contrib/btree_gist/btree_utils_num.h

index 3604c73313a958c3031ca51f31d2c1c86700a8a7..b5d9175e6d5b64c9d9cc6fc440cf1b2516b968da 100644 (file)
@@ -5,6 +5,7 @@
 
 #include "btree_gist.h"
 #include "btree_utils_num.h"
+#include "utils/float.h"
 
 typedef struct float4key
 {
@@ -98,7 +99,8 @@ float4_dist(PG_FUNCTION_ARGS)
        float4          r;
 
        r = a - b;
-       CHECKFLOATVAL(r, isinf(a) || isinf(b), true);
+       if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
+               float_overflow_error();
 
        PG_RETURN_FLOAT4(Abs(r));
 }
index 10a5262aaa7ca98698987fd36b09dae9ce01f347..8fe1fedeef817ba868290aa6bfdee22c87a53ad1 100644 (file)
@@ -5,6 +5,7 @@
 
 #include "btree_gist.h"
 #include "btree_utils_num.h"
+#include "utils/float.h"
 
 typedef struct float8key
 {
@@ -76,8 +77,8 @@ gbt_float8_dist(const void *a, const void *b, FmgrInfo *flinfo)
        float8          r;
 
        r = arg1 - arg2;
-       CHECKFLOATVAL(r, isinf(arg1) || isinf(arg2), true);
-
+       if (unlikely(isinf(r)) && !isinf(arg1) && !isinf(arg2))
+               float_overflow_error();
        return Abs(r);
 }
 
@@ -106,7 +107,8 @@ float8_dist(PG_FUNCTION_ARGS)
        float8          r;
 
        r = a - b;
-       CHECKFLOATVAL(r, isinf(a) || isinf(b), true);
+       if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
+               float_overflow_error();
 
        PG_RETURN_FLOAT8(Abs(r));
 }
index cec69861726f90c9db68c4b30cb1e36d5e340f8e..415a7a6459a739c2b25469dc0429ac29b8a78f7d 100644 (file)
@@ -89,23 +89,6 @@ typedef struct
 
 #define GET_FLOAT_DISTANCE(t, arg1, arg2)      Abs( ((float8) *((const t *) (arg1))) - ((float8) *((const t *) (arg2))) )
 
-/*
- * check to see if a float4/8 val has underflowed or overflowed
- * borrowed from src/backend/utils/adt/float.c
- */
-#define CHECKFLOATVAL(val, inf_is_valid, zero_is_valid)                        \
-do {                                                                                                                   \
-       if (isinf(val) && !(inf_is_valid))                                                      \
-               ereport(ERROR,                                                                                  \
-                               (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),   \
-                 errmsg("value out of range: overflow")));                             \
-                                                                                                                               \
-       if ((val) == 0.0 && !(zero_is_valid))                                           \
-               ereport(ERROR,                                                                                  \
-                               (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),   \
-                errmsg("value out of range: underflow")));                             \
-} while(0)
-
 
 extern Interval *abs_interval(Interval *a);