SQLite

Check-in [dc65ad8c4c]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Performance improvement in sqlite3BtreeNext() and sqlite3BtreePrevious() for the common case of a valid cursor.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: dc65ad8c4c67b21e3b042b8df6580d02b634a90b
User & Date: drh 2013-08-19 20:04:10.623
References
2014-09-29
14:43 New ticket [209d31e316] Assertion fault when deleting a table out from under a SELECT. (artifact: 442fc706a7 user: drh)
Context
2013-08-20
03:13
Performance optimizations in the VDBE and especially to the OP_Next and related opcodes and in the sqlite3BtreeNext() and sqlite3BtreePrevious() routines. This is a cherrypick of [6f99b54aedeb], [d2efea1682a7], and [d78c5d89de4b]. (check-in: 7f72fc4f47 user: drh tags: trunk)
2013-08-19
21:15
Add tointeger() and toreal() SQL functions. (check-in: af49707211 user: mistachkin tags: toTypeFuncs)
20:04
Performance improvement in sqlite3BtreeNext() and sqlite3BtreePrevious() for the common case of a valid cursor. (check-in: dc65ad8c4c user: drh tags: trunk)
19:29
Initialize a variable in fts3_write.c on the grounds that the argument required to show that it does not require initialization with is complicated. Add an assert() to where.c to silence a clang scan-build warning. (check-in: d6c4d48a00 user: dan tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/btree.c.
4792
4793
4794
4795
4796
4797
4798


4799
4800
4801
4802
4803
4804
4805
4806
4807


4808
4809
4810
4811
4812
4813

4814
4815
4816
4817
4818
4819
4820
*/
int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
  int rc;
  int idx;
  MemPage *pPage;

  assert( cursorHoldsMutex(pCur) );


  rc = restoreCursorPosition(pCur);
  if( rc!=SQLITE_OK ){
    return rc;
  }
  assert( pRes!=0 );
  if( CURSOR_INVALID==pCur->eState ){
    *pRes = 1;
    return SQLITE_OK;
  }


  if( pCur->skipNext>0 ){
    pCur->skipNext = 0;
    *pRes = 0;
    return SQLITE_OK;
  }
  pCur->skipNext = 0;


  pPage = pCur->apPage[pCur->iPage];
  idx = ++pCur->aiIdx[pCur->iPage];
  assert( pPage->isInit );

  /* If the database file is corrupt, it is possible for the value of idx 
  ** to be invalid here. This can only occur if a second cursor modifies







>
>
|
|
|
|
<
|
|
|
|
>
>
|
|
|
|
|
|
>







4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804

4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
*/
int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
  int rc;
  int idx;
  MemPage *pPage;

  assert( cursorHoldsMutex(pCur) );
  assert( pRes!=0 );
  if( pCur->eState!=CURSOR_VALID ){
    rc = restoreCursorPosition(pCur);
    if( rc!=SQLITE_OK ){
      return rc;
    }

    if( CURSOR_INVALID==pCur->eState ){
      *pRes = 1;
      return SQLITE_OK;
    }
  }
  if( pCur->skipNext ){
    if( pCur->skipNext>0 ){
      pCur->skipNext = 0;
      *pRes = 0;
      return SQLITE_OK;
    }
    pCur->skipNext = 0;
  }

  pPage = pCur->apPage[pCur->iPage];
  idx = ++pCur->aiIdx[pCur->iPage];
  assert( pPage->isInit );

  /* If the database file is corrupt, it is possible for the value of idx 
  ** to be invalid here. This can only occur if a second cursor modifies
4866
4867
4868
4869
4870
4871
4872



4873
4874
4875
4876
4877
4878
4879
4880
4881


4882
4883
4884
4885
4886
4887

4888
4889
4890
4891
4892
4893
4894
** this routine was called, then set *pRes=1.
*/
int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
  int rc;
  MemPage *pPage;

  assert( cursorHoldsMutex(pCur) );



  rc = restoreCursorPosition(pCur);
  if( rc!=SQLITE_OK ){
    return rc;
  }
  pCur->atLast = 0;
  if( CURSOR_INVALID==pCur->eState ){
    *pRes = 1;
    return SQLITE_OK;
  }


  if( pCur->skipNext<0 ){
    pCur->skipNext = 0;
    *pRes = 0;
    return SQLITE_OK;
  }
  pCur->skipNext = 0;


  pPage = pCur->apPage[pCur->iPage];
  assert( pPage->isInit );
  if( !pPage->leaf ){
    int idx = pCur->aiIdx[pCur->iPage];
    rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
    if( rc ){







>
>
>
|
|
<
|
<
|
|
|
|
>
>
|
|
|
|
|
|
>







4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881

4882

4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
** this routine was called, then set *pRes=1.
*/
int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
  int rc;
  MemPage *pPage;

  assert( cursorHoldsMutex(pCur) );
  pCur->atLast = 0;
  if( pCur->eState!=CURSOR_VALID ){
    if( ALWAYS(pCur->eState>=CURSOR_REQUIRESEEK) ){
      rc = btreeRestoreCursorPosition(pCur);
      if( rc!=SQLITE_OK ) return rc;

    }

    if( CURSOR_INVALID==pCur->eState ){
      *pRes = 1;
      return SQLITE_OK;
    }
  }
  if( pCur->skipNext ){
    if( pCur->skipNext<0 ){
      pCur->skipNext = 0;
      *pRes = 0;
      return SQLITE_OK;
    }
    pCur->skipNext = 0;
  }

  pPage = pCur->apPage[pCur->iPage];
  assert( pPage->isInit );
  if( !pPage->leaf ){
    int idx = pCur->aiIdx[pCur->iPage];
    rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
    if( rc ){