diff options
author | Andres Freund <andres@anarazel.de> | 2019-01-26 14:17:52 -0800 |
---|---|---|
committer | Andres Freund <andres@anarazel.de> | 2019-01-26 14:17:52 -0800 |
commit | a9c35cf85ca1ff72f16f0f10d7ddee6e582b62b8 (patch) | |
tree | 4f34f2c902977a7ce0be7b1e32a12adf0cb223b7 /src/backend/utils/adt/arrayfuncs.c | |
parent | 6d3ede5f1c654f923b2767b0b0c3b09569adaa18 (diff) | |
download | postgresql-a9c35cf85ca1ff72f16f0f10d7ddee6e582b62b8.tar.gz postgresql-a9c35cf85ca1ff72f16f0f10d7ddee6e582b62b8.zip |
Change function call information to be variable length.
Before this change FunctionCallInfoData, the struct arguments etc for
V1 function calls are stored in, always had space for
FUNC_MAX_ARGS/100 arguments, storing datums and their nullness in two
arrays. For nearly every function call 100 arguments is far more than
needed, therefore wasting memory. Arg and argnull being two separate
arrays also guarantees that to access a single argument, two
cachelines have to be touched.
Change the layout so there's a single variable-length array with pairs
of value / isnull. That drastically reduces memory consumption for
most function calls (on x86-64 a two argument function now uses
64bytes, previously 936 bytes), and makes it very likely that argument
value and its nullness are on the same cacheline.
Arguments are stored in a new NullableDatum struct, which, due to
padding, needs more memory per argument than before. But as usually
far fewer arguments are stored, and individual arguments are cheaper
to access, that's still a clear win. It's likely that there's other
places where conversion to NullableDatum arrays would make sense,
e.g. TupleTableSlots, but that's for another commit.
Because the function call information is now variable-length
allocations have to take the number of arguments into account. For
heap allocations that can be done with SizeForFunctionCallInfoData(),
for on-stack allocations there's a new LOCAL_FCINFO(name, nargs) macro
that helps to allocate an appropriately sized and aligned variable.
Some places with stack allocation function call information don't know
the number of arguments at compile time, and currently variably sized
stack allocations aren't allowed in postgres. Therefore allow for
FUNC_MAX_ARGS space in these cases. They're not that common, so for
now that seems acceptable.
Because of the need to allocate FunctionCallInfo of the appropriate
size, older extensions may need to update their code. To avoid subtle
breakages, the FunctionCallInfoData struct has been renamed to
FunctionCallInfoBaseData. Most code only references FunctionCallInfo,
so that shouldn't cause much collateral damage.
This change is also a prerequisite for more efficient expression JIT
compilation (by allocating the function call information on the stack,
allowing LLVM to optimize it away); previously the size of the call
information caused problems inside LLVM's optimizer.
Author: Andres Freund
Reviewed-By: Tom Lane
Discussion: https://postgr.es/m/20180605172952.x34m5uz6ju6enaem@alap3.anarazel.de
Diffstat (limited to 'src/backend/utils/adt/arrayfuncs.c')
-rw-r--r-- | src/backend/utils/adt/arrayfuncs.c | 122 |
1 files changed, 60 insertions, 62 deletions
diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c index e457d81f23c..a785361fd07 100644 --- a/src/backend/utils/adt/arrayfuncs.c +++ b/src/backend/utils/adt/arrayfuncs.c @@ -3571,6 +3571,7 @@ array_contains_nulls(ArrayType *array) Datum array_eq(PG_FUNCTION_ARGS) { + LOCAL_FCINFO(locfcinfo, 2); AnyArrayType *array1 = PG_GETARG_ANY_ARRAY_P(0); AnyArrayType *array2 = PG_GETARG_ANY_ARRAY_P(1); Oid collation = PG_GET_COLLATION(); @@ -3590,7 +3591,6 @@ array_eq(PG_FUNCTION_ARGS) array_iter it1; array_iter it2; int i; - FunctionCallInfoData locfcinfo; if (element_type != AARR_ELEMTYPE(array2)) ereport(ERROR, @@ -3630,7 +3630,7 @@ array_eq(PG_FUNCTION_ARGS) /* * apply the operator to each pair of array elements. */ - InitFunctionCallInfoData(locfcinfo, &typentry->eq_opr_finfo, 2, + InitFunctionCallInfoData(*locfcinfo, &typentry->eq_opr_finfo, 2, collation, NULL, NULL); /* Loop over source data */ @@ -3666,12 +3666,12 @@ array_eq(PG_FUNCTION_ARGS) /* * Apply the operator to the element pair */ - locfcinfo.arg[0] = elt1; - locfcinfo.arg[1] = elt2; - locfcinfo.argnull[0] = false; - locfcinfo.argnull[1] = false; - locfcinfo.isnull = false; - oprresult = DatumGetBool(FunctionCallInvoke(&locfcinfo)); + locfcinfo->args[0].value = elt1; + locfcinfo->args[0].isnull = false; + locfcinfo->args[1].value = elt2; + locfcinfo->args[1].isnull = false; + locfcinfo->isnull = false; + oprresult = DatumGetBool(FunctionCallInvoke(locfcinfo)); if (!oprresult) { result = false; @@ -3742,6 +3742,7 @@ btarraycmp(PG_FUNCTION_ARGS) static int array_cmp(FunctionCallInfo fcinfo) { + LOCAL_FCINFO(locfcinfo, 2); AnyArrayType *array1 = PG_GETARG_ANY_ARRAY_P(0); AnyArrayType *array2 = PG_GETARG_ANY_ARRAY_P(1); Oid collation = PG_GET_COLLATION(); @@ -3761,7 +3762,6 @@ array_cmp(FunctionCallInfo fcinfo) array_iter it1; array_iter it2; int i; - FunctionCallInfoData locfcinfo; if (element_type != AARR_ELEMTYPE(array2)) ereport(ERROR, @@ -3794,7 +3794,7 @@ array_cmp(FunctionCallInfo fcinfo) /* * apply the operator to each pair of array elements. */ - InitFunctionCallInfoData(locfcinfo, &typentry->cmp_proc_finfo, 2, + InitFunctionCallInfoData(*locfcinfo, &typentry->cmp_proc_finfo, 2, collation, NULL, NULL); /* Loop over source data */ @@ -3833,12 +3833,12 @@ array_cmp(FunctionCallInfo fcinfo) } /* Compare the pair of elements */ - locfcinfo.arg[0] = elt1; - locfcinfo.arg[1] = elt2; - locfcinfo.argnull[0] = false; - locfcinfo.argnull[1] = false; - locfcinfo.isnull = false; - cmpresult = DatumGetInt32(FunctionCallInvoke(&locfcinfo)); + locfcinfo->args[0].value = elt1; + locfcinfo->args[0].isnull = false; + locfcinfo->args[1].value = elt2; + locfcinfo->args[1].isnull = false; + locfcinfo->isnull = false; + cmpresult = DatumGetInt32(FunctionCallInvoke(locfcinfo)); if (cmpresult == 0) continue; /* equal */ @@ -3913,6 +3913,7 @@ array_cmp(FunctionCallInfo fcinfo) Datum hash_array(PG_FUNCTION_ARGS) { + LOCAL_FCINFO(locfcinfo, 1); AnyArrayType *array = PG_GETARG_ANY_ARRAY_P(0); int ndims = AARR_NDIM(array); int *dims = AARR_DIMS(array); @@ -3925,7 +3926,6 @@ hash_array(PG_FUNCTION_ARGS) char typalign; int i; array_iter iter; - FunctionCallInfoData locfcinfo; /* * We arrange to look up the hash function only once per series of calls, @@ -3953,7 +3953,7 @@ hash_array(PG_FUNCTION_ARGS) /* * apply the hash function to each array element. */ - InitFunctionCallInfoData(locfcinfo, &typentry->hash_proc_finfo, 1, + InitFunctionCallInfoData(*locfcinfo, &typentry->hash_proc_finfo, 1, InvalidOid, NULL, NULL); /* Loop over source data */ @@ -3977,10 +3977,10 @@ hash_array(PG_FUNCTION_ARGS) else { /* Apply the hash function */ - locfcinfo.arg[0] = elt; - locfcinfo.argnull[0] = false; - locfcinfo.isnull = false; - elthash = DatumGetUInt32(FunctionCallInvoke(&locfcinfo)); + locfcinfo->args[0].value = elt; + locfcinfo->args[0].isnull = false; + locfcinfo->isnull = false; + elthash = DatumGetUInt32(FunctionCallInvoke(locfcinfo)); } /* @@ -4010,6 +4010,7 @@ hash_array(PG_FUNCTION_ARGS) Datum hash_array_extended(PG_FUNCTION_ARGS) { + LOCAL_FCINFO(locfcinfo, 2); AnyArrayType *array = PG_GETARG_ANY_ARRAY_P(0); uint64 seed = PG_GETARG_INT64(1); int ndims = AARR_NDIM(array); @@ -4023,7 +4024,6 @@ hash_array_extended(PG_FUNCTION_ARGS) char typalign; int i; array_iter iter; - FunctionCallInfoData locfcinfo; typentry = (TypeCacheEntry *) fcinfo->flinfo->fn_extra; if (typentry == NULL || @@ -4042,7 +4042,7 @@ hash_array_extended(PG_FUNCTION_ARGS) typbyval = typentry->typbyval; typalign = typentry->typalign; - InitFunctionCallInfoData(locfcinfo, &typentry->hash_extended_proc_finfo, 2, + InitFunctionCallInfoData(*locfcinfo, &typentry->hash_extended_proc_finfo, 2, InvalidOid, NULL, NULL); /* Loop over source data */ @@ -4065,12 +4065,11 @@ hash_array_extended(PG_FUNCTION_ARGS) else { /* Apply the hash function */ - locfcinfo.arg[0] = elt; - locfcinfo.arg[1] = Int64GetDatum(seed); - locfcinfo.argnull[0] = false; - locfcinfo.argnull[1] = false; - locfcinfo.isnull = false; - elthash = DatumGetUInt64(FunctionCallInvoke(&locfcinfo)); + locfcinfo->args[0].value = elt; + locfcinfo->args[0].isnull = false; + locfcinfo->args[1].value = Int64GetDatum(seed); + locfcinfo->args[1].isnull = false; + elthash = DatumGetUInt64(FunctionCallInvoke(locfcinfo)); } result = (result << 5) - result + elthash; @@ -4100,6 +4099,7 @@ static bool array_contain_compare(AnyArrayType *array1, AnyArrayType *array2, Oid collation, bool matchall, void **fn_extra) { + LOCAL_FCINFO(locfcinfo, 2); bool result = matchall; Oid element_type = AARR_ELEMTYPE(array1); TypeCacheEntry *typentry; @@ -4113,7 +4113,6 @@ array_contain_compare(AnyArrayType *array1, AnyArrayType *array2, Oid collation, int i; int j; array_iter it1; - FunctionCallInfoData locfcinfo; if (element_type != AARR_ELEMTYPE(array2)) ereport(ERROR, @@ -4164,7 +4163,7 @@ array_contain_compare(AnyArrayType *array1, AnyArrayType *array2, Oid collation, /* * Apply the comparison operator to each pair of array elements. */ - InitFunctionCallInfoData(locfcinfo, &typentry->eq_opr_finfo, 2, + InitFunctionCallInfoData(*locfcinfo, &typentry->eq_opr_finfo, 2, collation, NULL, NULL); /* Loop over source data */ @@ -4206,12 +4205,12 @@ array_contain_compare(AnyArrayType *array1, AnyArrayType *array2, Oid collation, /* * Apply the operator to the element pair */ - locfcinfo.arg[0] = elt1; - locfcinfo.arg[1] = elt2; - locfcinfo.argnull[0] = false; - locfcinfo.argnull[1] = false; - locfcinfo.isnull = false; - oprresult = DatumGetBool(FunctionCallInvoke(&locfcinfo)); + locfcinfo->args[0].value = elt1; + locfcinfo->args[0].isnull = false; + locfcinfo->args[1].value = elt2; + locfcinfo->args[1].isnull = false; + locfcinfo->isnull = false; + oprresult = DatumGetBool(FunctionCallInvoke(locfcinfo)); if (oprresult) break; } @@ -6042,6 +6041,7 @@ array_replace_internal(ArrayType *array, bool remove, Oid collation, FunctionCallInfo fcinfo) { + LOCAL_FCINFO(locfcinfo, 2); ArrayType *result; Oid element_type; Datum *values; @@ -6062,7 +6062,6 @@ array_replace_internal(ArrayType *array, int bitmask; bool changed = false; TypeCacheEntry *typentry; - FunctionCallInfoData locfcinfo; element_type = ARR_ELEMTYPE(array); ndim = ARR_NDIM(array); @@ -6117,7 +6116,7 @@ array_replace_internal(ArrayType *array, } /* Prepare to apply the comparison operator */ - InitFunctionCallInfoData(locfcinfo, &typentry->eq_opr_finfo, 2, + InitFunctionCallInfoData(*locfcinfo, &typentry->eq_opr_finfo, 2, collation, NULL, NULL); /* Allocate temporary arrays for new values */ @@ -6175,12 +6174,12 @@ array_replace_internal(ArrayType *array, /* * Apply the operator to the element pair */ - locfcinfo.arg[0] = elt; - locfcinfo.arg[1] = search; - locfcinfo.argnull[0] = false; - locfcinfo.argnull[1] = false; - locfcinfo.isnull = false; - oprresult = DatumGetBool(FunctionCallInvoke(&locfcinfo)); + locfcinfo->args[0].value = elt; + locfcinfo->args[0].isnull = false; + locfcinfo->args[1].value = search; + locfcinfo->args[1].isnull = false; + locfcinfo->isnull = false; + oprresult = DatumGetBool(FunctionCallInvoke(locfcinfo)); if (!oprresult) { /* no match, keep element */ @@ -6457,10 +6456,10 @@ width_bucket_array_fixed(Datum operand, Oid collation, TypeCacheEntry *typentry) { + LOCAL_FCINFO(locfcinfo, 2); char *thresholds_data; int typlen = typentry->typlen; bool typbyval = typentry->typbyval; - FunctionCallInfoData locfcinfo; int left; int right; @@ -6470,7 +6469,7 @@ width_bucket_array_fixed(Datum operand, */ thresholds_data = (char *) ARR_DATA_PTR(thresholds); - InitFunctionCallInfoData(locfcinfo, &typentry->cmp_proc_finfo, 2, + InitFunctionCallInfoData(*locfcinfo, &typentry->cmp_proc_finfo, 2, collation, NULL, NULL); /* Find the bucket */ @@ -6484,13 +6483,13 @@ width_bucket_array_fixed(Datum operand, ptr = thresholds_data + mid * typlen; - locfcinfo.arg[0] = operand; - locfcinfo.arg[1] = fetch_att(ptr, typbyval, typlen); - locfcinfo.argnull[0] = false; - locfcinfo.argnull[1] = false; - locfcinfo.isnull = false; + locfcinfo->args[0].value = operand; + locfcinfo->args[0].isnull = false; + locfcinfo->args[1].value = fetch_att(ptr, typbyval, typlen); + locfcinfo->args[1].isnull = false; + locfcinfo->isnull = false; - cmpresult = DatumGetInt32(FunctionCallInvoke(&locfcinfo)); + cmpresult = DatumGetInt32(FunctionCallInvoke(locfcinfo)); if (cmpresult < 0) right = mid; @@ -6510,17 +6509,17 @@ width_bucket_array_variable(Datum operand, Oid collation, TypeCacheEntry *typentry) { + LOCAL_FCINFO(locfcinfo, 2); char *thresholds_data; int typlen = typentry->typlen; bool typbyval = typentry->typbyval; char typalign = typentry->typalign; - FunctionCallInfoData locfcinfo; int left; int right; thresholds_data = (char *) ARR_DATA_PTR(thresholds); - InitFunctionCallInfoData(locfcinfo, &typentry->cmp_proc_finfo, 2, + InitFunctionCallInfoData(*locfcinfo, &typentry->cmp_proc_finfo, 2, collation, NULL, NULL); /* Find the bucket */ @@ -6541,13 +6540,12 @@ width_bucket_array_variable(Datum operand, ptr = (char *) att_align_nominal(ptr, typalign); } - locfcinfo.arg[0] = operand; - locfcinfo.arg[1] = fetch_att(ptr, typbyval, typlen); - locfcinfo.argnull[0] = false; - locfcinfo.argnull[1] = false; - locfcinfo.isnull = false; + locfcinfo->args[0].value = operand; + locfcinfo->args[0].isnull = false; + locfcinfo->args[1].value = fetch_att(ptr, typbyval, typlen); + locfcinfo->args[1].isnull = false; - cmpresult = DatumGetInt32(FunctionCallInvoke(&locfcinfo)); + cmpresult = DatumGetInt32(FunctionCallInvoke(locfcinfo)); if (cmpresult < 0) right = mid; |