]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Use integer handles for fts3 cursors for a small performance improvement. fts3-int-cursor
authordan <dan@noemail.net>
Mon, 17 Jul 2017 09:30:15 +0000 (09:30 +0000)
committerdan <dan@noemail.net>
Mon, 17 Jul 2017 09:30:15 +0000 (09:30 +0000)
FossilOrigin-Name: f4f7196b877001098278f2a3ee4c6839c5f130c638c930decb559d24ab7a20c1

ext/fts3/fts3.c
ext/fts3/fts3Int.h
manifest
manifest.uuid

index 7f59e331ebf725c1aa106ec24da7a3016bedb36c..b7d277383dbedd58eaee58d978953d89d31d2a15 100644 (file)
   SQLITE_EXTENSION_INIT1
 #endif
 
+/*
+** GCC does not define the offsetof() macro so we'll have to do it
+** ourselves. This definition was copied from sqlite3Int.h.
+*/
+#ifndef offsetof
+#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
+#endif
+
 static int fts3EvalNext(Fts3Cursor *pCsr);
 static int fts3EvalStart(Fts3Cursor *pCsr);
 static int fts3TermSegReaderCursor(
@@ -1671,19 +1679,25 @@ static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
 ** Implementation of xOpen method.
 */
 static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
-  sqlite3_vtab_cursor *pCsr;               /* Allocated cursor */
-
-  UNUSED_PARAMETER(pVTab);
+  Fts3Table *p = (Fts3Table *)pVTab;
+  Fts3Cursor *pCsr;               /* Allocated cursor */
 
   /* Allocate a buffer large enough for an Fts3Cursor structure. If the
   ** allocation succeeds, zero it and return SQLITE_OK. Otherwise, 
   ** if the allocation fails, return SQLITE_NOMEM.
   */
-  *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
+  pCsr = (Fts3Cursor*)sqlite3_malloc(sizeof(Fts3Cursor));
+  *ppCsr = (sqlite3_vtab_cursor*)pCsr;
   if( !pCsr ){
     return SQLITE_NOMEM;
   }
   memset(pCsr, 0, sizeof(Fts3Cursor));
+
+  /* Link the new cursor into the linked list at Fts3Table.pCsr. */
+  pCsr->pNext = p->pAllCsr;
+  p->pAllCsr = pCsr;
+  pCsr->iId = ++p->iLastCsrId;
+
   return SQLITE_OK;
 }
 
@@ -1712,12 +1726,13 @@ static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){
 ** argument.
 */
 static void fts3ClearCursor(Fts3Cursor *pCsr){
+  const int nZero = sizeof(Fts3Cursor)-offsetof(Fts3Cursor,eSearch); 
   fts3CursorFinalizeStmt(pCsr);
   sqlite3Fts3FreeDeferredTokens(pCsr);
   sqlite3_free(pCsr->aDoclist);
   sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
   sqlite3Fts3ExprFree(pCsr->pExpr);
-  memset(&(&pCsr->base)[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
+  memset((void*)&pCsr->eSearch, 0, nZero);
 }
 
 /*
@@ -1725,10 +1740,14 @@ static void fts3ClearCursor(Fts3Cursor *pCsr){
 ** on the xClose method of the virtual table interface.
 */
 static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
-  Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
-  assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
+  Fts3Table *p = (Fts3Table*)pCursor->pVtab;
+  Fts3Cursor *pCsr = (Fts3Cursor*)pCursor;
+  Fts3Cursor **pp;
+  assert( p->pSegments==0 );
   fts3ClearCursor(pCsr);
-  assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
+  assert( p->pSegments==0 );
+  for(pp=&p->pAllCsr; (*pp)!=pCsr; pp=&(*pp)->pNext);
+  *pp = 0;
   sqlite3_free(pCsr);
   return SQLITE_OK;
 }
@@ -3353,7 +3372,7 @@ static int fts3ColumnMethod(
   switch( iCol-p->nColumn ){
     case 0:
       /* The special 'table-name' column */
-      sqlite3_result_pointer(pCtx, pCsr);
+      sqlite3_result_int(pCtx, pCsr->iId);
       break;
 
     case 1:
@@ -3571,11 +3590,18 @@ static int fts3FunctionArg(
   sqlite3_value *pVal,            /* argv[0] passed to function */
   Fts3Cursor **ppCsr              /* OUT: Store cursor handle here */
 ){
-  int rc;
-  *ppCsr = (Fts3Cursor*)sqlite3_value_pointer(pVal);
-  if( (*ppCsr)!=0 ){
-    rc = SQLITE_OK;
-  }else{
+  int rc = SQLITE_OK;
+  int iId;
+  Fts3Table *p = (Fts3Table*)sqlite3_user_data(pContext);
+  iId = sqlite3_value_int(pVal);
+  Fts3Cursor *pRet;
+
+  for(pRet=p->pAllCsr; pRet; pRet=pRet->pNext){
+    if( pRet->iId==iId ) break;
+  }
+  *ppCsr = pRet;
+
+  if( pRet==0 ){
     char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
     sqlite3_result_error(pContext, zErr, -1);
     sqlite3_free(zErr);
@@ -3735,6 +3761,7 @@ static int fts3FindFunctionMethod(
   for(i=0; i<SizeofArray(aOverload); i++){
     if( strcmp(zName, aOverload[i].zName)==0 ){
       *pxFunc = aOverload[i].xFunc;
+      *ppArg = (void*)pVtab;
       return 1;
     }
   }
index c3cab9d821a435efab8dbd90019362fe3f5d8862..c68fa8c0e12479890505c15a79fb96b43936f4ce 100644 (file)
@@ -288,6 +288,9 @@ struct Fts3Table {
   ** by special insert command 'test-no-incr-doclist'.  */
   int bNoIncrDoclist;
 #endif
+
+  Fts3Cursor *pAllCsr;            /* List of all open cursors */
+  int iLastCsrId;                 /* Last cursor id assigned (or 0) */
 };
 
 /*
@@ -297,6 +300,11 @@ struct Fts3Table {
 */
 struct Fts3Cursor {
   sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
+  int iId;                        /* Numeric id of this cursor */
+  Fts3Cursor *pNext;              /* Next in list of all cursors */
+
+  /* Variables below this point are zeroed by fts3ClearCursor() */
+
   i16 eSearch;                    /* Search strategy (see below) */
   u8 isEof;                       /* True if at End Of Results */
   u8 isRequireSeek;               /* True if must seek pStmt to %_content row */
index 51a952a0f863a17f48cb4087cdead7c974858b64..17d79bff38bdf3e65e68653e87d55de1652d5ed9 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Add\sthe\s"unionvtab"\svirtual\stable\sextension\sin\sext/misc/unionvtab.c.
-D 2017-07-15T20:48:30.280
+C Use\sinteger\shandles\sfor\sfts3\scursors\sfor\sa\ssmall\sperformance\simprovement.
+D 2017-07-17T09:30:15.939
 F Makefile.in d9873c9925917cca9990ee24be17eb9613a668012c85a343aef7e5536ae266e8
 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
 F Makefile.msc 20850e3e8d4d4791e0531955852d768eb06f24138214870d543abb1a47346fba
@@ -70,9 +70,9 @@ F ext/fts3/README.content fdc666a70d5257a64fee209f97cf89e0e6e32b51
 F ext/fts3/README.syntax a19711dc5458c20734b8e485e75fb1981ec2427a
 F ext/fts3/README.tokenizers e0a8b81383ea60d0334d274fadf305ea14a8c314
 F ext/fts3/README.txt 8c18f41574404623b76917b9da66fcb0ab38328d
-F ext/fts3/fts3.c 02fbd2215309a7a73cbf29045897344987b6e17bb0b1685d13155f8b29768a50
+F ext/fts3/fts3.c 839018ad38355de1b6a36240d2b5c4fd32af617b8a8adbeb37239241fcece771
 F ext/fts3/fts3.h 3a10a0af180d502cecc50df77b1b22df142817fe
-F ext/fts3/fts3Int.h eb2502000148e80913b965db3e59f29251266d0a
+F ext/fts3/fts3Int.h 6e503a752e2cf8f4de312d8909a00d6df101c99f270de1861a1d6d0cbc038120
 F ext/fts3/fts3_aux.c 9edc3655fcb287f0467d0a4b886a01c6185fe9f1
 F ext/fts3/fts3_expr.c dfd571a24412779ac01f25c01d888c6ef7b2d0ef
 F ext/fts3/fts3_hash.c 29b986e43f4e9dd40110eafa377dc0d63c422c60
@@ -1635,7 +1635,10 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 253945d480b052bfe311888022b5eb0be91c8c80cda05036e58207d57520262c
-R ee0a9dc5b1464f766485894fa2069cae
+P 62a86aa6c0519cf1fa232169122d3d6ae8d2f66b20530fb934a82a15712bd2f0
+R 1f2a6820ac6b2ca14c452355885bac61
+T *branch * fts3-int-cursor
+T *sym-fts3-int-cursor *
+T -sym-trunk *
 U dan
-Z d195e18cbc4e4f34f8cf4b9b0b0c8aaa
+Z 5522287dc73b8f3fd5c7ba004e151c88
index 58f352fc6eeb3a8b7aae9797d9f22a6f8eb8fa33..7c6b359844b6cd5536d38e6ba7a9aab0d18f2ef2 100644 (file)
@@ -1 +1 @@
-62a86aa6c0519cf1fa232169122d3d6ae8d2f66b20530fb934a82a15712bd2f0
\ No newline at end of file
+f4f7196b877001098278f2a3ee4c6839c5f130c638c930decb559d24ab7a20c1
\ No newline at end of file