diff options
author | drh <drh@noemail.net> | 2018-02-20 15:23:37 +0000 |
---|---|---|
committer | drh <drh@noemail.net> | 2018-02-20 15:23:37 +0000 |
commit | cc398969e058b006bf87e35700b366977fc8682c (patch) | |
tree | 199c67541ca02bb9ae9099fc75f18bf58a76a948 /src/printf.c | |
parent | b0b6f8783c8a80fbf68e38a3230182edeacef306 (diff) | |
download | sqlite-cc398969e058b006bf87e35700b366977fc8682c.tar.gz sqlite-cc398969e058b006bf87e35700b366977fc8682c.zip |
Optimize calls to sqlite3_mprintf("%z...") so that they attempt to append
text onto the end of the existing memory allocation rather than reallocating
and copying.
FossilOrigin-Name: 4bc8a48e644562f6e6192f4c6fc4a70f6bb59f8126ed6c6dc876bedf65d74cda
Diffstat (limited to 'src/printf.c')
-rw-r--r-- | src/printf.c | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/printf.c b/src/printf.c index 11d5b8282..fcbd7fdbe 100644 --- a/src/printf.c +++ b/src/printf.c @@ -206,6 +206,11 @@ void sqlite3VXPrintf( PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ char buf[etBUFSIZE]; /* Conversion buffer */ + /* pAccum never starts out with an empty buffer that was obtained from + ** malloc(). This precondition is required by the mprintf("%z...") + ** optimization. */ + assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); + bufpt = 0; if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){ pArgList = va_arg(ap, PrintfArguments*); @@ -681,6 +686,18 @@ void sqlite3VXPrintf( if( bufpt==0 ){ bufpt = ""; }else if( xtype==etDYNSTRING ){ + if( pAccum->nChar==0 && pAccum->mxAlloc && width==0 && precision<0 ){ + /* Special optimization for sqlite3_mprintf("%z..."): + ** Extend an existing memory allocation rather than creating + ** a new one. */ + assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 ); + pAccum->zText = bufpt; + pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt); + pAccum->nChar = 0x7fffffff & (int)strlen(bufpt); + pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED; + length = 0; + break; + } zExtra = bufpt; } if( precision>=0 ){ |