/ Check-in [eb071eb5]
Login

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

Overview
Comment:Performance enhancements and size reduction for sqlite3VXPrintf()
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: eb071eb58cf675bd6cd9dd46aed9d57fbb7a77e8
User & Date: drh 2014-03-17 14:24:27
Context
2014-03-17
15:06
Clean up some obsolete "register" declarations in printf.c. check-in: ecd9d3f9 user: drh tags: trunk
14:24
Performance enhancements and size reduction for sqlite3VXPrintf() check-in: eb071eb5 user: drh tags: trunk
2014-03-14
11:46
Fix a harmless compiler warning that crops up with SQLITE_MAX_MMAP_SIZE=0. check-in: 1277932b user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/printf.c.

   130    130     d = digit;
   131    131     digit += '0';
   132    132     *val = (*val - d)*10.0;
   133    133     return (char)digit;
   134    134   }
   135    135   #endif /* SQLITE_OMIT_FLOATING_POINT */
   136    136   
   137         -/*
   138         -** Append N space characters to the given string buffer.
   139         -*/
   140         -void sqlite3AppendSpace(StrAccum *pAccum, int N){
   141         -  static const char zSpaces[] = "                             ";
   142         -  while( N>=(int)sizeof(zSpaces)-1 ){
   143         -    sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
   144         -    N -= sizeof(zSpaces)-1;
   145         -  }
   146         -  if( N>0 ){
   147         -    sqlite3StrAccumAppend(pAccum, zSpaces, N);
   148         -  }
   149         -}
   150         -
   151    137   /*
   152    138   ** Set the StrAccum object to an error mode.
   153    139   */
   154    140   static void setStrAccumError(StrAccum *p, u8 eError){
   155    141     p->accError = eError;
   156    142     p->nAlloc = 0;
   157    143   }
................................................................................
   233    219       }
   234    220       useIntern = bFlags & SQLITE_PRINTF_INTERNAL;
   235    221     }else{
   236    222       bArgList = useIntern = 0;
   237    223     }
   238    224     for(; (c=(*fmt))!=0; ++fmt){
   239    225       if( c!='%' ){
   240         -      int amt;
   241    226         bufpt = (char *)fmt;
   242         -      amt = 1;
   243         -      while( (c=(*++fmt))!='%' && c!=0 ) amt++;
   244         -      sqlite3StrAccumAppend(pAccum, bufpt, amt);
          227  +      while( (c=(*++fmt))!='%' && c!=0 ){};
          228  +      sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt));
   245    229         if( c==0 ) break;
   246    230       }
   247    231       if( (c=(*++fmt))==0 ){
   248    232         sqlite3StrAccumAppend(pAccum, "%", 1);
   249    233         break;
   250    234       }
   251    235       /* Find out what flags are present */
................................................................................
   725    709         }
   726    710       }/* End switch over the format type */
   727    711       /*
   728    712       ** The text of the conversion is pointed to by "bufpt" and is
   729    713       ** "length" characters long.  The field width is "width".  Do
   730    714       ** the output.
   731    715       */
   732         -    if( !flag_leftjustify ){
   733         -      register int nspace;
   734         -      nspace = width-length;
   735         -      if( nspace>0 ){
   736         -        sqlite3AppendSpace(pAccum, nspace);
   737         -      }
   738         -    }
   739         -    if( length>0 ){
   740         -      sqlite3StrAccumAppend(pAccum, bufpt, length);
   741         -    }
   742         -    if( flag_leftjustify ){
   743         -      register int nspace;
   744         -      nspace = width-length;
   745         -      if( nspace>0 ){
   746         -        sqlite3AppendSpace(pAccum, nspace);
   747         -      }
   748         -    }
          716  +    width -= length;
          717  +    if( width>0 && !flag_leftjustify ) sqlite3AppendSpace(pAccum, width);
          718  +    sqlite3StrAccumAppend(pAccum, bufpt, length);
          719  +    if( width>0 && flag_leftjustify ) sqlite3AppendSpace(pAccum, width);
          720  +
   749    721       if( zExtra ) sqlite3_free(zExtra);
   750    722     }/* End for loop over the format string */
   751    723   } /* End of function */
   752    724   
   753    725   /*
   754         -** Append N bytes of text from z to the StrAccum object.
          726  +** Enlarge the memory allocation on a StrAccum object so that it is
          727  +** able to accept at least N more bytes of text.
          728  +**
          729  +** Return the number of bytes of text that StrAccum is able to accept
          730  +** after the attempted enlargement.  The value returned might be zero.
          731  +*/
          732  +static int sqlite3StrAccumEnlarge(StrAccum *p, int N){
          733  +  char *zNew;
          734  +  assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */
          735  +  if( p->accError ){
          736  +    testcase(p->accError==STRACCUM_TOOBIG);
          737  +    testcase(p->accError==STRACCUM_NOMEM);
          738  +    return 0;
          739  +  }
          740  +  if( !p->useMalloc ){
          741  +    N = p->nAlloc - p->nChar - 1;
          742  +    setStrAccumError(p, STRACCUM_TOOBIG);
          743  +    return N;
          744  +  }else{
          745  +    char *zOld = (p->zText==p->zBase ? 0 : p->zText);
          746  +    i64 szNew = p->nChar;
          747  +    szNew += N + 1;
          748  +    if( szNew > p->mxAlloc ){
          749  +      sqlite3StrAccumReset(p);
          750  +      setStrAccumError(p, STRACCUM_TOOBIG);
          751  +      return 0;
          752  +    }else{
          753  +      p->nAlloc = (int)szNew;
          754  +    }
          755  +    if( p->useMalloc==1 ){
          756  +      zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
          757  +    }else{
          758  +      zNew = sqlite3_realloc(zOld, p->nAlloc);
          759  +    }
          760  +    if( zNew ){
          761  +      if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
          762  +      p->zText = zNew;
          763  +    }else{
          764  +      sqlite3StrAccumReset(p);
          765  +      setStrAccumError(p, STRACCUM_NOMEM);
          766  +      return 0;
          767  +    }
          768  +  }
          769  +  return N;
          770  +}
          771  +
          772  +/*
          773  +** Append N space characters to the given string buffer.
          774  +*/
          775  +void sqlite3AppendSpace(StrAccum *p, int N){
          776  +  if( p->nChar+N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ) return;
          777  +  while( (N--)>0 ) p->zText[p->nChar++] = ' ';
          778  +}
          779  +
          780  +/*
          781  +** The StrAccum "p" is not large enough to accept N new bytes of z[].
          782  +** So enlarge if first, then do the append.
          783  +**
          784  +** This is a helper routine to sqlite3StrAccumAppend() that does special-case
          785  +** work (enlarging the buffer) using tail recursion, so that the
          786  +** sqlite3StrAccumAppend() routine can use fast calling semantics.
          787  +*/
          788  +static void enlargeAndAppend(StrAccum *p, const char *z, int N){
          789  +  N = sqlite3StrAccumEnlarge(p, N);
          790  +  if( N>0 ){
          791  +    memcpy(&p->zText[p->nChar], z, N);
          792  +    p->nChar += N;
          793  +  }
          794  +}
          795  +
          796  +/*
          797  +** Append N bytes of text from z to the StrAccum object.  Increase the
          798  +** size of the memory allocation for StrAccum if necessary.
   755    799   */
   756    800   void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
   757    801     assert( z!=0 );
   758    802     assert( p->zText!=0 || p->nChar==0 || p->accError );
   759    803     assert( N>=0 );
   760    804     assert( p->accError==0 || p->nAlloc==0 );
   761    805     if( p->nChar+N >= p->nAlloc ){
   762         -    char *zNew;
   763         -    if( p->accError ){
   764         -      testcase(p->accError==STRACCUM_TOOBIG);
   765         -      testcase(p->accError==STRACCUM_NOMEM);
   766         -      return;
   767         -    }
   768         -    if( !p->useMalloc ){
   769         -      N = p->nAlloc - p->nChar - 1;
   770         -      setStrAccumError(p, STRACCUM_TOOBIG);
   771         -      if( N<=0 ){
   772         -        return;
   773         -      }
   774         -    }else{
   775         -      char *zOld = (p->zText==p->zBase ? 0 : p->zText);
   776         -      i64 szNew = p->nChar;
   777         -      szNew += N + 1;
   778         -      if( szNew > p->mxAlloc ){
   779         -        sqlite3StrAccumReset(p);
   780         -        setStrAccumError(p, STRACCUM_TOOBIG);
   781         -        return;
   782         -      }else{
   783         -        p->nAlloc = (int)szNew;
   784         -      }
   785         -      if( p->useMalloc==1 ){
   786         -        zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
   787         -      }else{
   788         -        zNew = sqlite3_realloc(zOld, p->nAlloc);
   789         -      }
   790         -      if( zNew ){
   791         -        if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
   792         -        p->zText = zNew;
   793         -      }else{
   794         -        sqlite3StrAccumReset(p);
   795         -        setStrAccumError(p, STRACCUM_NOMEM);
   796         -        return;
   797         -      }
   798         -    }
          806  +    enlargeAndAppend(p,z,N);
          807  +    return;
   799    808     }
   800    809     assert( p->zText );
   801    810     memcpy(&p->zText[p->nChar], z, N);
   802    811     p->nChar += N;
   803    812   }
   804    813   
   805    814   /*