Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Further performance improvements to mem6.c. (CVS 5482) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
4528f7b1cce2d009f1bf32bfb8eeaf3c |
User & Date: | danielk1977 2008-07-25 16:07:01.000 |
Context
2008-07-25
| ||
16:39 | Add an SQLITE_OMIT_LOCALTIME around the "utc" modifier in date/time functions. (CVS 5483) (check-in: 71486e93b2 user: drh tags: trunk) | |
16:07 | Further performance improvements to mem6.c. (CVS 5482) (check-in: 4528f7b1cc user: danielk1977 tags: trunk) | |
15:39 | Add the capability to track the maximum depth of the LALR(1) parser stack so that critical applications can check to see if they are getting close to limits. (CVS 5481) (check-in: ef0250f3dc user: drh tags: trunk) | |
Changes
Changes to src/mem6.c.
︙ | ︙ | |||
28 29 30 31 32 33 34 | ** the same as that used by mem5.c. ** ** This strategy is designed to prevent the default memory allocation ** system (usually the system malloc) from suffering from heap ** fragmentation. On some systems, heap fragmentation can cause a ** significant real-time slowdown. ** | | | 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | ** the same as that used by mem5.c. ** ** This strategy is designed to prevent the default memory allocation ** system (usually the system malloc) from suffering from heap ** fragmentation. On some systems, heap fragmentation can cause a ** significant real-time slowdown. ** ** $Id: mem6.c,v 1.6 2008/07/25 16:07:01 danielk1977 Exp $ */ #ifdef SQLITE_ENABLE_MEMSYS6 #include "sqliteInt.h" /* |
︙ | ︙ | |||
51 52 53 54 55 56 57 58 59 60 61 62 63 64 | */ #define SMALL_MALLOC_DEFAULT_THRESHOLD 256 /* ** Minimum size for a memory chunk. */ #define MIN_CHUNKSIZE (1<<16) typedef struct Mem6Chunk Mem6Chunk; typedef struct Mem6Link Mem6Link; /* ** A minimum allocation is an instance of the following structure. | > > | 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | */ #define SMALL_MALLOC_DEFAULT_THRESHOLD 256 /* ** Minimum size for a memory chunk. */ #define MIN_CHUNKSIZE (1<<16) #define LOG2_MINALLOC 4 typedef struct Mem6Chunk Mem6Chunk; typedef struct Mem6Link Mem6Link; /* ** A minimum allocation is an instance of the following structure. |
︙ | ︙ | |||
98 99 100 101 102 103 104 105 106 107 108 109 110 111 | int nAtom; /* Smallest possible allocation in bytes */ int nBlock; /* Number of nAtom sized blocks in zPool */ u8 *zPool; /* Pointer to memory chunk from which allocations are made */ }; #define MEM6LINK(idx) ((Mem6Link *)(&pChunk->zPool[(idx)*pChunk->nAtom])) /* ** Unlink the chunk at pChunk->aPool[i] from list it is currently ** on. It should be found on pChunk->aiFreelist[iLogsize]. */ static void memsys6Unlink(Mem6Chunk *pChunk, int i, int iLogsize){ int next, prev; assert( i>=0 && i<pChunk->nBlock ); | > > > > > > > > | | 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | int nAtom; /* Smallest possible allocation in bytes */ int nBlock; /* Number of nAtom sized blocks in zPool */ u8 *zPool; /* Pointer to memory chunk from which allocations are made */ }; #define MEM6LINK(idx) ((Mem6Link *)(&pChunk->zPool[(idx)*pChunk->nAtom])) struct Mem6Global { int nMinAlloc; /* Minimum allowed allocation size */ int nThreshold; /* Allocs larger than this go to malloc() */ int nLogThreshold; /* log2 of (nThreshold/nMinAlloc) */ sqlite3_mutex *mutex; Mem6Chunk *pChunk; /* Singly linked list of all memory chunks */ } mem6; /* ** Unlink the chunk at pChunk->aPool[i] from list it is currently ** on. It should be found on pChunk->aiFreelist[iLogsize]. */ static void memsys6Unlink(Mem6Chunk *pChunk, int i, int iLogsize){ int next, prev; assert( i>=0 && i<pChunk->nBlock ); assert( iLogsize>=0 && iLogsize<=mem6.nLogThreshold ); assert( (pChunk->aCtrl[i] & CTRL_LOGSIZE)==iLogsize ); next = MEM6LINK(i)->next; prev = MEM6LINK(i)->prev; if( prev<0 ){ pChunk->aiFreelist[iLogsize] = next; }else{ |
︙ | ︙ | |||
127 128 129 130 131 132 133 | /* ** Link the chunk at mem5.aPool[i] so that is on the iLogsize ** free list. */ static void memsys6Link(Mem6Chunk *pChunk, int i, int iLogsize){ int x; assert( i>=0 && i<pChunk->nBlock ); | | | | | > | > > > > > > > > > > > > > > > > > > > > > > | | > | > | | | < < < < < < | | < | 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | /* ** Link the chunk at mem5.aPool[i] so that is on the iLogsize ** free list. */ static void memsys6Link(Mem6Chunk *pChunk, int i, int iLogsize){ int x; assert( i>=0 && i<pChunk->nBlock ); assert( iLogsize>=0 && iLogsize<=mem6.nLogThreshold ); assert( (pChunk->aCtrl[i] & CTRL_LOGSIZE)==iLogsize ); x = MEM6LINK(i)->next = pChunk->aiFreelist[iLogsize]; MEM6LINK(i)->prev = -1; if( x>=0 ){ assert( x<pChunk->nBlock ); MEM6LINK(x)->prev = i; } pChunk->aiFreelist[iLogsize] = i; } /* ** Find the first entry on the freelist iLogsize. Unlink that ** entry and return its index. */ static int memsys6UnlinkFirst(Mem6Chunk *pChunk, int iLogsize){ int i; int iFirst; assert( iLogsize>=0 && iLogsize<=mem6.nLogThreshold ); i = iFirst = pChunk->aiFreelist[iLogsize]; assert( iFirst>=0 ); memsys6Unlink(pChunk, iFirst, iLogsize); return iFirst; } static int roundupLog2(int n){ static const char LogTable256[256] = { 0, /* 1 */ 1, /* 2 */ 2, 2, /* 3..4 */ 3, 3, 3, 3, /* 5..8 */ 4, 4, 4, 4, 4, 4, 4, 4, /* 9..16 */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, /* 17..32 */ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, /* 33..64 */ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, /* 65..128 */ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, /* 129..256 */ }; assert(n<=(1<<16) && n>0); if( n<=256 ) return LogTable256[n-1]; return LogTable256[(n>>8) - ((n&0xFF)?0:1)] + 8; } /* ** Allocate and return a block of (pChunk->nAtom << iLogsize) bytes from chunk ** pChunk. If the allocation request cannot be satisfied, return 0. */ static void *chunkMalloc(Mem6Chunk *pChunk, int iLogsize){ int i; /* Index of a mem5.aPool[] slot */ int iBin; /* Index into mem5.aiFreelist[] */ /* Make sure mem5.aiFreelist[iLogsize] contains at least one free ** block. If not, then split a block of the next larger power of ** two in order to create a new free block of size iLogsize. */ for(iBin=iLogsize; pChunk->aiFreelist[iBin]<0 && iBin<=mem6.nLogThreshold; iBin++){} if( iBin>mem6.nLogThreshold ) return 0; i = memsys6UnlinkFirst(pChunk, iBin); while( iBin>iLogsize ){ int newSize; iBin--; newSize = 1 << iBin; pChunk->aCtrl[i+newSize] = CTRL_FREE | iBin; memsys6Link(pChunk, i+newSize, iBin); } pChunk->aCtrl[i] = iLogsize; |
︙ | ︙ | |||
221 222 223 224 225 226 227 | size = 1<<iLogsize; assert( iBlock+size-1<pChunk->nBlock ); pChunk->aCtrl[iBlock] |= CTRL_FREE; pChunk->aCtrl[iBlock+size-1] |= CTRL_FREE; pChunk->aCtrl[iBlock] = CTRL_FREE | iLogsize; | | | 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | size = 1<<iLogsize; assert( iBlock+size-1<pChunk->nBlock ); pChunk->aCtrl[iBlock] |= CTRL_FREE; pChunk->aCtrl[iBlock+size-1] |= CTRL_FREE; pChunk->aCtrl[iBlock] = CTRL_FREE | iLogsize; while( iLogsize<mem6.nLogThreshold ){ int iBuddy; if( (iBlock>>iLogsize) & 1 ){ iBuddy = iBlock - size; }else{ iBuddy = iBlock + size; } assert( iBuddy>=0 ); |
︙ | ︙ | |||
287 288 289 290 291 292 293 | memset(pChunk, 0, sizeof(Mem6Chunk)); pChunk->nAtom = nMinAlloc; pChunk->nBlock = ((nChunk-sizeof(Mem6Chunk)) / (pChunk->nAtom+sizeof(u8))); pChunk->zPool = (u8 *)&pChunk[1]; pChunk->aCtrl = &pChunk->zPool[pChunk->nBlock*pChunk->nAtom]; | | | | < < < < < < < < | 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | memset(pChunk, 0, sizeof(Mem6Chunk)); pChunk->nAtom = nMinAlloc; pChunk->nBlock = ((nChunk-sizeof(Mem6Chunk)) / (pChunk->nAtom+sizeof(u8))); pChunk->zPool = (u8 *)&pChunk[1]; pChunk->aCtrl = &pChunk->zPool[pChunk->nBlock*pChunk->nAtom]; for(ii=0; ii<=mem6.nLogThreshold; ii++){ pChunk->aiFreelist[ii] = -1; } iOffset = 0; for(ii=mem6.nLogThreshold; ii>=0; ii--){ int nAlloc = (1<<ii); while( (iOffset+nAlloc)<=pChunk->nBlock ){ pChunk->aCtrl[iOffset] = ii | CTRL_FREE; memsys6Link(pChunk, iOffset, ii); iOffset += nAlloc; } } return pChunk; } static void mem6Enter(void){ sqlite3_mutex_enter(mem6.mutex); } static void mem6Leave(void){ sqlite3_mutex_leave(mem6.mutex); |
︙ | ︙ | |||
334 335 336 337 338 339 340 | Mem6Chunk *p; for(p=mem6.pChunk; p; p=p->pNext){ iTotal = iTotal*2; } return iTotal; } | < < < < < < < < < < < < < < < < > > > > > | < | < < | > | 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | Mem6Chunk *p; for(p=mem6.pChunk; p; p=p->pNext){ iTotal = iTotal*2; } return iTotal; } static void freeChunk(Mem6Chunk *pChunk){ Mem6Chunk **pp = &mem6.pChunk; for( pp=&mem6.pChunk; *pp!=pChunk; pp = &(*pp)->pNext ); *pp = (*pp)->pNext; free(pChunk); } static void *memsys6Malloc(int nByte){ Mem6Chunk *pChunk; void *p = 0; int nTotal = nByte+8; int iOffset = 0; if( nTotal>mem6.nThreshold ){ p = malloc(nTotal); }else{ int iLogsize = 0; if( nTotal>(1<<LOG2_MINALLOC) ){ iLogsize = roundupLog2(nTotal) - LOG2_MINALLOC; } mem6Enter(); for(pChunk=mem6.pChunk; pChunk; pChunk=pChunk->pNext){ p = chunkMalloc(pChunk, iLogsize); if( p ){ break; } } if( !p ){ int iSize = nextChunkSize(); p = malloc(iSize); if( p ){ pChunk = chunkInit((u8 *)p, iSize, mem6.nMinAlloc); pChunk->pNext = mem6.pChunk; mem6.pChunk = pChunk; p = chunkMalloc(pChunk, iLogsize); assert(p); } } iOffset = ((u8*)p - (u8*)pChunk); mem6Leave(); } if( !p ){ return 0; } ((u32 *)p)[0] = iOffset; ((u32 *)p)[1] = nByte; return &((u32 *)p)[2]; |
︙ | ︙ | |||
435 436 437 438 439 440 441 | memsys6Free(p); } } return p2; } | < < | | > > > | > < | 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | memsys6Free(p); } } return p2; } static int memsys6Roundup(int n){ if( n>mem6.nThreshold ){ return n; }else{ return (1<<roundupLog2(n)); } } static int memsys6Init(void *pCtx){ u8 bMemstat = sqlite3Config.bMemstat; mem6.nMinAlloc = (1 << LOG2_MINALLOC); mem6.pChunk = 0; mem6.nThreshold = sqlite3Config.nSmall; if( mem6.nThreshold<=0 ){ mem6.nThreshold = SMALL_MALLOC_DEFAULT_THRESHOLD; } mem6.nLogThreshold = roundupLog2(mem6.nThreshold) - LOG2_MINALLOC; if( !bMemstat ){ mem6.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); } return SQLITE_OK; } static void memsys6Shutdown(void *pCtx){ memset(&mem6, 0, sizeof(mem6)); } |
︙ | ︙ |