From: drh <> Date: Mon, 30 Jun 2025 10:30:47 +0000 (+0000) Subject: Factor out the code that tries to avoid evaluating subquery operands if the X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e24f20a4f5a6d26cdaece58eff77619a4ee757b9;p=thirdparty%2Fsqlite.git Factor out the code that tries to avoid evaluating subquery operands if the other operand is NULL into a subroutine, so that it can be more easily reused by other parts of the code generator. FossilOrigin-Name: 3c6c71bcea16443b82234414137dfa1b59e2ee8fe5097906c738fc1228fec4e6 --- diff --git a/manifest b/manifest index fcdf5f0344..e4cf97a0e0 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Improve\sthe\sbytecode\sgenerated\sfor\scomparisons\sso\sthat\sif\sone\soperand\sis\na\ssubquery\sand\sthe\sother\soperand\sevaluates\sto\sNULL,\sthe\ssubquery\soperand\nis\snot\seven\scomputed.\s\sThis\sfixes\s5\sof\sthe\s12\sslow\squeries\sdescribed\nin\s[forum:/forumpost/52651713ac|forum\spost\s52651713ac]. -D 2025-06-28T17:59:15.236 +C Factor\sout\sthe\scode\sthat\stries\sto\savoid\sevaluating\ssubquery\soperands\sif\sthe\nother\soperand\sis\sNULL\sinto\sa\ssubroutine,\sso\sthat\sit\scan\sbe\smore\seasily\sreused\nby\sother\sparts\sof\sthe\scode\sgenerator. +D 2025-06-30T10:30:47.908 F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea @@ -736,7 +736,7 @@ F src/date.c 9db4d604e699a73e10b8e85a44db074a1f04c0591a77e2abfd77703f50dce1e9 F src/dbpage.c b3e218f8ed74fcbb7fa805df8ca669a3718d397617b3d8a8aac3307dc315c4d6 F src/dbstat.c 73362c0df0f40ad5523a6f5501224959d0976757b511299bf892313e79d14f5c F src/delete.c 03a77ba20e54f0f42ebd8eddf15411ed6bdb06a2c472ac4b6b336521bf7cea42 -F src/expr.c b46621069e36a2fbc966215691e4775134e2cc6c7a6e9e54b01cbb70b51d57a5 +F src/expr.c f007ab177a9900d10893a58bf6b756e89144df635bab61b25f37a17390f12778 F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007 F src/fkey.c 928ed2517e8732113d2b9821aa37af639688d752f4ea9ac6e0e393d713eeb76f F src/func.c de47a8295503aa130baae5e6d9868ecf4f7c4dbffa65d83ad1f70bdbac0ee2d6 @@ -2208,11 +2208,8 @@ F tool/version-info.c 3b36468a90faf1bbd59c65fd0eb66522d9f941eedd364fabccd7227350 F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7 F tool/warnings.sh 1ad0169b022b280bcaaf94a7fa231591be96b514230ab5c98fbf15cd7df842dd F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P 5508b56fd24016c13981ec280ecdd833007c9d8dd595edb295b984c2b487b5c8 -R c5ec83310afc6f17c7231575eaf1e606 -T *branch * optimize-null-values -T *sym-optimize-null-values * -T -sym-trunk * +P f147bc04776ac0056412f69dfc518016c0d5b4e9d964664e3d88f595fb29dbe0 +R 2a36b21b6d3bc1bf7db15e8f66e788a3 U drh -Z 4eeee81d77d56831be900c22e39d8eec +Z 4ecc3e0b30247a36c4e1fea0e6dc5b85 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 19505125a7..2be7100258 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -f147bc04776ac0056412f69dfc518016c0d5b4e9d964664e3d88f595fb29dbe0 +3c6c71bcea16443b82234414137dfa1b59e2ee8fe5097906c738fc1228fec4e6 diff --git a/src/expr.c b/src/expr.c index b4461cf5c6..de729ea62f 100644 --- a/src/expr.c +++ b/src/expr.c @@ -5830,6 +5830,67 @@ static void exprCodeBetween( testcase( xJump==0 ); } +/* +** Compute the two operands of a binary operator. +** +** If either operand contains a subquery, then the code strives to +** compute the operand containing the subquery second. If the other +** operand evalutes to NULL, then a jump is made. The address of the +** IsNull operand that does this jump is returned. The caller can use +** this to optimize the computation so as to avoid doing the potentially +** expensive subquery. +** +** If no optimization opportunities exist, return 0. +*/ +static int exprComputeOperands( + Parse *pParse, /* Parsing context */ + Expr *pExpr, /* The comparison expression */ + int *pR1, /* OUT: Register holding the left operand */ + int *pR2, /* OUT: Register holding the right operand */ + int *pFree1, /* OUT: Temp register to free if not zero */ + int *pFree2 /* OUT: Another temp register to free if not zero */ +){ + int addrIsNull; + int r1, r2; + Vdbe *v = pParse->pVdbe; + + assert( v!=0 ); + /* + ** If the left operand contains a (possibly expensive) subquery and the + ** right operand does not and the right operation might be NULL, + ** then compute the right operand first and do an IsNull jump if the + ** right operand evalutes to NULL. + */ + if( ExprHasProperty(pExpr->pLeft, EP_Subquery) + && !ExprHasProperty(pExpr->pRight, EP_Subquery) + && sqlite3ExprCanBeNull(pExpr->pRight) + ){ + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, pFree2); + addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, r2); VdbeCoverage(v); + }else{ + addrIsNull = 0; + } + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, pFree1); + if( addrIsNull==0 ){ + /* + ** If the right operand contains a subquery and the left operand does not + ** and the left operand might be NULL, then check the left operand do + ** an IsNull check on the left operand before computing the right + ** operand. + */ + if( ExprHasProperty(pExpr->pRight, EP_Subquery) + && sqlite3ExprCanBeNull(pExpr->pLeft) + ){ + addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, r1); VdbeCoverage(v); + } + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, pFree2); + } + *pR1 = r1; + *pR2 = r2; + return addrIsNull; +} + + /* ** Generate code for a boolean expression such that a jump is made ** to the label "dest" if the expression is true but execution @@ -6076,75 +6137,47 @@ void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ break; } case TK_IS: - case TK_ISNOT: + case TK_ISNOT: { testcase( pExpr->op==TK_IS ); testcase( pExpr->op==TK_ISNOT ); + if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr; op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ; - jumpIfNull = SQLITE_NULLEQ; - /* no break */ deliberate_fall_through + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, + r1, r2, dest, SQLITE_NULLEQ, + ExprHasProperty(pExpr,EP_Commuted)); + testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); + testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); + break; + } case TK_LT: case TK_LE: case TK_GT: case TK_GE: case TK_NE: case TK_EQ: { - int r2Done = 0; - int addrBypass = 0; + int addrIsNull; if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr; - testcase( jumpIfNull==0 ); - /* - ** If the left operand is a (possibly expensive) subquery and the - ** right operand is not and the right operation might be NULL and - ** the operator is not IS or IS NOT, then compute the right operand - ** first and skip the computation of the left operand if the right - ** operand is NULL. - */ - if( jumpIfNull!=SQLITE_NULLEQ - && ExprHasProperty(pExpr->pLeft, EP_Subquery) - && !ExprHasProperty(pExpr->pRight, EP_Subquery) - && sqlite3ExprCanBeNull(pExpr->pRight) - ){ - r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); - addrBypass = sqlite3VdbeAddOp2(v, OP_IsNull, r2, dest); - VdbeCoverageIf(v, jumpIfNull==SQLITE_JUMPIFNULL); - VdbeCoverageIf(v, jumpIfNull!=SQLITE_JUMPIFNULL); - if( jumpIfNull==SQLITE_JUMPIFNULL ) addrBypass = 0; - r2Done = 1; - } - r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); - if( !r2Done ){ - /* - ** If the right operand is a subquery and the left operand is not - ** and the left operand might be NULL and the comparison operator - ** is not IS or IS NOT, then check the left operand to see if it is - ** NULL and skip the computation of the right operand if it is. - */ - if( jumpIfNull!=SQLITE_NULLEQ - && ExprHasProperty(pExpr->pRight, EP_Subquery) - && sqlite3ExprCanBeNull(pExpr->pLeft) - ){ - addrBypass = sqlite3VdbeAddOp2(v, OP_IsNull, r1, dest); - VdbeCoverageIf(v, jumpIfNull==SQLITE_JUMPIFNULL); - VdbeCoverageIf(v, jumpIfNull!=SQLITE_JUMPIFNULL); - if( jumpIfNull==SQLITE_JUMPIFNULL ) addrBypass = 0; - } - r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); - } + addrIsNull = exprComputeOperands(pParse, pExpr, + &r1, &r2, ®Free1, ®Free2); codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, r1, r2, dest, jumpIfNull,ExprHasProperty(pExpr,EP_Commuted)); assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); - assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); - VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); - VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); - assert(TK_NE==OP_Ne); testcase(op==OP_Ne); - VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); - VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); + assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); + assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); testcase( regFree1==0 ); testcase( regFree2==0 ); - if( addrBypass ) sqlite3VdbeJumpHere(v, addrBypass); + if( addrIsNull ){ + if( jumpIfNull ){ + sqlite3VdbeChangeP2(v, addrIsNull, dest); + }else{ + sqlite3VdbeJumpHere(v, addrIsNull); + } + } break; } case TK_ISNULL: