]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Make the BTree balance() routine a little faster by reusing database
authordrh <drh@noemail.net>
Mon, 8 Jul 2002 02:16:37 +0000 (02:16 +0000)
committerdrh <drh@noemail.net>
Mon, 8 Jul 2002 02:16:37 +0000 (02:16 +0000)
pages locally rather than freeing and reallocating them. (CVS 666)

FossilOrigin-Name: 3c2dea4310af491d6cb09856d4bc5236d6dc44ac

manifest
manifest.uuid
src/btree.c

index 4b901b8f4012c2cc3ff2e60bc68918fcb0072353..e50db17c1a1b2016515f47021951bc632095fc2a 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Version\s2.5.6\s(CVS\s664)
-D 2002-07-07T17:13:00
+C Make\sthe\sBTree\sbalance()\sroutine\sa\slittle\sfaster\sby\sreusing\sdatabase\r\npages\slocally\srather\sthan\sfreeing\sand\sreallocating\sthem.\s(CVS\s666)
+D 2002-07-08T02:16:38
 F Makefile.in 6291a33b87d2a395aafd7646ee1ed562c6f2c28c
 F Makefile.template 4e11752e0b5c7a043ca50af4296ec562857ba495
 F README a4c0ba11354ef6ba0776b400d057c59da47a4cc0
@@ -18,7 +18,7 @@ F publish.sh 1a04b9aa0d9c9661e338268343476ed0851c5778
 F spec.template 238f7db425a78dc1bb7682e56e3834c7270a3f5e
 F sqlite.1 83f4a9d37bdf2b7ef079a82d54eaf2e3509ee6ea
 F src/TODO af7f3cab0228e34149cf98e073aa83d45878e7e6
-F src/btree.c 73212bda34c491ce9165464c3e1ada737acf5e06
+F src/btree.c ce999ee3f5c6130aa12529ba4f510679d0a07faa
 F src/btree.h 8abeabfe6e0b1a990b64fa457592a6482f6674f3
 F src/build.c ea4a3bc15d6338294e68100f642edf48e4082403
 F src/delete.c 215492ffcea4262a993e55f3c4a67dc9fea4da9c
@@ -140,7 +140,7 @@ F www/speed.tcl da8afcc1d3ccc5696cfb388a68982bc3d9f7f00f
 F www/sqlite.tcl ae3dcfb077e53833b59d4fcc94d8a12c50a44098
 F www/tclsqlite.tcl 1db15abeb446aad0caf0b95b8b9579720e4ea331
 F www/vdbe.tcl 2013852c27a02a091d39a766bc87cff329f21218
-P ee86704daf184307fe98b5631f22ceb3d701afce
-R 40fe3b9213d364dad4e1cb9b6ce3c5e4
+P 111c78e6835306fcd8b6d22b9ae68dfb9ab4febe
+R 66eadf0358149489c17da262453c92f0
 U drh
-Z 0f5647bf09d0573645455993f27ff345
+Z 70877f6880980e12b35ccd8168df5635
index 3ba299caa10686ce56f55bc961cfd24a62bffc22..0f38434159b08a1573a2e35d72f11cd1952acd04 100644 (file)
@@ -1 +1 @@
-111c78e6835306fcd8b6d22b9ae68dfb9ab4febe
\ No newline at end of file
+3c2dea4310af491d6cb09856d4bc5236d6dc44ac
\ No newline at end of file
index 01200492e46f5a9e89cfd44e800f93295153e258..9dfb46dd9fb82e7796b21e413f5aee9ee8651207 100644 (file)
@@ -9,7 +9,7 @@
 **    May you share freely, never taking more than you give.
 **
 *************************************************************************
-** $Id: btree.c,v 1.65 2002/07/07 16:52:47 drh Exp $
+** $Id: btree.c,v 1.66 2002/07/08 02:16:38 drh Exp $
 **
 ** This file implements a external (disk-based) database using BTrees.
 ** For a detailed discussion of BTrees, refer to
@@ -2099,10 +2099,6 @@ static int balance(Btree *pBt, MemPage *pPage, BtCursor *pCur){
   */
   for(i=0; i<nOld; i++){
     copyPage(&aOld[i], apOld[i]);
-    rc = freePage(pBt, apOld[i], pgnoOld[i]);
-    if( rc ) goto balance_cleanup;
-    sqlitepager_unref(apOld[i]);
-    apOld[i] = &aOld[i];
   }
 
   /*
@@ -2112,7 +2108,7 @@ static int balance(Btree *pBt, MemPage *pPage, BtCursor *pCur){
   */
   nCell = 0;
   for(i=0; i<nOld; i++){
-    MemPage *pOld = apOld[i];
+    MemPage *pOld = &aOld[i];
     for(j=0; j<pOld->nCell; j++){
       apCell[nCell] = pOld->apCell[j];
       szCell[nCell] = cellSize(apCell[nCell]);
@@ -2166,16 +2162,33 @@ static int balance(Btree *pBt, MemPage *pPage, BtCursor *pCur){
   assert( cntNew[0]>0 );
 
   /*
-  ** Allocate k new pages
+  ** Allocate k new pages.  Reuse old pages where possible.
   */
   for(i=0; i<k; i++){
-    rc = allocatePage(pBt, &apNew[i], &pgnoNew[i]);
-    if( rc ) goto balance_cleanup;
+    if( i<nOld ){
+      apNew[i] = apOld[i];
+      pgnoNew[i] = pgnoOld[i];
+      apOld[i] = 0;
+      sqlitepager_write(apNew[i]);
+    }else{
+      rc = allocatePage(pBt, &apNew[i], &pgnoNew[i]);
+      if( rc ) goto balance_cleanup;
+    }
     nNew++;
     zeroPage(apNew[i]);
     apNew[i]->isInit = 1;
   }
 
+  /* Free any old pages that were not reused as new pages.
+  */
+  while( i<nOld ){
+    rc = freePage(pBt, apOld[i], pgnoOld[i]);
+    if( rc ) goto balance_cleanup;
+    sqlitepager_unref(apOld[i]);
+    apOld[i] = 0;
+    i++;
+  }
+
   /*
   ** Put the new pages in accending order.  This helps to
   ** keep entries in the disk file in order so that a scan
@@ -2236,7 +2249,7 @@ static int balance(Btree *pBt, MemPage *pPage, BtCursor *pCur){
     }
   }
   assert( j==nCell );
-  apNew[nNew-1]->u.hdr.rightChild = apOld[nOld-1]->u.hdr.rightChild;
+  apNew[nNew-1]->u.hdr.rightChild = aOld[nOld-1].u.hdr.rightChild;
   if( nxDiv==pParent->nCell ){
     pParent->u.hdr.rightChild = pgnoNew[nNew-1];
   }else{
@@ -2274,7 +2287,7 @@ balance_cleanup:
     sqlitepager_unref(extraUnref);
   }
   for(i=0; i<nOld; i++){
-    if( apOld[i]!=&aOld[i] ) sqlitepager_unref(apOld[i]);
+    if( apOld[i]!=0 && apOld[i]!=&aOld[i] ) sqlitepager_unref(apOld[i]);
   }
   for(i=0; i<nNew; i++){
     sqlitepager_unref(apNew[i]);