v3-0002-Add-special-case-fast-paths-for-strict-functions.patch
application/octet-stream
Filename: v3-0002-Add-special-case-fast-paths-for-strict-functions.patch
Type: application/octet-stream
Part: 1
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: format-patch
Series: patch v3-0002
Subject: Add special case fast-paths for strict functions
| File | + | − |
|---|---|---|
| src/backend/executor/execExpr.c | 12 | 1 |
| src/backend/executor/execExprInterp.c | 73 | 1 |
| src/backend/jit/llvm/llvmjit_expr.c | 6 | 1 |
| src/include/executor/execExpr.h | 5 | 2 |
From 64c250efc9522dfdfe1792f2e50b1369daa3387c Mon Sep 17 00:00:00 2001
From: Daniel Gustafsson <dgustafsson@postgresql.org>
Date: Thu, 4 Jul 2024 17:58:23 +0200
Subject: [PATCH v3 2/2] Add special case fast-paths for strict functions
Many STRICT function calls will have one or two arguments, in which
case we can speed up checking for NULL input by avoiding setting up
a loop over the arguments. This adds EEOP_FUNCEXPR_STRICT_1 and the
corresponding EEOP_FUNCEXPR_STRICT_2 for functions with one and two
arguments respectively.
Author: Andres Freund, Daniel Gustafsson
Reviewed-by: Andreas Karlsson <andreas@proxel.se>
Discussion: https://postgr.es/m/415721CE-7D2E-4B74-B5D9-1950083BA03E@yesql.se
Discussion: https://postgr.es/m/20191023163849.sosqbfs5yenocez3@alap3.anarazel.de
---
src/backend/executor/execExpr.c | 13 ++++-
src/backend/executor/execExprInterp.c | 74 ++++++++++++++++++++++++++-
src/backend/jit/llvm/llvmjit_expr.c | 7 ++-
src/include/executor/execExpr.h | 7 ++-
4 files changed, 96 insertions(+), 5 deletions(-)
diff --git a/src/backend/executor/execExpr.c b/src/backend/executor/execExpr.c
index 5351f6a81f..39225056f0 100644
--- a/src/backend/executor/execExpr.c
+++ b/src/backend/executor/execExpr.c
@@ -2692,7 +2692,15 @@ ExecInitFunc(ExprEvalStep *scratch, Expr *node, List *args, Oid funcid,
if (pgstat_track_functions <= flinfo->fn_stats)
{
if (flinfo->fn_strict && nargs > 0)
- scratch->opcode = EEOP_FUNCEXPR_STRICT;
+ {
+ /* Choose nargs optimized implementation if available. */
+ if (nargs == 1)
+ scratch->opcode = EEOP_FUNCEXPR_STRICT_1;
+ else if (nargs == 2)
+ scratch->opcode = EEOP_FUNCEXPR_STRICT_2;
+ else
+ scratch->opcode = EEOP_FUNCEXPR_STRICT;
+ }
else
scratch->opcode = EEOP_FUNCEXPR;
}
@@ -3751,6 +3759,8 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
{
if (strictnulls)
scratch.opcode = EEOP_AGG_STRICT_INPUT_CHECK_NULLS;
+ else if (strictargs && pertrans->numTransInputs == 1)
+ scratch.opcode = EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1;
else
scratch.opcode = EEOP_AGG_STRICT_INPUT_CHECK_ARGS;
scratch.d.agg_strict_input_check.nulls = strictnulls;
@@ -3827,6 +3837,7 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
as->d.jump.jumpdone = state->steps_len;
}
else if (as->opcode == EEOP_AGG_STRICT_INPUT_CHECK_ARGS ||
+ as->opcode == EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1 ||
as->opcode == EEOP_AGG_STRICT_INPUT_CHECK_NULLS)
{
Assert(as->d.agg_strict_input_check.jumpnull == -1);
diff --git a/src/backend/executor/execExprInterp.c b/src/backend/executor/execExprInterp.c
index 5cabf41341..15e3aa1175 100644
--- a/src/backend/executor/execExprInterp.c
+++ b/src/backend/executor/execExprInterp.c
@@ -316,7 +316,9 @@ ExecReadyInterpretedExpr(ExprState *state)
return;
}
else if (step0 == EEOP_CASE_TESTVAL &&
- step1 == EEOP_FUNCEXPR_STRICT &&
+ (step1 == EEOP_FUNCEXPR_STRICT ||
+ step1 == EEOP_FUNCEXPR_STRICT_1 ||
+ step1 == EEOP_FUNCEXPR_STRICT_2) &&
state->steps[0].d.casetest.value)
{
state->evalfunc_private = (void *) ExecJustApplyFuncToCase;
@@ -427,6 +429,8 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
&&CASE_EEOP_CONST,
&&CASE_EEOP_FUNCEXPR,
&&CASE_EEOP_FUNCEXPR_STRICT,
+ &&CASE_EEOP_FUNCEXPR_STRICT_1,
+ &&CASE_EEOP_FUNCEXPR_STRICT_2,
&&CASE_EEOP_FUNCEXPR_FUSAGE,
&&CASE_EEOP_FUNCEXPR_STRICT_FUSAGE,
&&CASE_EEOP_BOOL_AND_STEP_FIRST,
@@ -501,6 +505,7 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
&&CASE_EEOP_AGG_STRICT_DESERIALIZE,
&&CASE_EEOP_AGG_DESERIALIZE,
&&CASE_EEOP_AGG_STRICT_INPUT_CHECK_ARGS,
+ &&CASE_EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1,
&&CASE_EEOP_AGG_STRICT_INPUT_CHECK_NULLS,
&&CASE_EEOP_AGG_PLAIN_PERGROUP_NULLCHECK,
&&CASE_EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL,
@@ -759,6 +764,7 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
EEO_NEXT();
}
+ /* strict function call with more than two arguments */
EEO_CASE(EEOP_FUNCEXPR_STRICT)
{
FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
@@ -766,6 +772,8 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
int nargs = op->d.func.nargs;
Datum d;
+ Assert(nargs > 2);
+
/* strict function, so check for NULL args */
for (int argno = 0; argno < nargs; argno++)
{
@@ -784,6 +792,54 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
EEO_NEXT();
}
+ /* strict function call with one argument */
+ EEO_CASE(EEOP_FUNCEXPR_STRICT_1)
+ {
+ FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
+ NullableDatum *args = fcinfo->args;
+
+ Assert(op->d.func.nargs == 1);
+
+ /* strict function, so check for NULL args */
+ if (args[0].isnull)
+ *op->resnull = true;
+ else
+ {
+ Datum d;
+
+ fcinfo->isnull = false;
+ d = op->d.func.fn_addr(fcinfo);
+ *op->resvalue = d;
+ *op->resnull = fcinfo->isnull;
+ }
+
+ EEO_NEXT();
+ }
+
+ /* strict function call with two arguments */
+ EEO_CASE(EEOP_FUNCEXPR_STRICT_2)
+ {
+ FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
+ NullableDatum *args = fcinfo->args;
+
+ Assert(op->d.func.nargs == 2);
+
+ /* strict function, so check for NULL args */
+ if (args[0].isnull || args[1].isnull)
+ *op->resnull = true;
+ else
+ {
+ Datum d;
+
+ fcinfo->isnull = false;
+ d = op->d.func.fn_addr(fcinfo);
+ *op->resvalue = d;
+ *op->resnull = fcinfo->isnull;
+ }
+
+ EEO_NEXT();
+ }
+
EEO_CASE(EEOP_FUNCEXPR_FUSAGE)
{
/* not common enough to inline */
@@ -1797,11 +1853,14 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
* input is not NULL.
*/
+ /* when checking more than one argument */
EEO_CASE(EEOP_AGG_STRICT_INPUT_CHECK_ARGS)
{
NullableDatum *args = op->d.agg_strict_input_check.args;
int nargs = op->d.agg_strict_input_check.nargs;
+ Assert(nargs > 1);
+
for (int argno = 0; argno < nargs; argno++)
{
if (args[argno].isnull)
@@ -1810,6 +1869,19 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
EEO_NEXT();
}
+ /* special case for just one argument */
+ EEO_CASE(EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1)
+ {
+ NullableDatum *args = op->d.agg_strict_input_check.args;
+ PG_USED_FOR_ASSERTS_ONLY int nargs = op->d.agg_strict_input_check.nargs;
+
+ Assert(nargs == 1);
+
+ if (args[0].isnull)
+ EEO_JUMP(op->d.agg_strict_input_check.jumpnull);
+ EEO_NEXT();
+ }
+
EEO_CASE(EEOP_AGG_STRICT_INPUT_CHECK_NULLS)
{
bool *nulls = op->d.agg_strict_input_check.nulls;
diff --git a/src/backend/jit/llvm/llvmjit_expr.c b/src/backend/jit/llvm/llvmjit_expr.c
index 28df6ef469..7c88ef4302 100644
--- a/src/backend/jit/llvm/llvmjit_expr.c
+++ b/src/backend/jit/llvm/llvmjit_expr.c
@@ -590,12 +590,16 @@ llvm_compile_expr(ExprState *state)
case EEOP_FUNCEXPR:
case EEOP_FUNCEXPR_STRICT:
+ case EEOP_FUNCEXPR_STRICT_1:
+ case EEOP_FUNCEXPR_STRICT_2:
{
FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
LLVMValueRef v_fcinfo_isnull;
LLVMValueRef v_retval;
- if (opcode == EEOP_FUNCEXPR_STRICT)
+ if (opcode == EEOP_FUNCEXPR_STRICT ||
+ opcode == EEOP_FUNCEXPR_STRICT_1 ||
+ opcode == EEOP_FUNCEXPR_STRICT_2)
{
LLVMBasicBlockRef b_nonull;
LLVMBasicBlockRef *b_checkargnulls;
@@ -2374,6 +2378,7 @@ llvm_compile_expr(ExprState *state)
}
case EEOP_AGG_STRICT_INPUT_CHECK_ARGS:
+ case EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1:
case EEOP_AGG_STRICT_INPUT_CHECK_NULLS:
{
int nargs = op->d.agg_strict_input_check.nargs;
diff --git a/src/include/executor/execExpr.h b/src/include/executor/execExpr.h
index 2abd66b972..0d2a49f037 100644
--- a/src/include/executor/execExpr.h
+++ b/src/include/executor/execExpr.h
@@ -108,11 +108,13 @@ typedef enum ExprEvalOp
/*
* Evaluate function call (including OpExprs etc). For speed, we
- * distinguish in the opcode whether the function is strict and/or
- * requires usage stats tracking.
+ * distinguish in the opcode whether the function is strict with 1, 2, or
+ * more arguments and/or requires usage stats tracking.
*/
EEOP_FUNCEXPR,
EEOP_FUNCEXPR_STRICT,
+ EEOP_FUNCEXPR_STRICT_1,
+ EEOP_FUNCEXPR_STRICT_2,
EEOP_FUNCEXPR_FUSAGE,
EEOP_FUNCEXPR_STRICT_FUSAGE,
@@ -265,6 +267,7 @@ typedef enum ExprEvalOp
EEOP_AGG_STRICT_DESERIALIZE,
EEOP_AGG_DESERIALIZE,
EEOP_AGG_STRICT_INPUT_CHECK_ARGS,
+ EEOP_AGG_STRICT_INPUT_CHECK_ARGS_1,
EEOP_AGG_STRICT_INPUT_CHECK_NULLS,
EEOP_AGG_PLAIN_PERGROUP_NULLCHECK,
EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL,
--
2.39.3 (Apple Git-146)