[patches] [refactoring 1/1] sql: remove unnecessary SCopy opcodes

Bulat Niatshin niatshin at tarantool.org
Tue Feb 6 18:18:54 MSK 2018


Remove unnecessary OP_SCopy opcodes from listings being generated
for INSERT/UPDATE operations, add it to VDBE listing only in
case of presence of UNIQUE bytecode.

Signed-off-by: Bulat Niatshin <niatshin at tarantool.org>
---
 src/box/sql/insert.c | 42 +++++++++++++++++++++++++-----------------
 1 file changed, 25 insertions(+), 17 deletions(-)

diff --git a/src/box/sql/insert.c b/src/box/sql/insert.c
index bc1906fb6..25d6862e5 100644
--- a/src/box/sql/insert.c
+++ b/src/box/sql/insert.c
@@ -1214,6 +1214,10 @@ sqlite3GenerateConstraintChecks(Parse * pParse,		/* The parser context */
 			uniqueByteCodeNeeded = true;
 		}
 
+		if (!IsPrimaryKeyIndex(pIdx) && !uniqueByteCodeNeeded) {
+			continue;
+		}
+
 		if (aRegIdx[ix] == 0)
 			continue;	/* Skip indices that do not change */
 		if (bAffinityDone == 0) {
@@ -1248,14 +1252,22 @@ sqlite3GenerateConstraintChecks(Parse * pParse,		/* The parser context */
 				VdbeComment((v, "%s column %d", pIdx->zName,
 					     i));
 			} else {
-				if (iField == pTab->iPKey) {
-					x = regNewData;
-				} else {
-					x = iField + regNewData + 1;
+				/* OP_SCopy copies value in separate register,
+				 * which lately will be used by OP_NoConflict.
+				 * But OP_NoConflict is necessary only in cases
+				 * when bytecode is needed for proper UNIQUE
+				 * constraint handling.
+				 */
+				if (uniqueByteCodeNeeded) {
+					if (iField == pTab->iPKey) {
+						x = regNewData;
+					} else {
+						x = iField + regNewData + 1;
+					}
+					assert(iField >= 0);
+					sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx + i);
+					VdbeComment((v, "%s", pTab->aCol[iField].zName));
 				}
-				assert(iField >= 0);
-				sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx + i);
-				VdbeComment((v, "%s", pTab->aCol[iField].zName));
 			}
 		}
 
@@ -1280,18 +1292,14 @@ sqlite3GenerateConstraintChecks(Parse * pParse,		/* The parser context */
 					sqlite3VdbeResolveLabel(v, skip_if_null);
 				}
 			}
-			if (IsPrimaryKeyIndex(pIdx) || uniqueByteCodeNeeded) {
-				sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData + 1,
-						  pTab->nCol, aRegIdx[ix]);
-				VdbeComment((v, "for %s", pIdx->zName));
-			}
+			sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData + 1,
+					pTab->nCol, aRegIdx[ix]);
+			VdbeComment((v, "for %s", pIdx->zName));
 		} else {
 			/* kyukhin: for Tarantool, this should be evaluated to NOP.  */
-			if (IsPrimaryKeyIndex(pIdx) || uniqueByteCodeNeeded) {
-				sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx,
-						  pIdx->nColumn, aRegIdx[ix]);
-				VdbeComment((v, "for %s", pIdx->zName));
-			}
+			sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx,
+					pIdx->nColumn, aRegIdx[ix]);
+			VdbeComment((v, "for %s", pIdx->zName));
 		}
 
 		/* In an UPDATE operation, if this index is the PRIMARY KEY
-- 
2.14.1




More information about the Tarantool-patches mailing list