/ Check-in [b406b20e]
Login

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

Overview
Comment:Change an unreachable branch into an assert() in sqlite3PagerAcquire() and optimize sqlite3PcacheOpenSavepoint() by factoring out rarely used code into a subroutine.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: b406b20ecdf0bff63c5c222fab11cb9acee86765
User & Date: drh 2015-06-30 11:07:32
Context
2015-06-30
15:10
Make use of built-in bswap32() and bswap16() functions in GCC/Clang for a significant performance improvement there. check-in: 8bfcda3d user: drh tags: trunk
12:47
Make use of htonl() and __builtin_bswap32() for faster implementations of sqlite3Get4byte() and sqlite3Put4byte(). check-in: bc27ebd7 user: drh tags: bswap-functions
11:07
Change an unreachable branch into an assert() in sqlite3PagerAcquire() and optimize sqlite3PcacheOpenSavepoint() by factoring out rarely used code into a subroutine. check-in: b406b20e user: drh tags: trunk
03:57
Optimization to the sqlite3PcacheFetch() logic. check-in: d9a0481c user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/pager.c.

  5315   5315           if( pBase==0 ){
  5316   5316             pPg = *ppPage = 0;
  5317   5317             rc = SQLITE_NOMEM;
  5318   5318             goto pager_acquire_err;
  5319   5319           }
  5320   5320         }
  5321   5321         pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
  5322         -      if( pPg==0 ) rc = SQLITE_NOMEM;
         5322  +      assert( pPg!=0 );
  5323   5323       }
  5324   5324     }
  5325   5325   
  5326   5326     if( rc!=SQLITE_OK ){
  5327   5327       /* Either the call to sqlite3PcacheFetch() returned an error or the
  5328   5328       ** pager was already in the error-state when this function was called.
  5329   5329       ** Set pPg to 0 and jump to the exception handler.  */
................................................................................
  6471   6471   ** to make up the difference. If the number of savepoints is already
  6472   6472   ** equal to nSavepoint, then this function is a no-op.
  6473   6473   **
  6474   6474   ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error 
  6475   6475   ** occurs while opening the sub-journal file, then an IO error code is
  6476   6476   ** returned. Otherwise, SQLITE_OK.
  6477   6477   */
  6478         -int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
         6478  +static SQLITE_NOINLINE int pagerOpenSavepoint(Pager *pPager, int nSavepoint){
  6479   6479     int rc = SQLITE_OK;                       /* Return code */
  6480   6480     int nCurrent = pPager->nSavepoint;        /* Current number of savepoints */
         6481  +  int ii;                                   /* Iterator variable */
         6482  +  PagerSavepoint *aNew;                     /* New Pager.aSavepoint array */
  6481   6483   
  6482   6484     assert( pPager->eState>=PAGER_WRITER_LOCKED );
         6485  +  assert( assert_pager_state(pPager) );
         6486  +  assert( nSavepoint>nCurrent && pPager->useJournal );
         6487  +
         6488  +  /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
         6489  +  ** if the allocation fails. Otherwise, zero the new portion in case a 
         6490  +  ** malloc failure occurs while populating it in the for(...) loop below.
         6491  +  */
         6492  +  aNew = (PagerSavepoint *)sqlite3Realloc(
         6493  +      pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
         6494  +  );
         6495  +  if( !aNew ){
         6496  +    return SQLITE_NOMEM;
         6497  +  }
         6498  +  memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
         6499  +  pPager->aSavepoint = aNew;
         6500  +
         6501  +  /* Populate the PagerSavepoint structures just allocated. */
         6502  +  for(ii=nCurrent; ii<nSavepoint; ii++){
         6503  +    aNew[ii].nOrig = pPager->dbSize;
         6504  +    if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
         6505  +      aNew[ii].iOffset = pPager->journalOff;
         6506  +    }else{
         6507  +      aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
         6508  +    }
         6509  +    aNew[ii].iSubRec = pPager->nSubRec;
         6510  +    aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
         6511  +    if( !aNew[ii].pInSavepoint ){
         6512  +      return SQLITE_NOMEM;
         6513  +    }
         6514  +    if( pagerUseWal(pPager) ){
         6515  +      sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
         6516  +    }
         6517  +    pPager->nSavepoint = ii+1;
         6518  +  }
         6519  +  assert( pPager->nSavepoint==nSavepoint );
         6520  +  assertTruncateConstraint(pPager);
         6521  +  return rc;
         6522  +}
         6523  +int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
         6524  +  assert( pPager->eState>=PAGER_WRITER_LOCKED );
  6483   6525     assert( assert_pager_state(pPager) );
  6484   6526   
  6485         -  if( nSavepoint>nCurrent && pPager->useJournal ){
  6486         -    int ii;                                 /* Iterator variable */
  6487         -    PagerSavepoint *aNew;                   /* New Pager.aSavepoint array */
         6527  +  if( nSavepoint>pPager->nSavepoint && pPager->useJournal ){
         6528  +    return pagerOpenSavepoint(pPager, nSavepoint);
         6529  +  }else{
         6530  +    return SQLITE_OK;
         6531  +  }
         6532  +}
  6488   6533   
  6489         -    /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
  6490         -    ** if the allocation fails. Otherwise, zero the new portion in case a 
  6491         -    ** malloc failure occurs while populating it in the for(...) loop below.
  6492         -    */
  6493         -    aNew = (PagerSavepoint *)sqlite3Realloc(
  6494         -        pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
  6495         -    );
  6496         -    if( !aNew ){
  6497         -      return SQLITE_NOMEM;
  6498         -    }
  6499         -    memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
  6500         -    pPager->aSavepoint = aNew;
  6501         -
  6502         -    /* Populate the PagerSavepoint structures just allocated. */
  6503         -    for(ii=nCurrent; ii<nSavepoint; ii++){
  6504         -      aNew[ii].nOrig = pPager->dbSize;
  6505         -      if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
  6506         -        aNew[ii].iOffset = pPager->journalOff;
  6507         -      }else{
  6508         -        aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
  6509         -      }
  6510         -      aNew[ii].iSubRec = pPager->nSubRec;
  6511         -      aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
  6512         -      if( !aNew[ii].pInSavepoint ){
  6513         -        return SQLITE_NOMEM;
  6514         -      }
  6515         -      if( pagerUseWal(pPager) ){
  6516         -        sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
  6517         -      }
  6518         -      pPager->nSavepoint = ii+1;
  6519         -    }
  6520         -    assert( pPager->nSavepoint==nSavepoint );
  6521         -    assertTruncateConstraint(pPager);
  6522         -  }
  6523         -
  6524         -  return rc;
  6525         -}
  6526   6534   
  6527   6535   /*
  6528   6536   ** This function is called to rollback or release (commit) a savepoint.
  6529   6537   ** The savepoint to release or rollback need not be the most recently 
  6530   6538   ** created savepoint.
  6531   6539   **
  6532   6540   ** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.