/ Check-in [9227ad48]
Login

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

Overview
Comment:Performance optimizations for sqlite3VXPrintf().
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 9227ad48e1612b32a3a3e9551c49890f93abc0a7
User & Date: drh 2013-12-09 19:03:26
Context
2013-12-09
19:25
Minor performance optimizations in pager.c. check-in: ba9eef5f user: drh tags: trunk
19:03
Performance optimizations for sqlite3VXPrintf(). check-in: 9227ad48 user: drh tags: trunk
12:24
Fix an incorrect test number in the output of the speedtest1.c program. No changes to the test algorithms. check-in: fbfc075a user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/btree.c.

  7741   7741     pCheck->mxErr--;
  7742   7742     pCheck->nErr++;
  7743   7743     va_start(ap, zFormat);
  7744   7744     if( pCheck->errMsg.nChar ){
  7745   7745       sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
  7746   7746     }
  7747   7747     if( zMsg1 ){
  7748         -    sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1);
         7748  +    sqlite3StrAccumAppendAll(&pCheck->errMsg, zMsg1);
  7749   7749     }
  7750   7750     sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
  7751   7751     va_end(ap);
  7752   7752     if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
  7753   7753       pCheck->mallocFailed = 1;
  7754   7754     }
  7755   7755   }

Changes to src/build.c.

  3985   3985     Table *pTab = pIdx->pTable;
  3986   3986   
  3987   3987     sqlite3StrAccumInit(&errMsg, 0, 0, 200);
  3988   3988     errMsg.db = pParse->db;
  3989   3989     for(j=0; j<pIdx->nKeyCol; j++){
  3990   3990       char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
  3991   3991       if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2);
  3992         -    sqlite3StrAccumAppend(&errMsg, pTab->zName, -1);
         3992  +    sqlite3StrAccumAppendAll(&errMsg, pTab->zName);
  3993   3993       sqlite3StrAccumAppend(&errMsg, ".", 1);
  3994         -    sqlite3StrAccumAppend(&errMsg, zCol, -1);
         3994  +    sqlite3StrAccumAppendAll(&errMsg, zCol);
  3995   3995     }
  3996   3996     zErr = sqlite3StrAccumFinish(&errMsg);
  3997   3997     sqlite3HaltConstraint(pParse, 
  3998   3998       (pIdx->autoIndex==2)?SQLITE_CONSTRAINT_PRIMARYKEY:SQLITE_CONSTRAINT_UNIQUE,
  3999   3999       onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
  4000   4000   }
  4001   4001   

Changes to src/printf.c.

   143    143       sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
   144    144       N -= sizeof(zSpaces)-1;
   145    145     }
   146    146     if( N>0 ){
   147    147       sqlite3StrAccumAppend(pAccum, zSpaces, N);
   148    148     }
   149    149   }
          150  +
          151  +/*
          152  +** Set the StrAccum object to an error mode.
          153  +*/
          154  +void setStrAccumError(StrAccum *p, u8 eError){
          155  +  p->accError = eError;
          156  +  p->nAlloc = 0;
          157  +}
   150    158   
   151    159   /*
   152    160   ** On machines with a small stack size, you can redefine the
   153    161   ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
   154    162   */
   155    163   #ifndef SQLITE_PRINT_BUF_SIZE
   156    164   # define SQLITE_PRINT_BUF_SIZE 70
................................................................................
   355    363           if( precision<etBUFSIZE-10 ){
   356    364             nOut = etBUFSIZE;
   357    365             zOut = buf;
   358    366           }else{
   359    367             nOut = precision + 10;
   360    368             zOut = zExtra = sqlite3Malloc( nOut );
   361    369             if( zOut==0 ){
   362         -            pAccum->accError = STRACCUM_NOMEM;
          370  +            setStrAccumError(pAccum, STRACCUM_NOMEM);
   363    371               return;
   364    372             }
   365    373           }
   366    374           bufpt = &zOut[nOut-1];
   367    375           if( xtype==etORDINAL ){
   368    376             static const char zOrd[] = "thstndrd";
   369    377             int x = (int)(longvalue % 10);
................................................................................
   467    475             e2 = 0;
   468    476           }else{
   469    477             e2 = exp;
   470    478           }
   471    479           if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){
   472    480             bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 );
   473    481             if( bufpt==0 ){
   474         -            pAccum->accError = STRACCUM_NOMEM;
          482  +            setStrAccumError(pAccum, STRACCUM_NOMEM);
   475    483               return;
   476    484             }
   477    485           }
   478    486           zOut = bufpt;
   479    487           nsd = 16 + flag_altform2*10;
   480    488           flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
   481    489           /* The sign in front of the number */
................................................................................
   602    610             if( ch==q )  n++;
   603    611           }
   604    612           needQuote = !isnull && xtype==etSQLESCAPE2;
   605    613           n += i + 1 + needQuote*2;
   606    614           if( n>etBUFSIZE ){
   607    615             bufpt = zExtra = sqlite3Malloc( n );
   608    616             if( bufpt==0 ){
   609         -            pAccum->accError = STRACCUM_NOMEM;
          617  +            setStrAccumError(pAccum, STRACCUM_NOMEM);
   610    618               return;
   611    619             }
   612    620           }else{
   613    621             bufpt = buf;
   614    622           }
   615    623           j = 0;
   616    624           if( needQuote ) bufpt[j++] = q;
................................................................................
   637    645         }
   638    646         case etSRCLIST: {
   639    647           SrcList *pSrc = va_arg(ap, SrcList*);
   640    648           int k = va_arg(ap, int);
   641    649           struct SrcList_item *pItem = &pSrc->a[k];
   642    650           assert( k>=0 && k<pSrc->nSrc );
   643    651           if( pItem->zDatabase ){
   644         -          sqlite3StrAccumAppend(pAccum, pItem->zDatabase, -1);
          652  +          sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
   645    653             sqlite3StrAccumAppend(pAccum, ".", 1);
   646    654           }
   647         -        sqlite3StrAccumAppend(pAccum, pItem->zName, -1);
          655  +        sqlite3StrAccumAppendAll(pAccum, pItem->zName);
   648    656           length = width = 0;
   649    657           break;
   650    658         }
   651    659         default: {
   652    660           assert( xtype==etINVALID );
   653    661           return;
   654    662         }
................................................................................
   680    688   } /* End of function */
   681    689   
   682    690   /*
   683    691   ** Append N bytes of text from z to the StrAccum object.
   684    692   */
   685    693   void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
   686    694     assert( z!=0 || N==0 );
   687         -  if( p->accError ){
   688         -    testcase(p->accError==STRACCUM_TOOBIG);
   689         -    testcase(p->accError==STRACCUM_NOMEM);
   690         -    return;
   691         -  }
   692         -  assert( p->zText!=0 || p->nChar==0 );
   693         -  if( N<=0 ){
   694         -    if( N==0 || z[0]==0 ) return;
   695         -    N = sqlite3Strlen30(z);
   696         -  }
          695  +  assert( p->zText!=0 || p->nChar==0 || p->accError );
          696  +  assert( N>=0 );
          697  +  assert( p->accError==0 || p->nAlloc==0 );
   697    698     if( p->nChar+N >= p->nAlloc ){
   698    699       char *zNew;
          700  +    if( p->accError ){
          701  +      testcase(p->accError==STRACCUM_TOOBIG);
          702  +      testcase(p->accError==STRACCUM_NOMEM);
          703  +      return;
          704  +    }
   699    705       if( !p->useMalloc ){
   700         -      p->accError = STRACCUM_TOOBIG;
   701    706         N = p->nAlloc - p->nChar - 1;
          707  +      setStrAccumError(p, STRACCUM_TOOBIG);
   702    708         if( N<=0 ){
   703    709           return;
   704    710         }
   705    711       }else{
   706    712         char *zOld = (p->zText==p->zBase ? 0 : p->zText);
   707    713         i64 szNew = p->nChar;
   708    714         szNew += N + 1;
   709    715         if( szNew > p->mxAlloc ){
   710    716           sqlite3StrAccumReset(p);
   711         -        p->accError = STRACCUM_TOOBIG;
          717  +        setStrAccumError(p, STRACCUM_TOOBIG);
   712    718           return;
   713    719         }else{
   714    720           p->nAlloc = (int)szNew;
   715    721         }
   716    722         if( p->useMalloc==1 ){
   717    723           zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
   718    724         }else{
   719    725           zNew = sqlite3_realloc(zOld, p->nAlloc);
   720    726         }
   721    727         if( zNew ){
   722    728           if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
   723    729           p->zText = zNew;
   724    730         }else{
   725         -        p->accError = STRACCUM_NOMEM;
   726    731           sqlite3StrAccumReset(p);
          732  +        setStrAccumError(p, STRACCUM_NOMEM);
   727    733           return;
   728    734         }
   729    735       }
   730    736     }
   731    737     assert( p->zText );
   732    738     memcpy(&p->zText[p->nChar], z, N);
   733    739     p->nChar += N;
   734    740   }
          741  +
          742  +/*
          743  +** Append the complete text of zero-terminated string z[] to the p string.
          744  +*/
          745  +void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){
          746  +  return sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z));
          747  +}
          748  +
   735    749   
   736    750   /*
   737    751   ** Finish off a string by making sure it is zero-terminated.
   738    752   ** Return a pointer to the resulting string.  Return a NULL
   739    753   ** pointer if any kind of error was encountered.
   740    754   */
   741    755   char *sqlite3StrAccumFinish(StrAccum *p){
................................................................................
   746    760           p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
   747    761         }else{
   748    762           p->zText = sqlite3_malloc(p->nChar+1);
   749    763         }
   750    764         if( p->zText ){
   751    765           memcpy(p->zText, p->zBase, p->nChar+1);
   752    766         }else{
   753         -        p->accError = STRACCUM_NOMEM;
          767  +        setStrAccumError(p, STRACCUM_NOMEM);
   754    768         }
   755    769       }
   756    770     }
   757    771     return p->zText;
   758    772   }
   759    773   
   760    774   /*

Changes to src/sqliteInt.h.

  3218   3218     FuncDestructor *pDestructor
  3219   3219   );
  3220   3220   int sqlite3ApiExit(sqlite3 *db, int);
  3221   3221   int sqlite3OpenTempDatabase(Parse *);
  3222   3222   
  3223   3223   void sqlite3StrAccumInit(StrAccum*, char*, int, int);
  3224   3224   void sqlite3StrAccumAppend(StrAccum*,const char*,int);
         3225  +void sqlite3StrAccumAppendAll(StrAccum*,const char*);
  3225   3226   void sqlite3AppendSpace(StrAccum*,int);
  3226   3227   char *sqlite3StrAccumFinish(StrAccum*);
  3227   3228   void sqlite3StrAccumReset(StrAccum*);
  3228   3229   void sqlite3SelectDestInit(SelectDest*,int,int);
  3229   3230   Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
  3230   3231   
  3231   3232   void sqlite3BackupRestart(sqlite3_backup *);

Changes to src/where.c.

  2564   2564   static void explainAppendTerm(
  2565   2565     StrAccum *pStr,             /* The text expression being built */
  2566   2566     int iTerm,                  /* Index of this term.  First is zero */
  2567   2567     const char *zColumn,        /* Name of the column */
  2568   2568     const char *zOp             /* Name of the operator */
  2569   2569   ){
  2570   2570     if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
  2571         -  sqlite3StrAccumAppend(pStr, zColumn, -1);
         2571  +  sqlite3StrAccumAppendAll(pStr, zColumn);
  2572   2572     sqlite3StrAccumAppend(pStr, zOp, 1);
  2573   2573     sqlite3StrAccumAppend(pStr, "?", 1);
  2574   2574   }
  2575   2575   
  2576   2576   /*
  2577   2577   ** Argument pLevel describes a strategy for scanning table pTab. This 
  2578   2578   ** function returns a pointer to a string buffer containing a description
................................................................................
  2610   2610     for(i=0; i<nEq; i++){
  2611   2611       char *z = (i==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[i]].zName;
  2612   2612       if( i>=nSkip ){
  2613   2613         explainAppendTerm(&txt, i, z, "=");
  2614   2614       }else{
  2615   2615         if( i ) sqlite3StrAccumAppend(&txt, " AND ", 5);
  2616   2616         sqlite3StrAccumAppend(&txt, "ANY(", 4);
  2617         -      sqlite3StrAccumAppend(&txt, z, -1);
         2617  +      sqlite3StrAccumAppendAll(&txt, z);
  2618   2618         sqlite3StrAccumAppend(&txt, ")", 1);
  2619   2619       }
  2620   2620     }
  2621   2621   
  2622   2622     j = i;
  2623   2623     if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
  2624   2624       char *z = (j==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[j]].zName;