/ Check-in [dcf4fb8d]
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 mutex usage in pcache1, for a significant speedup.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | faster-pcache1-fetch
Files: files | file ages | folders
SHA1: dcf4fb8d764611de60afea27cda0a8548ba7ca82
User & Date: drh 2015-06-13 11:10:55
Context
2015-06-13
11:19
Omit mutex calls in pcache1 when the mutex is NULL, for a significant performance improvement. check-in: caf8f574 user: drh tags: trunk
11:10
Avoid unnecessary mutex usage in pcache1, for a significant speedup. Closed-Leaf check-in: dcf4fb8d user: drh tags: faster-pcache1-fetch
2015-06-12
13:49
Add a fast-path implementation of pcache1Fetch() for the common case of separate caches that do not use a mutex. check-in: 760700ed user: drh tags: faster-pcache1-fetch
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/pcache1.c.

   144    144   ** compiling for systems that do not support real WSD.
   145    145   */
   146    146   #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
   147    147   
   148    148   /*
   149    149   ** Macros to enter and leave the PCache LRU mutex.
   150    150   */
   151         -#define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
   152         -#define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
          151  +#if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
          152  +# define pcache1EnterMutex(X)  assert((X)->mutex==0)
          153  +# define pcache1LeaveMutex(X)  assert((X)->mutex==0)
          154  +# define PCACHE1_MIGHT_USE_GROUP_MUTEX 0
          155  +#else
          156  +# define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
          157  +# define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
          158  +# define PCACHE1_MIGHT_USE_GROUP_MUTEX 1
          159  +#endif
   153    160   
   154    161   /******************************************************************************/
   155    162   /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
   156    163   
   157    164   /*
   158    165   ** This function is called during initialization if a static buffer is 
   159    166   ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
................................................................................
   525    532   /*
   526    533   ** Implementation of the sqlite3_pcache.xInit method.
   527    534   */
   528    535   static int pcache1Init(void *NotUsed){
   529    536     UNUSED_PARAMETER(NotUsed);
   530    537     assert( pcache1.isInit==0 );
   531    538     memset(&pcache1, 0, sizeof(pcache1));
          539  +#if SQLITE_THREADSAFE
   532    540     if( sqlite3GlobalConfig.bCoreMutex ){
   533    541       pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
   534    542       pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
   535    543     }
          544  +#endif
   536    545     pcache1.grp.mxPinned = 10;
   537    546     pcache1.isInit = 1;
   538    547     return SQLITE_OK;
   539    548   }
   540    549   
   541    550   /*
   542    551   ** Implementation of the sqlite3_pcache.xShutdown method.
................................................................................
   832    841     }else if( createFlag ){
   833    842       /* Steps 3, 4, and 5 implemented by this subroutine */
   834    843       return pcache1FetchStage2(pCache, iKey, createFlag);
   835    844     }else{
   836    845       return 0;
   837    846     }
   838    847   }
          848  +#if PCACHE1_MIGHT_USE_GROUP_MUTEX
   839    849   static PgHdr1 *pcache1FetchWithMutex(
   840    850     sqlite3_pcache *p, 
   841    851     unsigned int iKey, 
   842    852     int createFlag
   843    853   ){
   844    854     PCache1 *pCache = (PCache1 *)p;
   845    855     PgHdr1 *pPage;
................................................................................
   846    856   
   847    857     pcache1EnterMutex(pCache->pGroup);
   848    858     pPage = pcache1FetchNoMutex(p, iKey, createFlag);
   849    859     assert( pPage==0 || pCache->iMaxKey>=iKey );
   850    860     pcache1LeaveMutex(pCache->pGroup);
   851    861     return pPage;
   852    862   }
          863  +#endif
   853    864   static sqlite3_pcache_page *pcache1Fetch(
   854    865     sqlite3_pcache *p, 
   855    866     unsigned int iKey, 
   856    867     int createFlag
   857    868   ){
          869  +#if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG)
   858    870     PCache1 *pCache = (PCache1 *)p;
          871  +#endif
   859    872   
   860    873     assert( offsetof(PgHdr1,page)==0 );
   861    874     assert( pCache->bPurgeable || createFlag!=1 );
   862    875     assert( pCache->bPurgeable || pCache->nMin==0 );
   863    876     assert( pCache->bPurgeable==0 || pCache->nMin==10 );
   864    877     assert( pCache->nMin==0 || pCache->bPurgeable );
   865    878     assert( pCache->nHash>0 );
          879  +#if PCACHE1_MIGHT_USE_GROUP_MUTEX
   866    880     if( pCache->pGroup->mutex ){
   867    881       return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag);
   868         -  }else{
          882  +  }else
          883  +#endif
          884  +  {
   869    885       return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag);
   870    886     }
   871    887   }
   872    888   
   873    889   
   874    890   /*
   875    891   ** Implementation of the sqlite3_pcache.xUnpin method.