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

Commit 30efc3b

Browse files
committed
Remove size increase in ExprEvalStep caused by hashed saops
50e17ad increased the size of ExprEvalStep from 64 bytes up to 88 bytes. Lots of effort was spent during the development of the current expression evaluation code to make an instance of this struct as small as possible. Making this struct larger than needed reduces CPU cache efficiency during expression evaluation which causes noticeable slowdowns during query execution. In order to reduce the size of the struct, here we remove the fn_addr field. The values from this field can be obtained via fcinfo, just with some extra pointer dereferencing. The extra indirection does not seem to cause any noticeable slowdowns. Various other fields have been moved into the ScalarArrayOpExprHashTable struct. These fields are only used when the ScalarArrayOpExprHashTable pointer has already been dereferenced, so no additional pointer dereferences occur for these. Here we also make hash_fcinfo_data the last field in ScalarArrayOpExprHashTable so that we can avoid a further pointer dereference to get the FunctionCallInfoBaseData. This also saves a call to palloc(). 50e17ad was added in 14, but it's too late to adjust the size of the ExprEvalStep in that version, so here we just backpatch to 15, which is currently in beta. Author: Andres Freund, David Rowley Discussion: https://postgr.es/m/20220616233130.rparivafipt6doj3@alap3.anarazel.de Backpatch-through: 15
1 parent c7e21e9 commit 30efc3b

File tree

3 files changed

+23
-29
lines changed

3 files changed

+23
-29
lines changed

src/backend/executor/execExpr.c

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,8 +1203,6 @@ ExecInitExprRec(Expr *node, ExprState *state,
12031203
FmgrInfo *finfo;
12041204
FunctionCallInfo fcinfo;
12051205
AclResult aclresult;
1206-
FmgrInfo *hash_finfo;
1207-
FunctionCallInfo hash_fcinfo;
12081206
Oid cmpfuncid;
12091207

12101208
/*
@@ -1262,18 +1260,6 @@ ExecInitExprRec(Expr *node, ExprState *state,
12621260
*/
12631261
if (OidIsValid(opexpr->hashfuncid))
12641262
{
1265-
hash_finfo = palloc0(sizeof(FmgrInfo));
1266-
hash_fcinfo = palloc0(SizeForFunctionCallInfo(1));
1267-
fmgr_info(opexpr->hashfuncid, hash_finfo);
1268-
fmgr_info_set_expr((Node *) node, hash_finfo);
1269-
InitFunctionCallInfoData(*hash_fcinfo, hash_finfo,
1270-
1, opexpr->inputcollid, NULL,
1271-
NULL);
1272-
1273-
scratch.d.hashedscalararrayop.hash_finfo = hash_finfo;
1274-
scratch.d.hashedscalararrayop.hash_fcinfo_data = hash_fcinfo;
1275-
scratch.d.hashedscalararrayop.hash_fn_addr = hash_finfo->fn_addr;
1276-
12771263
/* Evaluate scalar directly into left function argument */
12781264
ExecInitExprRec(scalararg, state,
12791265
&fcinfo->args[0].value, &fcinfo->args[0].isnull);
@@ -1292,11 +1278,8 @@ ExecInitExprRec(Expr *node, ExprState *state,
12921278
scratch.d.hashedscalararrayop.inclause = opexpr->useOr;
12931279
scratch.d.hashedscalararrayop.finfo = finfo;
12941280
scratch.d.hashedscalararrayop.fcinfo_data = fcinfo;
1295-
scratch.d.hashedscalararrayop.fn_addr = finfo->fn_addr;
1281+
scratch.d.hashedscalararrayop.saop = opexpr;
12961282

1297-
scratch.d.hashedscalararrayop.hash_finfo = hash_finfo;
1298-
scratch.d.hashedscalararrayop.hash_fcinfo_data = hash_fcinfo;
1299-
scratch.d.hashedscalararrayop.hash_fn_addr = hash_finfo->fn_addr;
13001283

13011284
ExprEvalPushStep(state, &scratch);
13021285
}

src/backend/executor/execExprInterp.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ typedef struct ScalarArrayOpExprHashTable
217217
{
218218
saophash_hash *hashtab; /* underlying hash table */
219219
struct ExprEvalStep *op;
220+
FmgrInfo hash_finfo; /* function's lookup data */
221+
FunctionCallInfoBaseData hash_fcinfo_data; /* arguments etc */
220222
} ScalarArrayOpExprHashTable;
221223

222224
/* Define parameters for ScalarArrayOpExpr hash table code generation. */
@@ -3474,13 +3476,13 @@ static uint32
34743476
saop_element_hash(struct saophash_hash *tb, Datum key)
34753477
{
34763478
ScalarArrayOpExprHashTable *elements_tab = (ScalarArrayOpExprHashTable *) tb->private_data;
3477-
FunctionCallInfo fcinfo = elements_tab->op->d.hashedscalararrayop.hash_fcinfo_data;
3479+
FunctionCallInfo fcinfo = &elements_tab->hash_fcinfo_data;
34783480
Datum hash;
34793481

34803482
fcinfo->args[0].value = key;
34813483
fcinfo->args[0].isnull = false;
34823484

3483-
hash = elements_tab->op->d.hashedscalararrayop.hash_fn_addr(fcinfo);
3485+
hash = elements_tab->hash_finfo.fn_addr(fcinfo);
34843486

34853487
return DatumGetUInt32(hash);
34863488
}
@@ -3502,7 +3504,7 @@ saop_hash_element_match(struct saophash_hash *tb, Datum key1, Datum key2)
35023504
fcinfo->args[1].value = key2;
35033505
fcinfo->args[1].isnull = false;
35043506

3505-
result = elements_tab->op->d.hashedscalararrayop.fn_addr(fcinfo);
3507+
result = elements_tab->op->d.hashedscalararrayop.finfo->fn_addr(fcinfo);
35063508

35073509
return DatumGetBool(result);
35083510
}
@@ -3549,6 +3551,7 @@ ExecEvalHashedScalarArrayOp(ExprState *state, ExprEvalStep *op, ExprContext *eco
35493551
/* Build the hash table on first evaluation */
35503552
if (elements_tab == NULL)
35513553
{
3554+
ScalarArrayOpExpr *saop;
35523555
int16 typlen;
35533556
bool typbyval;
35543557
char typalign;
@@ -3560,6 +3563,8 @@ ExecEvalHashedScalarArrayOp(ExprState *state, ExprEvalStep *op, ExprContext *eco
35603563
MemoryContext oldcontext;
35613564
ArrayType *arr;
35623565

3566+
saop = op->d.hashedscalararrayop.saop;
3567+
35633568
arr = DatumGetArrayTypeP(*op->resvalue);
35643569
nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
35653570

@@ -3571,10 +3576,21 @@ ExecEvalHashedScalarArrayOp(ExprState *state, ExprEvalStep *op, ExprContext *eco
35713576
oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
35723577

35733578
elements_tab = (ScalarArrayOpExprHashTable *)
3574-
palloc(sizeof(ScalarArrayOpExprHashTable));
3579+
palloc0(offsetof(ScalarArrayOpExprHashTable, hash_fcinfo_data) +
3580+
SizeForFunctionCallInfo(1));
35753581
op->d.hashedscalararrayop.elements_tab = elements_tab;
35763582
elements_tab->op = op;
35773583

3584+
fmgr_info(saop->hashfuncid, &elements_tab->hash_finfo);
3585+
fmgr_info_set_expr((Node *) saop, &elements_tab->hash_finfo);
3586+
3587+
InitFunctionCallInfoData(elements_tab->hash_fcinfo_data,
3588+
&elements_tab->hash_finfo,
3589+
1,
3590+
saop->inputcollid,
3591+
NULL,
3592+
NULL);
3593+
35783594
/*
35793595
* Create the hash table sizing it according to the number of elements
35803596
* in the array. This does assume that the array has no duplicates.
@@ -3669,7 +3685,7 @@ ExecEvalHashedScalarArrayOp(ExprState *state, ExprEvalStep *op, ExprContext *eco
36693685
fcinfo->args[1].value = (Datum) 0;
36703686
fcinfo->args[1].isnull = true;
36713687

3672-
result = op->d.hashedscalararrayop.fn_addr(fcinfo);
3688+
result = op->d.hashedscalararrayop.finfo->fn_addr(fcinfo);
36733689
resultnull = fcinfo->isnull;
36743690

36753691
/*

src/include/executor/execExpr.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -584,12 +584,7 @@ typedef struct ExprEvalStep
584584
struct ScalarArrayOpExprHashTable *elements_tab;
585585
FmgrInfo *finfo; /* function's lookup data */
586586
FunctionCallInfo fcinfo_data; /* arguments etc */
587-
/* faster to access without additional indirection: */
588-
PGFunction fn_addr; /* actual call address */
589-
FmgrInfo *hash_finfo; /* function's lookup data */
590-
FunctionCallInfo hash_fcinfo_data; /* arguments etc */
591-
/* faster to access without additional indirection: */
592-
PGFunction hash_fn_addr; /* actual call address */
587+
ScalarArrayOpExpr *saop;
593588
} hashedscalararrayop;
594589

595590
/* for EEOP_XMLEXPR */

0 commit comments

Comments
 (0)