[tarantool-patches] [PATCH] sql: remove redundant goto from VDBE prologue

Nikita Pettik korablev at tarantool.org
Tue Jun 12 21:26:07 MSK 2018


Structure of VDBE prologue:

    0: OP_Init 0 N (address to start) 0 --|
|-> 1: ...                                |
|      ...                                |
|   N: OP_Transaction         <------------
|   N+1: (Constant expressions to be saved in registers)
|      ...
|-- M: OP_Goto 0 1 0

However, last opcode in VDBE program (i.e. OP_Goto) is generated always,
despite the existence of OP_Transaction or constant expressions.
Thus, VDBE program for queries like <SELECT * FROM table;> features
redundant jump. Such useless jump wastes exectuion time (although it is
executed once) and may affect jump prediction.

This patch adds conditional branch for generating jump opcode finishing
VDBE program: it is appended only if we need to start transaction or
code constrant expressions.

Closes #3231
---
Branch: https://github.com/tarantool/tarantool/commits/np/gh-3231-remove-goto-from-vdbe-prologue
Issue: https://github.com/tarantool/tarantool/issues/3231

 src/box/sql/build.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/src/box/sql/build.c b/src/box/sql/build.c
index 62d687b17..66bef404c 100644
--- a/src/box/sql/build.c
+++ b/src/box/sql/build.c
@@ -94,9 +94,6 @@ sqlite3FinishCoding(Parse * pParse)
 			sqlite3VdbeJumpHere(v, 0);
 			if (pParse->initiateTTrans)
 				sqlite3VdbeAddOp0(v, OP_TTransaction);
-			if (db->init.busy == 0)
-				sqlite3VdbeChangeP5(v, 1);
-
 			/* Code constant expressions that where factored out of inner loops */
 			if (pParse->pConstExpr) {
 				ExprList *pEL = pParse->pConstExpr;
@@ -107,10 +104,22 @@ sqlite3FinishCoding(Parse * pParse)
 							iConstExprReg);
 				}
 			}
-
-			/* Finally, jump back to the beginning of the executable code. */
-			sqlite3VdbeGoto(v, 1);
 		}
+		/*
+		 * Finally, jump back to the beginning of
+		 * the executable code. In fact, it is required
+		 * only if some additional opcodes are generated.
+		 * Otherwise, it would be useless jump:
+		 *
+		 * 0:        OP_Init 0 vdbe_end ...
+		 * 1: ...
+		 *    ...
+		 * vdbe_end: OP_Goto 0 1 ...
+		 */
+		if (pParse->pConstExpr != NULL || pParse->initiateTTrans)
+			sqlite3VdbeGoto(v, 1);
+		else
+			sqlite3VdbeChangeP2(v, 0, 1);
 	}
 
 	/* Get the VDBE program ready for execution
-- 
2.15.1





More information about the Tarantool-patches mailing list