1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-08 14:02:16 +03:00

Have each {quote: BtShared} structure hang on to a buffer of just under page-size bytes for temporary use. This reduces the number of calls to malloc(). (CVS 4914)

FossilOrigin-Name: fe1bc0f3b7cd87cd65f7d03b91095b59788a6f8d
This commit is contained in:
danielk1977
2008-03-25 14:24:56 +00:00
parent 6507ecb376
commit 52ae7246c6
4 changed files with 30 additions and 14 deletions

View File

@@ -1,5 +1,5 @@
C Fix\sfor\smemory\sleak\sin\smalloc3.test.\s(CVS\s4913) C Have\seach\s{quote:\sBtShared}\sstructure\shang\son\sto\sa\sbuffer\sof\sjust\sunder\spage-size\sbytes\sfor\stemporary\suse.\sThis\sreduces\sthe\snumber\sof\scalls\sto\smalloc().\s(CVS\s4914)
D 2008-03-25T09:56:45 D 2008-03-25T14:24:57
F Makefile.arm-wince-mingw32ce-gcc ac5f7b2cef0cd850d6f755ba6ee4ab961b1fadf7 F Makefile.arm-wince-mingw32ce-gcc ac5f7b2cef0cd850d6f755ba6ee4ab961b1fadf7
F Makefile.in cf434ce8ca902e69126ae0f94fc9f7dc7428a5fa F Makefile.in cf434ce8ca902e69126ae0f94fc9f7dc7428a5fa
F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -86,9 +86,9 @@ F src/attach.c bdc75e759ca25a16f4dc7fbdbc6d37ad2561bb24
F src/auth.c c8b2ab5c8bad4bd90ed7c294694f48269162c627 F src/auth.c c8b2ab5c8bad4bd90ed7c294694f48269162c627
F src/bitvec.c 49817d442e51e4123585f3cf3c2afc293a3c91e2 F src/bitvec.c 49817d442e51e4123585f3cf3c2afc293a3c91e2
F src/btmutex.c 483ced3c52205b04b97df69161fadbf87f4f1ea2 F src/btmutex.c 483ced3c52205b04b97df69161fadbf87f4f1ea2
F src/btree.c 0a170b7bb69035f6e5c4d722e907b908c5480e83 F src/btree.c 366f5f66ebb3dfdc8513242d4aecbfb402dcc0a9
F src/btree.h e02869e1e7753ad5b3c7cb7852e952c95c2e609a F src/btree.h e02869e1e7753ad5b3c7cb7852e952c95c2e609a
F src/btreeInt.h c2deca3e778e2a1e6196343b8087a868f4faa19a F src/btreeInt.h d232be68a7ab2a24376dc6332a869e717551b0bd
F src/build.c d0715b3454b140cb405412e3029ec7c0fb434efd F src/build.c d0715b3454b140cb405412e3029ec7c0fb434efd
F src/callback.c 77b302b0d41468dcda78c70e706e5b84577f0fa0 F src/callback.c 77b302b0d41468dcda78c70e706e5b84577f0fa0
F src/complete.c 4cf68fd75d60257524cbe74f87351b9848399131 F src/complete.c 4cf68fd75d60257524cbe74f87351b9848399131
@@ -617,7 +617,7 @@ F www/tclsqlite.tcl 8be95ee6dba05eabcd27a9d91331c803f2ce2130
F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b
F www/whentouse.tcl fc46eae081251c3c181bd79c5faef8195d7991a5 F www/whentouse.tcl fc46eae081251c3c181bd79c5faef8195d7991a5
P 047153648155654b0cd70b811935209d2e21776c P ef0e40e814b3d3a00721f8ca39bac0db1be24347
R fe7778c82934f6b01597127050469d93 R d58ce8f374758d218509d77eae1fa411
U danielk1977 U danielk1977
Z cbb9339bf69751e7040fab28fe7c6c53 Z 6b876eb9851a975cdfed8252be002c4a

View File

@@ -1 +1 @@
ef0e40e814b3d3a00721f8ca39bac0db1be24347 fe1bc0f3b7cd87cd65f7d03b91095b59788a6f8d

View File

@@ -9,7 +9,7 @@
** May you share freely, never taking more than you give. ** May you share freely, never taking more than you give.
** **
************************************************************************* *************************************************************************
** $Id: btree.c,v 1.445 2008/03/25 09:56:45 danielk1977 Exp $ ** $Id: btree.c,v 1.446 2008/03/25 14:24:57 danielk1977 Exp $
** **
** This file implements a external (disk-based) database using BTrees. ** This file implements a external (disk-based) database using BTrees.
** See the header comment on "btreeInt.h" for additional information. ** See the header comment on "btreeInt.h" for additional information.
@@ -1440,6 +1440,7 @@ int sqlite3BtreeClose(Btree *p){
pBt->xFreeSchema(pBt->pSchema); pBt->xFreeSchema(pBt->pSchema);
} }
sqlite3_free(pBt->pSchema); sqlite3_free(pBt->pSchema);
sqlite3_free(pBt->pTmpSpace);
sqlite3_free(pBt); sqlite3_free(pBt);
} }
@@ -1544,6 +1545,8 @@ int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){
assert( (pageSize & 7)==0 ); assert( (pageSize & 7)==0 );
assert( !pBt->pPage1 && !pBt->pCursor ); assert( !pBt->pPage1 && !pBt->pCursor );
pBt->pageSize = pageSize; pBt->pageSize = pageSize;
sqlite3_free(pBt->pTmpSpace);
pBt->pTmpSpace = 0;
rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
} }
pBt->usableSize = pBt->pageSize - nReserve; pBt->usableSize = pBt->pageSize - nReserve;
@@ -1678,6 +1681,8 @@ static int lockBtree(BtShared *pBt){
releasePage(pPage1); releasePage(pPage1);
pBt->usableSize = usableSize; pBt->usableSize = usableSize;
pBt->pageSize = pageSize; pBt->pageSize = pageSize;
sqlite3_free(pBt->pTmpSpace);
pBt->pTmpSpace = 0;
sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
return SQLITE_OK; return SQLITE_OK;
} }
@@ -5558,6 +5563,16 @@ static int checkReadLocks(Btree *pBtree, Pgno pgnoRoot, BtCursor *pExclude){
return SQLITE_OK; return SQLITE_OK;
} }
/*
** Make sure pBt->pTmpSpace points to an allocation of
** MX_CELL_SIZE(pBt) bytes.
*/
static void allocateTempSpace(BtShared *pBt){
if( !pBt->pTmpSpace ){
pBt->pTmpSpace = sqlite3_malloc(MX_CELL_SIZE(pBt));
}
}
/* /*
** Insert a new record into the BTree. The key is given by (pKey,nKey) ** Insert a new record into the BTree. The key is given by (pKey,nKey)
** and the data is given by (pData,nData). The cursor is used only to ** and the data is given by (pData,nData). The cursor is used only to
@@ -5616,7 +5631,8 @@ int sqlite3BtreeInsert(
pCur->pgnoRoot, nKey, nData, pPage->pgno, pCur->pgnoRoot, nKey, nData, pPage->pgno,
loc==0 ? "overwrite" : "new entry")); loc==0 ? "overwrite" : "new entry"));
assert( pPage->isInit ); assert( pPage->isInit );
newCell = sqlite3_malloc( MX_CELL_SIZE(pBt) ); allocateTempSpace(pBt);
newCell = pBt->pTmpSpace;
if( newCell==0 ) return SQLITE_NOMEM; if( newCell==0 ) return SQLITE_NOMEM;
rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
if( rc ) goto end_insert; if( rc ) goto end_insert;
@@ -5653,7 +5669,6 @@ int sqlite3BtreeInsert(
moveToRoot(pCur); moveToRoot(pCur);
} }
end_insert: end_insert:
sqlite3_free(newCell);
return rc; return rc;
} }
@@ -5742,7 +5757,8 @@ int sqlite3BtreeDelete(BtCursor *pCur){
pNext = findCell(leafCur.pPage, leafCur.idx); pNext = findCell(leafCur.pPage, leafCur.idx);
szNext = cellSizePtr(leafCur.pPage, pNext); szNext = cellSizePtr(leafCur.pPage, pNext);
assert( MX_CELL_SIZE(pBt)>=szNext+4 ); assert( MX_CELL_SIZE(pBt)>=szNext+4 );
tempCell = sqlite3_malloc( MX_CELL_SIZE(pBt) ); allocateTempSpace(pBt);
tempCell = pBt->pTmpSpace;
if( tempCell==0 ){ if( tempCell==0 ){
rc = SQLITE_NOMEM; rc = SQLITE_NOMEM;
} }
@@ -5758,7 +5774,6 @@ int sqlite3BtreeDelete(BtCursor *pCur){
rc = balance(leafCur.pPage, 0); rc = balance(leafCur.pPage, 0);
} }
} }
sqlite3_free(tempCell);
sqlite3BtreeReleaseTempCursor(&leafCur); sqlite3BtreeReleaseTempCursor(&leafCur);
}else{ }else{
TRACE(("DELETE: table=%d delete from leaf %d\n", TRACE(("DELETE: table=%d delete from leaf %d\n",

View File

@@ -9,7 +9,7 @@
** May you share freely, never taking more than you give. ** May you share freely, never taking more than you give.
** **
************************************************************************* *************************************************************************
** $Id: btreeInt.h,v 1.18 2008/03/25 00:22:21 drh Exp $ ** $Id: btreeInt.h,v 1.19 2008/03/25 14:24:57 danielk1977 Exp $
** **
** This file implements a external (disk-based) database using BTrees. ** This file implements a external (disk-based) database using BTrees.
** For a detailed discussion of BTrees, refer to ** For a detailed discussion of BTrees, refer to
@@ -398,6 +398,7 @@ struct BtShared {
BtLock *pLock; /* List of locks held on this shared-btree struct */ BtLock *pLock; /* List of locks held on this shared-btree struct */
Btree *pExclusive; /* Btree with an EXCLUSIVE lock on the whole db */ Btree *pExclusive; /* Btree with an EXCLUSIVE lock on the whole db */
#endif #endif
u8 *pTmpSpace; /* BtShared.pageSize bytes of space for tmp use */
}; };
/* /*