]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Enhance sqlite3PcacheTruncate() to run faster in the common case where the
authordrh <drh@noemail.net>
Thu, 11 Aug 2016 13:03:51 +0000 (13:03 +0000)
committerdrh <drh@noemail.net>
Thu, 11 Aug 2016 13:03:51 +0000 (13:03 +0000)
cutoff is just a few pages less than the page number highwater mark.

FossilOrigin-Name: 03dceaea15aa357097a4615ef6044be36d505044

manifest
manifest.uuid
src/pcache1.c

index 9d5c14e49e15f41ba8ebb3267326504d342cfde7..06d71f0e3e4dc5b6ddf1dbd5c8d3cc6e680b00bb 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Version\s3.14
-D 2016-08-08T13:40:27.974
+C Enhance\ssqlite3PcacheTruncate()\sto\srun\sfaster\sin\sthe\scommon\scase\swhere\sthe\ncutoff\sis\sjust\sa\sfew\spages\sless\sthan\sthe\spage\snumber\shighwater\smark.
+D 2016-08-11T13:03:51.493
 F Makefile.in cfd8fb987cd7a6af046daa87daa146d5aad0e088
 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
 F Makefile.msc d66d0395c38571aab3804f8db0fa20707ae4609a
@@ -375,7 +375,7 @@ F src/pager.h 966d2769e76ae347c8a32c4165faf6e6cb64546d
 F src/parse.y 99b676e6fc2f4e331ab93e76b3987cffdbd28efa
 F src/pcache.c 5583c8ade4b05075a60ba953ef471d1c1a9c05df
 F src/pcache.h 2cedcd8407eb23017d92790b112186886e179490
-F src/pcache1.c 7f51d2b541aab57596adf62db2c4bb025d34f04d
+F src/pcache1.c 4bb7a6a5300c67d0b033d25adb509c120c03e812
 F src/pragma.c c8b499756658cb8b82cfdbb5845c22cf11f297aa
 F src/pragma.h 64c78a648751b9f4f297276c4eb7507b14b4628c
 F src/prepare.c 22df6171aec1d86904ed2ad30c2348a5748aa04e
@@ -1509,10 +1509,12 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P ebc396a19fa79bea208ecda277ffff5d02166d0b
-R e7962d3372d6a1e29e3980753a6f0162
-T +bgcolor * #d0c0ff
-T +sym-release *
-T +sym-version-3.14.0 *
+P d5e98057028abcf7217d0d2b2e29bbbcdf09d6de
+Q +059f4e2efefb7b9deaf539110c19bceaeb10c6ce
+Q +b07a26df06a2ffb946ff8a1cc7f43eaf701a94b5
+R 58ac6a5fe1b5145799405ee268d389bb
+T *branch * branch-3.14
+T *sym-branch-3.14 *
+T -sym-trunk *
 U drh
-Z ecb9b7072c2b71d36f58909f83563ec2
+Z 9c6be3afbd2ad00f204f6e02d3202729
index 732a4af61b711705ca45b57738a7d507c62476ec..0a19ad23dd5ed33a30417e57edcae0bcb4169aa3 100644 (file)
@@ -1 +1 @@
-d5e98057028abcf7217d0d2b2e29bbbcdf09d6de
\ No newline at end of file
+03dceaea15aa357097a4615ef6044be36d505044
\ No newline at end of file
index 5fe963ad05d9b4339102b5addfdd82111657b413..1b1971a3993507cfff6c841071bbcb31bd574765 100644 (file)
@@ -632,12 +632,30 @@ static void pcache1TruncateUnsafe(
   PCache1 *pCache,             /* The cache to truncate */
   unsigned int iLimit          /* Drop pages with this pgno or larger */
 ){
-  TESTONLY( unsigned int nPage = 0; )  /* To assert pCache->nPage is correct */
-  unsigned int h;
+  TESTONLY( int nPage = 0; )  /* To assert pCache->nPage is correct */
+  unsigned int h, iStop;
   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
-  for(h=0; h<pCache->nHash; h++){
-    PgHdr1 **pp = &pCache->apHash[h]; 
+  assert( pCache->iMaxKey >= iLimit );
+  assert( pCache->nHash > 0 );
+  if( pCache->iMaxKey - iLimit < pCache->nHash ){
+    /* If we are just shaving the last few pages off the end of the
+    ** cache, then there is no point in scanning the entire hash table.
+    ** Only scan those hash slots that might contain pages that need to
+    ** be removed. */
+    h = iLimit % pCache->nHash;
+    iStop = pCache->iMaxKey % pCache->nHash;
+    TESTONLY( nPage = -10; )  /* Disable the pCache->nPage validity check */
+  }else{
+    /* This is the general case where many pages are being removed.
+    ** It is necessary to scan the entire hash table */
+    h = pCache->nHash/2;
+    iStop = h - 1;
+  }
+  for(;;){
+    PgHdr1 **pp;
     PgHdr1 *pPage;
+    assert( h<pCache->nHash );
+    pp = &pCache->apHash[h]; 
     while( (pPage = *pp)!=0 ){
       if( pPage->iKey>=iLimit ){
         pCache->nPage--;
@@ -646,11 +664,13 @@ static void pcache1TruncateUnsafe(
         pcache1FreePage(pPage);
       }else{
         pp = &pPage->pNext;
-        TESTONLY( nPage++; )
+        TESTONLY( if( nPage>=0 ) nPage++; )
       }
     }
+    if( h==iStop ) break;
+    h = (h+1) % pCache->nHash;
   }
-  assert( pCache->nPage==nPage );
+  assert( nPage<0 || pCache->nPage==(unsigned)nPage );
 }
 
 /******************************************************************************/
@@ -1127,7 +1147,7 @@ static void pcache1Destroy(sqlite3_pcache *p){
   PGroup *pGroup = pCache->pGroup;
   assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
   pcache1EnterMutex(pGroup);
-  pcache1TruncateUnsafe(pCache, 0);
+  if( pCache->nPage ) pcache1TruncateUnsafe(pCache, 0);
   assert( pGroup->nMaxPage >= pCache->nMax );
   pGroup->nMaxPage -= pCache->nMax;
   assert( pGroup->nMinPage >= pCache->nMin );