/ Check-in [cfb31582]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Avoid unnecessary calls to sqlite3BtreeEnterAll() and sqlite3BtreeLeaveAll() when no btree is using shared-cache.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: cfb3158204628eb2fd170090a7f212df0e4ce6c9
User & Date: drh 2017-01-02 18:19:29
Context
2017-01-02
18:40
Change SQLITE_DEFAULT_PCACHE_INITSZ from 100 to 20, which is experimentally determined to be slightly faster. check-in: 12d9493c user: drh tags: trunk
18:19
Avoid unnecessary calls to sqlite3BtreeEnterAll() and sqlite3BtreeLeaveAll() when no btree is using shared-cache. check-in: cfb31582 user: drh tags: trunk
12:20
Add the --all option to the wordcount test program. Fix the speedtest1 test program so that it builds on MSVC and so that the --lookaside 0 0 option works. check-in: cb338f36 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to src/attach.c.

   133    133       return;
   134    134     }
   135    135     assert( pVfs );
   136    136     flags |= SQLITE_OPEN_MAIN_DB;
   137    137     rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
   138    138     sqlite3_free( zPath );
   139    139     db->nDb++;
          140  +  db->skipBtreeMutex = 0;
   140    141     if( rc==SQLITE_CONSTRAINT ){
   141    142       rc = SQLITE_ERROR;
   142    143       zErrDyn = sqlite3MPrintf(db, "database is already attached");
   143    144     }else if( rc==SQLITE_OK ){
   144    145       Pager *pPager;
   145    146       aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
   146    147       if( !aNew->pSchema ){

Changes to src/btmutex.c.

   179    179   ** There is a corresponding leave-all procedures.
   180    180   **
   181    181   ** Enter the mutexes in accending order by BtShared pointer address
   182    182   ** to avoid the possibility of deadlock when two threads with
   183    183   ** two or more btrees in common both try to lock all their btrees
   184    184   ** at the same instant.
   185    185   */
   186         -void sqlite3BtreeEnterAll(sqlite3 *db){
          186  +static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){
   187    187     int i;
          188  +  int skipOk = 1;
   188    189     Btree *p;
   189    190     assert( sqlite3_mutex_held(db->mutex) );
   190    191     for(i=0; i<db->nDb; i++){
   191    192       p = db->aDb[i].pBt;
   192         -    if( p ) sqlite3BtreeEnter(p);
          193  +    if( p && p->sharable ){
          194  +      sqlite3BtreeEnter(p);
          195  +      skipOk = 0;
   193    196     }
   194    197   }
   195         -void sqlite3BtreeLeaveAll(sqlite3 *db){
          198  +  db->skipBtreeMutex = skipOk;
          199  +}
          200  +void sqlite3BtreeEnterAll(sqlite3 *db){
          201  +  if( db->skipBtreeMutex==0 ) btreeEnterAll(db);
          202  +}
          203  +static void SQLITE_NOINLINE btreeLeaveAll(sqlite3 *db){
   196    204     int i;
   197    205     Btree *p;
   198    206     assert( sqlite3_mutex_held(db->mutex) );
   199    207     for(i=0; i<db->nDb; i++){
   200    208       p = db->aDb[i].pBt;
   201    209       if( p ) sqlite3BtreeLeave(p);
   202    210     }
   203    211   }
          212  +void sqlite3BtreeLeaveAll(sqlite3 *db){
          213  +  if( db->skipBtreeMutex==0 ) btreeLeaveAll(db);
          214  +}
   204    215   
   205    216   #ifndef NDEBUG
   206    217   /*
   207    218   ** Return true if the current thread holds the database connection
   208    219   ** mutex and all required BtShared mutexes.
   209    220   **
   210    221   ** This routine is used inside assert() statements only.

Changes to src/sqliteInt.h.

  1292   1292     u8 bBenignMalloc;             /* Do not require OOMs if true */
  1293   1293     u8 dfltLockMode;              /* Default locking-mode for attached dbs */
  1294   1294     signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
  1295   1295     u8 suppressErr;               /* Do not issue error messages if true */
  1296   1296     u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
  1297   1297     u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
  1298   1298     u8 mTrace;                    /* zero or more SQLITE_TRACE flags */
         1299  +  u8 skipBtreeMutex;            /* True if no shared-cache backends */
  1299   1300     int nextPagesize;             /* Pagesize after VACUUM if >0 */
  1300   1301     u32 magic;                    /* Magic number for detect library misuse */
  1301   1302     int nChange;                  /* Value returned by sqlite3_changes() */
  1302   1303     int nTotalChange;             /* Value returned by sqlite3_total_changes() */
  1303   1304     int aLimit[SQLITE_N_LIMIT];   /* Limits */
  1304   1305     int nMaxSorterMmap;           /* Maximum size of regions mapped by sorter */
  1305   1306     struct sqlite3InitInfo {      /* Information used during initialization */