1
0
mirror of https://github.com/sqlite/sqlite.git synced 2026-01-06 08:01:16 +03:00

Performance enhancements to the JSON parser and code generator.

FossilOrigin-Name: 0a745897d66e9f36ea6e787fad54f0c413d52c226dc96fad36ccd33a0aa1930e
This commit is contained in:
drh
2023-07-19 23:02:45 +00:00
3 changed files with 100 additions and 47 deletions

View File

@@ -1,5 +1,5 @@
C Revise\sthe\snew\sJSON\sparser\sperformance\stest\sto\smake\sthe\stest\srun\s25\stimes\nlonger,\sand\sthus\sprovide\sa\smore\srepeatable\snumber.
D 2023-07-19T20:23:34.703
C Performance\senhancements\sto\sthe\sJSON\sparser\sand\scode\sgenerator.
D 2023-07-19T23:02:45.962
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -597,7 +597,7 @@ F src/hash.h 3340ab6e1d13e725571d7cee6d3e3135f0779a7d8e76a9ce0a85971fa3953c51
F src/hwtime.h f9c2dfb84dce7acf95ce6d289e46f5f9d3d1afd328e53da8f8e9008e3b3caae6
F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71
F src/insert.c 3f0a94082d978bbdd33c38fefea15346c6c6bffb70bc645a71dc0f1f87dd3276
F src/json.c 14c474fb1249a46eb44e878e2361f36abfe686b134039b0d1883d93d61505b4a
F src/json.c e48136fce64e5004b1b8be76624cc9a4ac9ff6ae630a97df187c0ea4b9692d1b
F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
F src/loadext.c 176d6b2cb18a6ad73b133db17f6fc351c4d9a2d510deebdb76c22bde9cfd1465
F src/main.c 512b1d45bc556edf4471a845afb7ba79e64bd5b832ab222dc195c469534cd002
@@ -2043,8 +2043,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P c47056e158073ee746c233dd2729cf224ea9e38821b15aa71ebf97607e602281
R f95aec1d730e7f651d5bd3177562e93c
P bee9e403ae47103938aabb9a7a7e120dfa741b464875965e58788a51fa56a8fe 144c8ccf6e5bb2527dd98742f0d67e0a16c627e7c67f754ce8ed4c4fb5b8d8b6
R af54725f832c177294e9e511cdacd53d
U drh
Z 755b9a0ce058340b12e7687869554354
Z 1c679f829efc53f4f55993e30939aa14
# Remove this line to create a well-formed Fossil manifest.

View File

@@ -1 +1 @@
bee9e403ae47103938aabb9a7a7e120dfa741b464875965e58788a51fa56a8fe
0a745897d66e9f36ea6e787fad54f0c413d52c226dc96fad36ccd33a0aa1930e

View File

@@ -218,12 +218,35 @@ static int jsonGrow(JsonString *p, u32 N){
/* Append N bytes from zIn onto the end of the JsonString string.
*/
static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
if( N==0 ) return;
if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
static SQLITE_NOINLINE void jsonAppendExpand(
JsonString *p,
const char *zIn,
u32 N
){
assert( N>0 );
if( jsonGrow(p,N) ) return;
memcpy(p->zBuf+p->nUsed, zIn, N);
p->nUsed += N;
}
static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
if( N==0 ) return;
if( N+p->nUsed >= p->nAlloc ){
jsonAppendExpand(p,zIn,N);
}else{
memcpy(p->zBuf+p->nUsed, zIn, N);
p->nUsed += N;
}
}
static void jsonAppendRawNZ(JsonString *p, const char *zIn, u32 N){
assert( N>0 );
if( N+p->nUsed >= p->nAlloc ){
jsonAppendExpand(p,zIn,N);
}else{
memcpy(p->zBuf+p->nUsed, zIn, N);
p->nUsed += N;
}
}
/* Append formatted text (not to exceed N bytes) to the JsonString.
*/
@@ -238,10 +261,17 @@ static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
/* Append a single character
*/
static void jsonAppendChar(JsonString *p, char c){
if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
static SQLITE_NOINLINE void jsonAppendCharExpand(JsonString *p, char c){
if( jsonGrow(p,1) ) return;
p->zBuf[p->nUsed++] = c;
}
static void jsonAppendChar(JsonString *p, char c){
if( p->nUsed>=p->nAlloc ){
jsonAppendCharExpand(p,c);
}else{
p->zBuf[p->nUsed++] = c;
}
}
/* Append a comma separator to the output buffer, if the previous
** character is not '[' or '{'.
@@ -250,7 +280,8 @@ static void jsonAppendSeparator(JsonString *p){
char c;
if( p->nUsed==0 ) return;
c = p->zBuf[p->nUsed-1];
if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
if( c=='[' || c=='{' ) return;
jsonAppendChar(p, ',');
}
/* Append the N-byte string in zIn to the end of the JsonString string
@@ -310,7 +341,7 @@ static void jsonAppendNormalizedString(JsonString *p, const char *zIn, u32 N){
while( N>0 ){
for(i=0; i<N && zIn[i]!='\\'; i++){}
if( i>0 ){
jsonAppendRaw(p, zIn, i);
jsonAppendRawNZ(p, zIn, i);
zIn += i;
N -= i;
if( N==0 ) break;
@@ -321,16 +352,16 @@ static void jsonAppendNormalizedString(JsonString *p, const char *zIn, u32 N){
jsonAppendChar(p, '\'');
break;
case 'v':
jsonAppendRaw(p, "\\u0009", 6);
jsonAppendRawNZ(p, "\\u0009", 6);
break;
case 'x':
jsonAppendRaw(p, "\\u00", 4);
jsonAppendRaw(p, &zIn[2], 2);
jsonAppendRawNZ(p, "\\u00", 4);
jsonAppendRawNZ(p, &zIn[2], 2);
zIn += 2;
N -= 2;
break;
case '0':
jsonAppendRaw(p, "\\u0000", 6);
jsonAppendRawNZ(p, "\\u0000", 6);
break;
case '\r':
if( zIn[2]=='\n' ){
@@ -348,7 +379,7 @@ static void jsonAppendNormalizedString(JsonString *p, const char *zIn, u32 N){
N -= 2;
break;
default:
jsonAppendRaw(p, zIn, 2);
jsonAppendRawNZ(p, zIn, 2);
break;
}
zIn += 2;
@@ -378,11 +409,12 @@ static void jsonAppendNormalizedInt(JsonString *p, const char *zIn, u32 N){
jsonPrintf(100,p,"%lld",i);
}else{
assert( rc==2 );
jsonAppendRaw(p, "9.0e999", 7);
jsonAppendRawNZ(p, "9.0e999", 7);
}
return;
}
jsonAppendRaw(p, zIn, N);
assert( N>0 );
jsonAppendRawNZ(p, zIn, N);
}
/*
@@ -414,7 +446,7 @@ static void jsonAppendNormalizedReal(JsonString *p, const char *zIn, u32 N){
}
}
if( N>0 ){
jsonAppendRaw(p, zIn, N);
jsonAppendRawNZ(p, zIn, N);
}
}
@@ -430,7 +462,7 @@ static void jsonAppendValue(
){
switch( sqlite3_value_type(pValue) ){
case SQLITE_NULL: {
jsonAppendRaw(p, "null", 4);
jsonAppendRawNZ(p, "null", 4);
break;
}
case SQLITE_FLOAT: {
@@ -538,15 +570,15 @@ static void jsonRenderNode(
switch( pNode->eType ){
default: {
assert( pNode->eType==JSON_NULL );
jsonAppendRaw(pOut, "null", 4);
jsonAppendRawNZ(pOut, "null", 4);
break;
}
case JSON_TRUE: {
jsonAppendRaw(pOut, "true", 4);
jsonAppendRawNZ(pOut, "true", 4);
break;
}
case JSON_FALSE: {
jsonAppendRaw(pOut, "false", 5);
jsonAppendRawNZ(pOut, "false", 5);
break;
}
case JSON_STRING: {
@@ -562,7 +594,8 @@ static void jsonRenderNode(
}else if( pNode->jnFlags & JNODE_JSON5 ){
jsonAppendNormalizedString(pOut, pNode->u.zJContent, pNode->n);
}else{
jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
assert( pNode->n>0 );
jsonAppendRawNZ(pOut, pNode->u.zJContent, pNode->n);
}
break;
}
@@ -571,7 +604,8 @@ static void jsonRenderNode(
if( pNode->jnFlags & JNODE_JSON5 ){
jsonAppendNormalizedReal(pOut, pNode->u.zJContent, pNode->n);
}else{
jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
assert( pNode->n>0 );
jsonAppendRawNZ(pOut, pNode->u.zJContent, pNode->n);
}
break;
}
@@ -580,7 +614,8 @@ static void jsonRenderNode(
if( pNode->jnFlags & JNODE_JSON5 ){
jsonAppendNormalizedInt(pOut, pNode->u.zJContent, pNode->n);
}else{
jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
assert( pNode->n>0 );
jsonAppendRawNZ(pOut, pNode->u.zJContent, pNode->n);
}
break;
}
@@ -882,7 +917,8 @@ static int jsonParseAddNode(
const char *zContent /* Content */
){
JsonNode *p;
if( pParse->aNode==0 || pParse->nNode>=pParse->nAlloc ){
assert( pParse->aNode!=0 || pParse->nNode>=pParse->nAlloc );
if( pParse->nNode>=pParse->nAlloc ){
return jsonParseAddNodeExpand(pParse, eType, n, zContent);
}
p = &pParse->aNode[pParse->nNode];
@@ -1235,15 +1271,31 @@ json_parse_restart:
jnFlags = 0;
parse_string:
cDelim = z[i];
j = i+1;
for(;;){
for(j=i+1; 1; j++){
static const char aOk[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
if( aOk[(unsigned char)z[j]] ) continue;
c = z[j];
if( (c & ~0x1f)==0 ){
/* Control characters are not allowed in strings */
pParse->iErr = j;
return -1;
}
if( c=='\\' ){
if( c==cDelim ){
break;
}else if( c=='\\' ){
c = z[++j];
if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
|| c=='n' || c=='r' || c=='t'
@@ -1263,10 +1315,11 @@ json_parse_restart:
pParse->iErr = j;
return -1;
}
}else if( c==cDelim ){
break;
}else if( c<=0x1f ){
/* Control characters are not allowed in strings */
pParse->iErr = j;
return -1;
}
j++;
}
jsonParseAddNode(pParse, JSON_STRING | (jnFlags<<8), j+1-i, &z[i]);
return j+1;
@@ -2003,12 +2056,12 @@ static void jsonParseFunc(
assert( x.aNode[i].eU==0 || x.aNode[i].eU==1 );
if( x.aNode[i].u.zJContent!=0 ){
assert( x.aNode[i].eU==1 );
jsonAppendRaw(&s, " ", 1);
jsonAppendChar(&s, ' ');
jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
}else{
assert( x.aNode[i].eU==0 );
}
jsonAppendRaw(&s, "\n", 1);
jsonAppendChar(&s, '\n');
}
jsonParseReset(&x);
jsonResult(&s);
@@ -2175,11 +2228,11 @@ static void jsonExtractFunc(
*/
jsonInit(&jx, ctx);
if( sqlite3Isdigit(zPath[0]) ){
jsonAppendRaw(&jx, "$[", 2);
jsonAppendRawNZ(&jx, "$[", 2);
jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
jsonAppendRaw(&jx, "]", 2);
jsonAppendRawNZ(&jx, "]", 2);
}else{
jsonAppendRaw(&jx, "$.", 1 + (zPath[0]!='['));
jsonAppendRawNZ(&jx, "$.", 1 + (zPath[0]!='['));
jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
jsonAppendChar(&jx, 0);
}
@@ -2214,7 +2267,7 @@ static void jsonExtractFunc(
if( pNode ){
jsonRenderNode(pNode, &jx, 0);
}else{
jsonAppendRaw(&jx, "null", 4);
jsonAppendRawNZ(&jx, "null", 4);
}
}
if( i==argc ){