[patches] [sql 1/1] sql: remove extra key copy from cursor routine

korablev at tarantool.org korablev at tarantool.org
Tue Jan 23 18:09:49 MSK 2018


From: Nikita Pettik <korablev at tarantool.org>

SQL cursor (i.e. ta_cursor*) holds key which is used to create index iterator.
Iterator itself doesn't make copy of key, it only saves pointer to it.
tarantoolSqlite3MoveToUnpacked() allocates memory for key on region and pass
it to cursor_seek(), which in its turn copies it from region to malloc.
Other callers of cursor_seek() pass only static variables as key arguments.
All points considered, this memcpy() in cursor_create() turns out to be
redundant and can be removed. In this respect, moveToUnpacked() will allocate
memory for key using malloc().

Signed-off-by: Nikita Pettik <korablev at tarantool.org>
---
 src/box/sql.c | 21 ++++++++-------------
 1 file changed, 8 insertions(+), 13 deletions(-)

diff --git a/src/box/sql.c b/src/box/sql.c
index 3511690ef..8f1f0d6df 100644
--- a/src/box/sql.c
+++ b/src/box/sql.c
@@ -270,15 +270,17 @@ int tarantoolSqlite3MovetoUnpacked(BtCursor *pCur, UnpackedRecord *pIdxKey,
 {
 	int rc, res_success;
 	size_t ks;
-	const char *k, *ke;
+	const char *ke;
 	enum iterator_type iter_type;
+	struct ta_cursor *taCur = pCur->pTaCursor;
 
 	ks = sqlite3VdbeMsgpackRecordLen(pIdxKey->aMem,
 					 pIdxKey->nField);
-	k = region_reserve(&fiber()->gc, ks);
-	if (k == NULL) return SQLITE_NOMEM;
-	ke = k + sqlite3VdbeMsgpackRecordPut((u8 *)k, pIdxKey->aMem,
-					     pIdxKey->nField);
+	taCur = cursor_create(taCur, ks);
+	ke = taCur->key + sqlite3VdbeMsgpackRecordPut((u8 *)taCur->key,
+						      pIdxKey->aMem,
+						      pIdxKey->nField);
+	pCur->pTaCursor = taCur;
 
 	switch (pIdxKey->opcode) {
 	default:
@@ -316,7 +318,7 @@ int tarantoolSqlite3MovetoUnpacked(BtCursor *pCur, UnpackedRecord *pIdxKey,
 		res_success = 0;
 		break;
 	}
-	rc = cursor_seek(pCur, pRes, iter_type, k, ke);
+	rc = cursor_seek(pCur, pRes, iter_type, taCur->key, ke);
 	if (*pRes == 0) {
 		*pRes = res_success;
 		/*
@@ -1019,13 +1021,6 @@ cursor_seek(BtCursor *pCur, int *pRes, enum iterator_type type,
 	}
 	pCur->pTaCursor = c;
 
-	/* Copy key if necessary. */
-	if (key_size != 0) {
-		memcpy(c->key, k, ke-k);
-		ke = c->key + (ke-k);
-		k = c->key;
-	}
-
 	c->iter = box_index_iterator(space_id, index_id, type, k, ke);
 	if (c->iter == NULL) {
 		pCur->eState = CURSOR_INVALID;
-- 
2.15.1




More information about the Tarantool-patches mailing list