Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Minor internal changes to vdbesort.c. Also, default to merging lists together 16 at a time. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | experimental |
Files: | files | file ages | folders |
SHA1: |
9ddc324a34dbf97acef92eef21f8a35f |
User & Date: | dan 2011-08-05 11:49:12.597 |
Context
2011-08-06
| ||
12:01 | In temp files used for merge sorting, store the size of each packed-memory-array at the start of the array itself. This is to avoid having to store the offsets of all arrays in the (potentially very large) file in main-memory. (check-in: 8051c1767c user: dan tags: experimental) | |
2011-08-05
| ||
11:49 | Minor internal changes to vdbesort.c. Also, default to merging lists together 16 at a time. (check-in: 9ddc324a34 user: dan tags: experimental) | |
2011-08-04
| ||
18:43 | Fix a comment in vdbesort.c. (check-in: db8518cab8 user: dan tags: experimental) | |
Changes
Changes to src/vdbesort.c.
︙ | ︙ | |||
85 86 87 88 89 90 91 | ** ** In other words, each time we advance to the next sorter element, log2(N) ** key comparison operations are required, where N is the number of segments ** being merged (rounded up to the next power of 2). */ struct VdbeSorter { int nWorking; /* Start a new b-tree after this many pages */ | < | 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | ** ** In other words, each time we advance to the next sorter element, log2(N) ** key comparison operations are required, where N is the number of segments ** being merged (rounded up to the next power of 2). */ struct VdbeSorter { int nWorking; /* Start a new b-tree after this many pages */ int nTree; /* Used size of aTree/aIter (power of 2) */ VdbeSorterIter *aIter; /* Array of iterators to merge */ int *aTree; /* Current state of incremental merge */ i64 iWriteOff; /* Current write offset within file pTemp1 */ sqlite3_file *pTemp1; /* PMA file 1 */ i64 *aOffset; /* Array of PMA offsets for file 1 */ |
︙ | ︙ | |||
114 115 116 117 118 119 120 | u8 *aKey; /* Pointer to current key */ }; /* Minimum allowable value for the VdbeSorter.nWorking variable */ #define SORTER_MIN_SEGMENT_SIZE 10 /* Maximum number of segments to merge in a single go */ | | < | 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | u8 *aKey; /* Pointer to current key */ }; /* Minimum allowable value for the VdbeSorter.nWorking variable */ #define SORTER_MIN_SEGMENT_SIZE 10 /* Maximum number of segments to merge in a single go */ #define SORTER_MAX_MERGE_COUNT 16 /* ** Append integer iOff to the VdbeSorter.aOffset[] array of the sorter object ** passed as the second argument. SQLITE_NOMEM is returned if an OOM error ** is encountered, or SQLITE_OK if no error occurs. ** ** TODO: The aOffset[] array may grow indefinitely. Fix this. */ static int vdbeSorterAppendOffset(sqlite3 *db, VdbeSorter *p, i64 iOff){ p->aOffset = sqlite3DbReallocOrFree( db, p->aOffset, (p->nOffset+1)*sizeof(i64) ); if( !p->aOffset ) return SQLITE_NOMEM; p->aOffset[p->nOffset++] = iOff; return SQLITE_OK; } |
︙ | ︙ | |||
288 289 290 291 292 293 294 | ** Free any cursor components allocated by sqlite3VdbeSorterXXX routines. */ void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){ VdbeSorter *pSorter = pCsr->pSorter; if( pSorter ){ if( pSorter->aIter ){ int i; | | < | 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | ** Free any cursor components allocated by sqlite3VdbeSorterXXX routines. */ void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){ VdbeSorter *pSorter = pCsr->pSorter; if( pSorter ){ if( pSorter->aIter ){ int i; for(i=0; i<pSorter->nTree; i++){ vdbeSorterIterZero(db, &pSorter->aIter[i]); } sqlite3DbFree(db, pSorter->aIter); } if( pSorter->pTemp1 ){ sqlite3OsCloseFree(pSorter->pTemp1); } sqlite3DbFree(db, pSorter->aOffset); sqlite3DbFree(db, pSorter); pCsr->pSorter = 0; |
︙ | ︙ | |||
444 445 446 447 448 449 450 | rc = sqlite3BtreeCursor(pCsr->pBt, iRoot, 1, pCsr->pKeyInfo, p); } } } return rc; } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < > | > > > > > > > > > > > > > < < < < < < < < < < < < < | 441 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 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 | rc = sqlite3BtreeCursor(pCsr->pBt, iRoot, 1, pCsr->pKeyInfo, p); } } } return rc; } /* ** Helper function for sqlite3VdbeSorterRewind(). */ static int vdbeSorterInitMerge( sqlite3 *db, VdbeCursor *pCsr, int iFirst, int *piNext ){ VdbeSorter *pSorter = pCsr->pSorter; int rc = SQLITE_OK; int i; int N = 2; int nIter; /* Number of iterators to initialize. */ nIter = pSorter->nOffset - iFirst; if( nIter>SORTER_MAX_MERGE_COUNT ){ nIter = SORTER_MAX_MERGE_COUNT; } assert( nIter>0 ); while( N<nIter ) N += N; /* Allocate aIter[] and aTree[], if required. */ if( pSorter->aIter==0 ){ int nByte = N * (sizeof(int) + sizeof(VdbeSorterIter)); pSorter->aIter = (VdbeSorterIter *)sqlite3DbMallocZero(db, nByte); if( !pSorter->aIter ) return SQLITE_NOMEM; pSorter->aTree = (int *)&pSorter->aIter[N]; } /* Initialize as many iterators as possible. */ for(i=iFirst; rc==SQLITE_OK && i<pSorter->nOffset && (i-iFirst)<SORTER_MAX_MERGE_COUNT; i++ ){ int iIter = i - iFirst; if( rc==SQLITE_OK ){ VdbeSorterIter *pIter = &pSorter->aIter[iIter]; i64 iStart = pSorter->aOffset[i]; i64 iEof; if( i==(pSorter->nOffset-1) ){ iEof = pSorter->iWriteOff; }else{ iEof = pSorter->aOffset[i+1]; } rc = vdbeSorterIterInit(db, pSorter->pTemp1, iStart, iEof, pIter); } } *piNext = i; assert( i>iFirst ); pSorter->nTree = N; /* Populate the aTree[] array. */ for(i=N-1; rc==SQLITE_OK && i>0; i--){ rc = vdbeSorterDoCompare(pCsr, i); } |
︙ | ︙ | |||
556 557 558 559 560 561 562 | if( rc!=SQLITE_OK ) return rc; if( pSorter->nOffset==0 ){ *pbEof = 1; return SQLITE_OK; } while( rc==SQLITE_OK ){ | < | < | | | | | 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | if( rc!=SQLITE_OK ) return rc; if( pSorter->nOffset==0 ){ *pbEof = 1; return SQLITE_OK; } while( rc==SQLITE_OK ){ int iNext = 0; /* Index of next segment to open */ int iNew = 0; /* Index of new, merged, PMA */ do { /* This call configures iterators for merging. */ rc = vdbeSorterInitMerge(db, pCsr, iNext, &iNext); assert( iNext>0 ); assert( rc!=SQLITE_OK || pSorter->aIter[ pSorter->aTree[1] ].pFile ); if( rc==SQLITE_OK && (iNew>0 || iNext<pSorter->nOffset) ){ int bEof = 0; if( pTemp2==0 ){ rc = vdbeSorterOpenTempFile(db, &pTemp2); } if( rc==SQLITE_OK ){ pSorter->aOffset[iNew] = iWrite2; } while( rc==SQLITE_OK && bEof==0 ){ int nByte; VdbeSorterIter *pIter = &pSorter->aIter[ pSorter->aTree[1] ]; assert( pIter->pFile ); nByte = pIter->nKey + sqlite3VarintLen(pIter->nKey); rc = sqlite3OsWrite(pTemp2, pIter->aAlloc, nByte, iWrite2); iWrite2 += nByte; if( rc==SQLITE_OK ){ rc = sqlite3VdbeSorterNext(db, pCsr, &bEof); } } iNew++; } }while( rc==SQLITE_OK && iNext<pSorter->nOffset ); if( iNew==0 ){ break; }else{ sqlite3_file *pTmp = pSorter->pTemp1; pSorter->nOffset = iNew; pSorter->pTemp1 = pTemp2; pTemp2 = pTmp; pSorter->iWriteOff = iWrite2; iWrite2 = 0; } } |
︙ | ︙ |