From: drh <> Date: Sun, 22 Mar 2026 17:50:48 +0000 (+0000) Subject: Additional performance improvements in sqlite3AtoF(). Also update the X-Git-Tag: major-release~62^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=dfd31e158cc6cb0011036f03b8230eee485d5df0;p=thirdparty%2Fsqlite.git Additional performance improvements in sqlite3AtoF(). Also update the header comments. FossilOrigin-Name: 8c3a1b450d6d5778004b9fb0e92b4f3f90d3e06ff87276370d01a45b7d607d23 --- diff --git a/manifest b/manifest index f171d63349..20df92d160 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Further\ssimplification\sand\sperformance\simprovement -D 2026-03-22T02:51:18.233 +C Additional\sperformance\simprovements\sin\ssqlite3AtoF().\s\sAlso\supdate\sthe\nheader\scomments. +D 2026-03-22T17:50:48.147 F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea @@ -798,7 +798,7 @@ F src/trigger.c 4bf3bfb3851d165e4404a9f9e69357345f3f7103378c07e07139fdd8aeb7bd20 F src/update.c 3e5e7ff66fa19ebe4d1b113d480639a24cc1175adbefabbd1a948a07f28e37cf F src/upsert.c 215328c3f91623c520ec8672c44323553f12caeb4f01b1090ebdca99fdf7b4f1 F src/utf.c 7267c3fb9e2467020507601af3354c2446c61f444387e094c779dccd5ca62165 -F src/util.c 44aeebb0daa0ea3109daadff33399014378e05ced9220f6151fa03c2fa4380cc +F src/util.c b7ec2de13f540ca184173f44845888f5b727f92b9d7255ca7309bec7ed0bf184 F src/vacuum.c d3d35d8ae893d419ade5fa196d761a83bddcbb62137a1a157ae751ef38b26e82 F src/vdbe.c dbafc6f5762f9ae90d9dc9ed065a31743a8ceafbdbfb1a0a3a228921fb3cc36a F src/vdbe.h 70e862ac8a11b590f8c1eaac17a0078429d42bc4ea3f757a9af0f451dd966a71 @@ -2195,8 +2195,8 @@ F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee F tool/warnings.sh a554d13f6e5cf3760f041b87939e3d616ec6961859c3245e8ef701d1eafc2ca2 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f F tool/winmain.c 00c8fb88e365c9017db14c73d3c78af62194d9644feaf60e220ab0f411f3604c -P aea648f3668219a2539160eb68c77ddd06aa8eac235c1326fe7b1547288b11ee -R 87703e2fc96cf20793c6348cd259dd70 +P 6c3dc2430adef061b458b6d9159628bb8debfd4d63876d5d5e7493afb799d97f +R 0c1564cd8bd4be660fcfe28f92df6384 U drh -Z 772c7a9e66852f7312caf69be9a36c21 +Z c76030a82efd805d9e5d4b632bb41994 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 61b8616ec4..4f813be386 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -6c3dc2430adef061b458b6d9159628bb8debfd4d63876d5d5e7493afb799d97f +8c3a1b450d6d5778004b9fb0e92b4f3f90d3e06ff87276370d01a45b7d607d23 diff --git a/src/util.c b/src/util.c index 6838946ab6..69a7fbe1bb 100644 --- a/src/util.c +++ b/src/util.c @@ -764,6 +764,9 @@ static void sqlite3Fp2Convert10(u64 m, int e, int n, u64 *pD, int *pP){ /* ** Return an IEEE754 floating point value that approximates d*pow(10,p). +** +** The (current) algorithm is adapted from the work of Ross Cox at +** https://github.com/rsc/fpfmt */ static double sqlite3Fp10Convert2(u64 d, int p){ int b, lp, e, adj, s; @@ -815,14 +818,17 @@ static double sqlite3Fp10Convert2(u64 d, int p){ ** ** z[] must be UTF-8 and zero-terminated. ** -** Return TRUE if the result is a valid real number (or integer) and FALSE -** if the string is empty or contains extraneous text. More specifically -** return +** Return positive if the result is a valid real number (or integer) and +** zero or negative if the string is empty or contains extraneous text. +** More specifically: +** ** 1 => The input string is a pure integer ** 2 or more => The input has a decimal point or eNNN clause -** 0 or less => The input string is not a valid number -** -1 => Not a valid number, but has a valid prefix which -** includes a decimal point and/or an eNNN clause +** 0 or less => The input string is not well-formed +** -1 => The input is not well-formed, but it does begin +** with a well-formed floating-point literal (with +** a "." or a "eNNN" suffix or both) followed by +** other extraneous text. ** ** Valid numbers are in one of these formats: ** @@ -833,29 +839,38 @@ static double sqlite3Fp10Convert2(u64 d, int p){ ** Leading and trailing whitespace is ignored for the purpose of determining ** validity. ** -** If some prefix of the input string is a valid number, this routine -** returns FALSE but it still converts the prefix and writes the result -** into *pResult. +** Algorithm sketch: Compute an unsigned 64-bit integer s and a base-10 +** exponent d such that the value encoding by the input is s*pow(10,d). +** Then invoke sqlite3Fp10Convert2() to calculated the closest possible +** IEEE754 double. The sign is added back afterwards, if the input string +** starts with a "-". The use of an unsigned 64-bit s mantissa means that +** only about the first 19 significant digits of the input can contribute +** to the result. This can result in suboptimal rounding decisions when +** correct rounding requires more than 19 input digits. For example, +** this routine renders "3500000000000000.2500001" as +** 3500000000000000.0 instead of 3500000000000000.5 because the decision +** to round up instead of using banker's rounding to round down is determined +** by the 23rd significant digit, which this routine ignores. It is not +** possible to do better without some kind of BigNum. */ -#if defined(_MSC_VER) -#pragma warning(disable : 4756) -#endif -int sqlite3AtoF(const char *z, double *pResult){ +int sqlite3AtoF(const char *zIn, double *pResult){ #ifndef SQLITE_OMIT_FLOATING_POINT + const unsigned char *z = (const unsigned char*)zIn; int neg = 0; /* True for a negative value */ u64 s = 0; /* mantissa */ int d = 0; /* Value is s * pow(10,d) */ int seenDigit = 0; /* true if any digits seen */ int seenFP = 0; /* True if we've seen a "." or a "e" */ + unsigned v; /* Value of a single digit */ start_of_text: - if( sqlite3Isdigit(z[0]) ){ + if( (v = (unsigned)z[0] - '0')<10 ){ parse_integer_part: seenDigit = 1; - s = z[0] - '0'; + s = v; z++; - while( sqlite3Isdigit(z[0]) ){ - s = s*10 + (z[0] - '0'); + while( (v = (unsigned)z[0] - '0')<10 ){ + s = s*10 + v; z++; if( s>=(LARGEST_INT64-9)/10 ){ while( sqlite3Isdigit(z[0]) ){ z++; d++; } @@ -865,10 +880,10 @@ int sqlite3AtoF(const char *z, double *pResult){ }else if( z[0]=='-' ){ neg = 1; z++; - if( sqlite3Isdigit(z[0]) ) goto parse_integer_part; + if( (v = (unsigned)z[0] - '0')<10 ) goto parse_integer_part; }else if( z[0]=='+' ){ z++; - if( sqlite3Isdigit(z[0]) ) goto parse_integer_part; + if( (v = (unsigned)z[0] - '0')<10 ) goto parse_integer_part; }else if( sqlite3Isspace(z[0]) ){ do{ z++; }while( sqlite3Isspace(z[0]) ); goto start_of_text; @@ -913,12 +928,12 @@ int sqlite3AtoF(const char *z, double *pResult){ } } /* copy digits to exponent */ - if( sqlite3Isdigit(*z) ){ - int exp = *z - '0'; + if( (v = (unsigned)z[0] - '0')<10 ){ + int exp = v; z++; seenFP = 1; - while( sqlite3Isdigit(*z) ){ - exp = exp<10000 ? (exp*10 + (*z - '0')) : 10000; + while( (v = (unsigned)z[0] - '0')<10 ){ + exp = exp<10000 ? (exp*10 + v) : 10000; z++; } d += esign*exp;