]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
In sqlite3_str_vappendf(), write floating-point renderings directly into
authordrh <>
Sat, 28 Mar 2026 20:24:47 +0000 (20:24 +0000)
committerdrh <>
Sat, 28 Mar 2026 20:24:47 +0000 (20:24 +0000)
the output buffer, saving a memcpy().

FossilOrigin-Name: e4e040e7aaaf5bfccbf0414fee3d08c5fdc24e03d1118dce92d2dd16c614ce63

manifest
manifest.tags
manifest.uuid
src/printf.c
src/sqliteInt.h

index 1a346c19a1bf86dc2e85dcae9453da83a555952b..01fc9826d09cf21b27abbb77e60a27f5902ab7bd 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Remove\sa\sharmless\sunused\svariable\sfrom\sthe\sfp-speed-2.c\sbenchmark.
-D 2026-03-28T14:21:53.670
+C In\ssqlite3_str_vappendf(),\swrite\sfloating-point\srenderings\sdirectly\sinto\nthe\soutput\sbuffer,\ssaving\sa\smemcpy().
+D 2026-03-28T20:24:47.134
 F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
@@ -729,7 +729,7 @@ F src/pcache.h 092b758d2c5e4dabb30eae46d8dfad77c0f70b16bf3ff1943f7a232b0fe0d4ba
 F src/pcache1.c 131ca0daf4e66b4608d2945ae76d6ed90de3f60539afbd5ef9ec65667a5f2fcd
 F src/pragma.c 789ef67117b74b5be0a2db6681f7f0c55e6913791b9da309aefd280de2c8a74d
 F src/prepare.c f6a6e28a281bd1d1da12f47d370a81af46159b40f73bf7fa0b276b664f9c8b7d
-F src/printf.c a440c004a3892cc3d334fa90e0ae59720918fe26c16942d4d45e2534640ce786
+F src/printf.c 3dc62b9608e75aabea38f83fa7ce004ce2de0cef957b99e9b70edff56be9a5e0
 F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c
 F src/resolve.c 928ff887f2a7c64275182060d94d06fdddbe32226c569781cf7e7edc6f58d7fd
 F src/rowset.c 8432130e6c344b3401a8874c3cb49fefe6873fec593294de077afea2dce5ec97
@@ -738,7 +738,7 @@ F src/shell.c.in 91ee40ec3f75192362cbaa0ad85316140b8dde00a184113d73a837fb6173dbc
 F src/sqlite.h.in e7acbb01518f05c5a342149ec1eeb1afcdccf9b90a6e9770a4893ae9a3c756ae
 F src/sqlite3.rc 015537e6ac1eec6c7050e17b616c2ffe6f70fca241835a84a4f0d5937383c479
 F src/sqlite3ext.h 1b7a0ee438bb5c2896d0609c537e917d8057b3340f6ad004d2de44f03e3d3cca
-F src/sqliteInt.h 9716721fb57e32938a1d30a84560ce7633c63860a2209e188c87afad15d4b464
+F src/sqliteInt.h 3ed075ef00be15203ef0560e316b7201c896801abb0c42c3cc444d6fd7c74147
 F src/sqliteLimit.h c70656b67ab5b96741a8f1c812bdd80c81f2b1c1e443d0cc3ea8c33bb1f1a092
 F src/status.c 7565d63a79aa2f326339a24a0461a60096d0bd2bce711fefb50b5c89335f3592
 F src/table.c 0f141b58a16de7e2fbe81c308379e7279f4c6b50eb08efeec5892794a0ba30d1
@@ -2196,8 +2196,11 @@ F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee
 F tool/warnings.sh a554d13f6e5cf3760f041b87939e3d616ec6961859c3245e8ef701d1eafc2ca2
 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
 F tool/winmain.c 00c8fb88e365c9017db14c73d3c78af62194d9644feaf60e220ab0f411f3604c
-P 7d81ff4c509d4039ef9e2f1066284ec560131422db0cbffa7e1826d71bb8f8e1
-R f9e8bfd72c320957630f960480c7f849
+P ff9008e7615d877e6cc852c60097777006b274971646d368eaa56b99a7acf6c7
+R 17c8bb5bf63c6fdc308a7448452e8ccb
+T *branch * direct-printf
+T *sym-direct-printf *
+T -sym-trunk *
 U drh
-Z 1dff429b17934e0b3d9099df4f195e64
+Z e912b19fcd984317ce0146494ddfd817
 # Remove this line to create a well-formed Fossil manifest.
index bec971799ff1b8ee641c166c7aeb22d12c785393..de53f42ef4b6f2c30e7aee1710c96716b1b55890 100644 (file)
@@ -1,2 +1,2 @@
-branch trunk
-tag trunk
+branch direct-printf
+tag direct-printf
index f7583d958f52458f0f7220ff17a259482a946213..40f15381e9382fbc8281ada1b40642b801c17373 100644 (file)
@@ -1 +1 @@
-ff9008e7615d877e6cc852c60097777006b274971646d368eaa56b99a7acf6c7
+e4e040e7aaaf5bfccbf0414fee3d08c5fdc24e03d1118dce92d2dd16c614ce63
index 2374bd1a2df458e8c62f6953e5a98b6c9eb19103..04a593a14c367db2df2ebf46a1c6c8284a825ca1 100644 (file)
@@ -531,6 +531,7 @@ void sqlite3_str_vappendf(
         FpDecode s;
         int iRound;
         int j;
+        i64 szBufNeeded;       /* Size needed to hold the output */
 
         if( bArgList ){
           realvalue = getDoubleArg(pArgList);
@@ -619,17 +620,15 @@ void sqlite3_str_vappendf(
         }else{
           e2 = s.iDP - 1;
         }
-        bufpt = buf;
-        {
-          i64 szBufNeeded;           /* Size of a temporary buffer needed */
-          szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+15;
-          if( cThousand && e2>0 ) szBufNeeded += (e2+2)/3;
-          if( szBufNeeded > etBUFSIZE ){
-            bufpt = zExtra = printfTempBuf(pAccum, szBufNeeded);
-            if( bufpt==0 ) return;
-          }
+
+        szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+8;
+        if( cThousand && e2>0 ) szBufNeeded += (e2+2)/3;
+        if( sqlite3StrAccumEnlargeIfNeeded(pAccum, szBufNeeded) ){
+          width = length = 0;
+          break;
         }
-        zOut = bufpt;
+        bufpt = zOut = pAccum->zText + pAccum->nChar;
+
         flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
         /* The sign in front of the number */
         if( prefix ){
@@ -714,27 +713,31 @@ void sqlite3_str_vappendf(
           *(bufpt++) = (char)(exp/10+'0');             /* 10's digit */
           *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
         }
-        *bufpt = 0;
 
-        /* The converted number is in buf[] and zero terminated. Output it.
-        ** Note that the number is in the usual order, not reversed as with
-        ** integer conversions. */
         length = (int)(bufpt-zOut);
-        bufpt = zOut;
-
-        /* Special case:  Add leading zeros if the flag_zeropad flag is
-        ** set and we are not left justified */
-        if( flag_zeropad && !flag_leftjustify && length < width){
-          int i;
-          int nPad = width - length;
-          for(i=width; i>=nPad; i--){
-            bufpt[i] = bufpt[i-nPad];
+        assert( length <= szBufNeeded );
+        if( length<width ){
+          i64 nPad = width - length;
+          if( flag_leftjustify ){
+            memset(bufpt, ' ', nPad);
+          }else if( !flag_zeropad ){
+            memmove(zOut+nPad, zOut, length);
+            memset(zOut, ' ', nPad);
+          }else{
+            int adj = prefix!=0;
+            memmove(zOut+nPad+adj, zOut+adj, length-adj);
+            memset(zOut+adj, '0', nPad);
           }
-          i = prefix!=0;
-          while( nPad-- ) bufpt[i++] = '0';
           length = width;
         }
-        break;
+        pAccum->nChar += length;
+        zOut[length] = 0;
+
+        /* Floating point conversions render directly into the output
+        ** buffer.  Hence, don't just break out of the switch().  Bypass the
+        ** output buffer writing that occurs after the switch() by continuing
+        ** to the next character in the format string. */
+        continue;
       }
       case etSIZE:
         if( !bArgList ){
@@ -778,11 +781,10 @@ void sqlite3_str_vappendf(
             i64 nCopyBytes;
             if( nPrior > precision-1 ) nPrior = precision - 1;
             nCopyBytes = length*nPrior;
-            if( nCopyBytes + pAccum->nChar >= pAccum->nAlloc ){
-              sqlite3StrAccumEnlarge(pAccum, nCopyBytes);
+            if( sqlite3StrAccumEnlargeIfNeeded(pAccum, nCopyBytes) ){
+              break;
             }
-            if( pAccum->accError ) break;
-            sqlite3_str_append(pAccum,
+           sqlite3_str_append(pAccum,
                  &pAccum->zText[pAccum->nChar-nCopyBytes], nCopyBytes);
             precision -= nPrior;
             nPrior *= 2;
@@ -1128,6 +1130,13 @@ int sqlite3StrAccumEnlarge(StrAccum *p, i64 N){
   return (int)N;
 }
 
+int sqlite3StrAccumEnlargeIfNeeded(StrAccum *p, i64 N){
+  if( N + p->nChar >= p->nAlloc ){
+    sqlite3StrAccumEnlarge(p, N);
+  }
+  return p->accError;
+}
+
 /*
 ** Append N copies of character c to the given string buffer.
 */
index 26a194782415427cbe4432f739dda5e5e036b8fa..fe9e59241f14284de1fcdf4481e81b74f899446c 100644 (file)
@@ -5526,6 +5526,7 @@ char *sqlite3RCStrResize(char*,u64);
 
 void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
 int sqlite3StrAccumEnlarge(StrAccum*, i64);
+int sqlite3StrAccumEnlargeIfNeeded(StrAccum*, i64);
 char *sqlite3StrAccumFinish(StrAccum*);
 void sqlite3StrAccumSetError(StrAccum*, u8);
 void sqlite3ResultStrAccum(sqlite3_context*,StrAccum*);