From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id C6ABD2547E for ; Mon, 18 Jun 2018 06:46:10 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id zRhRhJU35sNU for ; Mon, 18 Jun 2018 06:46:10 -0400 (EDT) Received: from smtp59.i.mail.ru (smtp59.i.mail.ru [217.69.128.39]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 42B0324FD4 for ; Mon, 18 Jun 2018 06:46:10 -0400 (EDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Subject: [tarantool-patches] Re: [PATCH] sql: remove redundant goto from VDBE prologue From: "n.pettik" In-Reply-To: <24a1a33e-02cc-ccf4-7ff9-a155f5e18781@tarantool.org> Date: Mon, 18 Jun 2018 13:46:04 +0300 Content-Transfer-Encoding: quoted-printable Message-Id: <28322F56-E52D-42C7-AE52-188D6F50AF63@tarantool.org> References: <20180612182607.53450-1-korablev@tarantool.org> <24a1a33e-02cc-ccf4-7ff9-a155f5e18781@tarantool.org> Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: tarantool-patches@freelists.org Cc: Vladislav Shpilevoy > 1. This function looks small. Lets refactor it to Tarantool > code style. If we don't, I am afraid this commit will be lost > in 'git blame', when this function finally will be refactored > completely. >=20 Ok. See diff at the end of letter. >> sqlite3VdbeJumpHere(v, 0); >> if (pParse->initiateTTrans) >> sqlite3VdbeAddOp0(v, OP_TTransaction); >> - if (db->init.busy =3D=3D 0) >> - sqlite3VdbeChangeP5(v, 1); >=20 > 2. Why is it removed? I do not see this code moved below. Actually, this code looks strange. If OP_TTransaction is added, then setting fifth param for it makes no sense (this opcode takes no args). If this change is related to OP_Halt, then it is also useless, since the last OP_Halt must be with signature 0 0 0 (comment from = vdbe.c): * There is an implied "Halt 0 0 0" instruction inserted at the very end = of * every program. So a jump past the last instruction of the program * is the same as executing Halt. That is why I removed this code. >=20 >> - >> /* Code constant expressions that where factored = out of inner loops */ >> if (pParse->pConstExpr) { >> ExprList *pEL =3D 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 !=3D NULL || = pParse->initiateTTrans) >> + sqlite3VdbeGoto(v, 1); >> + else >> + sqlite3VdbeChangeP2(v, 0, 1); >=20 > 3. As far as I see, P2 in OP_Init is 1 already when we are here. It is > not? See allocVdbe function. P2 =3D=3D 1 by default, and here it can = be changed to > goto to ttrans. In fact, it is changed by sqlite3VdbeJumpHere(v, 0);=20 Thus, we have to again set its value to 1, in case of omitting jump. >=20 >> } >> /* Get the VDBE program ready for execution >=20 > 4. Can we test the new VDBE plan using EXPLAIN? I am wondering why all > plan changes are not tested using EXPLAIN. Well, it it is quite complicated to test EXPLAIN command since any = change to query planner or code generator at all would result in rewriting such tests. = Hence, once we decided to avoid using tests with EXPLAIN until we get stable code = generation. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Subject: [PATCH] sql: remove redundant goto from VDBE prologue 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