Tarantool development patches archive
 help / color / mirror / Atom feed
* [tarantool-patches] [PATCH] sql: remove redundant goto from VDBE prologue
@ 2018-06-12 18:26 Nikita Pettik
  2018-06-17 20:08 ` [tarantool-patches] " Vladislav Shpilevoy
  0 siblings, 1 reply; 9+ messages in thread
From: Nikita Pettik @ 2018-06-12 18:26 UTC (permalink / raw)
  To: tarantool-patches; +Cc: v.shpilevoy, Nikita Pettik

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2018-06-19  9:08 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-12 18:26 [tarantool-patches] [PATCH] sql: remove redundant goto from VDBE prologue Nikita Pettik
2018-06-17 20:08 ` [tarantool-patches] " Vladislav Shpilevoy
2018-06-18 10:46   ` n.pettik
2018-06-18 11:06     ` Vladislav Shpilevoy
2018-06-18 17:45       ` n.pettik
2018-06-18 18:57         ` Vladislav Shpilevoy
2018-06-18 20:55           ` n.pettik
2018-06-18 21:03             ` Vladislav Shpilevoy
2018-06-19  8:21               ` Kirill Yukhin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox