/ Check-in [6e52fa5f]
Login

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

Overview
Comment:Remove the zBase field from the StrAccum object. Resulting code is slightly smaller and faster.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6e52fa5fd79988a433bae0152ceae036edab4bb18d2b48ed04c1f53f141728b0
User & Date: drh 2017-08-12 02:01:55
Context
2017-08-12
02:16
Update the speed-check.sh test script to append log output to the end of the cout-NAME.txt file. check-in: 14d262d6 user: drh tags: trunk
02:01
Remove the zBase field from the StrAccum object. Resulting code is slightly smaller and faster. check-in: 6e52fa5f user: drh tags: trunk
01:09
Size and performance micro-optimization on sqlite3SrcListIndexedBy(). check-in: 28a5aec1 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/printf.c.

   778    778     if( p->mxAlloc==0 ){
   779    779       N = p->nAlloc - p->nChar - 1;
   780    780       setStrAccumError(p, STRACCUM_TOOBIG);
   781    781       return N;
   782    782     }else{
   783    783       char *zOld = isMalloced(p) ? p->zText : 0;
   784    784       i64 szNew = p->nChar;
   785         -    assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) );
   786    785       szNew += N + 1;
   787    786       if( szNew+p->nChar<=p->mxAlloc ){
   788    787         /* Force exponential buffer size growth as long as it does not overflow,
   789    788         ** to avoid having to call this routine too often */
   790    789         szNew += p->nChar;
   791    790       }
   792    791       if( szNew > p->mxAlloc ){
................................................................................
   820    819   ** Append N copies of character c to the given string buffer.
   821    820   */
   822    821   void sqlite3AppendChar(StrAccum *p, int N, char c){
   823    822     testcase( p->nChar + (i64)N > 0x7fffffff );
   824    823     if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
   825    824       return;
   826    825     }
   827         -  assert( (p->zText==p->zBase)==!isMalloced(p) );
   828    826     while( (N--)>0 ) p->zText[p->nChar++] = c;
   829    827   }
   830    828   
   831    829   /*
   832    830   ** The StrAccum "p" is not large enough to accept N new bytes of z[].
   833    831   ** So enlarge if first, then do the append.
   834    832   **
................................................................................
   838    836   */
   839    837   static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){
   840    838     N = sqlite3StrAccumEnlarge(p, N);
   841    839     if( N>0 ){
   842    840       memcpy(&p->zText[p->nChar], z, N);
   843    841       p->nChar += N;
   844    842     }
   845         -  assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) );
   846    843   }
   847    844   
   848    845   /*
   849    846   ** Append N bytes of text from z to the StrAccum object.  Increase the
   850    847   ** size of the memory allocation for StrAccum if necessary.
   851    848   */
   852    849   void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
................................................................................
   873    870   
   874    871   /*
   875    872   ** Finish off a string by making sure it is zero-terminated.
   876    873   ** Return a pointer to the resulting string.  Return a NULL
   877    874   ** pointer if any kind of error was encountered.
   878    875   */
   879    876   static SQLITE_NOINLINE char *strAccumFinishRealloc(StrAccum *p){
          877  +  char *zText;
   880    878     assert( p->mxAlloc>0 && !isMalloced(p) );
   881         -  p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
   882         -  if( p->zText ){
   883         -    memcpy(p->zText, p->zBase, p->nChar+1);
          879  +  zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
          880  +  if( zText ){
          881  +    memcpy(zText, p->zText, p->nChar+1);
   884    882       p->printfFlags |= SQLITE_PRINTF_MALLOCED;
   885    883     }else{
   886    884       setStrAccumError(p, STRACCUM_NOMEM);
   887    885     }
   888         -  return p->zText;
          886  +  p->zText = zText;
          887  +  return zText;
   889    888   }
   890    889   char *sqlite3StrAccumFinish(StrAccum *p){
   891    890     if( p->zText ){
   892         -    assert( (p->zText==p->zBase)==!isMalloced(p) );
   893    891       p->zText[p->nChar] = 0;
   894    892       if( p->mxAlloc>0 && !isMalloced(p) ){
   895    893         return strAccumFinishRealloc(p);
   896    894       }
   897    895     }
   898    896     return p->zText;
   899    897   }
   900    898   
   901    899   /*
   902    900   ** Reset an StrAccum string.  Reclaim all malloced memory.
   903    901   */
   904    902   void sqlite3StrAccumReset(StrAccum *p){
   905         -  assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) );
   906    903     if( isMalloced(p) ){
   907    904       sqlite3DbFree(p->db, p->zText);
   908    905       p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
   909    906     }
   910    907     p->zText = 0;
   911    908   }
   912    909   
................................................................................
   921    918   **        is malloced.
   922    919   ** n:     Size of zBase in bytes.  If total space requirements never exceed
   923    920   **        n then no memory allocations ever occur.
   924    921   ** mx:    Maximum number of bytes to accumulate.  If mx==0 then no memory
   925    922   **        allocations will ever occur.
   926    923   */
   927    924   void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
   928         -  p->zText = p->zBase = zBase;
          925  +  p->zText = zBase;
   929    926     p->db = db;
   930         -  p->nChar = 0;
   931    927     p->nAlloc = n;
   932    928     p->mxAlloc = mx;
          929  +  p->nChar = 0;
   933    930     p->accError = 0;
   934    931     p->printfFlags = 0;
   935    932   }
   936    933   
   937    934   /*
   938    935   ** Print into memory obtained from sqliteMalloc().  Use the internal
   939    936   ** %-conversion extensions.

Changes to src/sqliteInt.h.

  3223   3223   
  3224   3224   /*
  3225   3225   ** An objected used to accumulate the text of a string where we
  3226   3226   ** do not necessarily know how big the string will be in the end.
  3227   3227   */
  3228   3228   struct StrAccum {
  3229   3229     sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
  3230         -  char *zBase;         /* A base allocation.  Not from malloc. */
  3231   3230     char *zText;         /* The string collected so far */
  3232         -  u32  nChar;          /* Length of the string so far */
  3233   3231     u32  nAlloc;         /* Amount of space allocated in zText */
  3234   3232     u32  mxAlloc;        /* Maximum allowed allocation.  0 for no malloc usage */
         3233  +  u32  nChar;          /* Length of the string so far */
  3235   3234     u8   accError;       /* STRACCUM_NOMEM or STRACCUM_TOOBIG */
  3236   3235     u8   printfFlags;    /* SQLITE_PRINTF flags below */
  3237   3236   };
  3238   3237   #define STRACCUM_NOMEM   1
  3239   3238   #define STRACCUM_TOOBIG  2
  3240   3239   #define SQLITE_PRINTF_INTERNAL 0x01  /* Internal-use-only converters allowed */
  3241   3240   #define SQLITE_PRINTF_SQLFUNC  0x02  /* SQL function arguments to VXPrintf */