mirror of
https://github.com/sqlite/sqlite.git
synced 2025-11-11 01:42:22 +03:00
Further performance enhancements to JSON parsing and rendering. Total
performance gain for large JSONs so far on this branch is about 11%. FossilOrigin-Name: adb4d6b007cbe9d7c9670f5fc196443ebe0f3a89df1f3290ba6247fcf83fe5bd
This commit is contained in:
12
manifest
12
manifest
@@ -1,5 +1,5 @@
|
|||||||
C Performance\soptimization\sfor\sparsing\slarge\sJSONs\sthat\scontain\slots\sof\stext.
|
C Further\sperformance\senhancements\sto\sJSON\sparsing\sand\srendering.\s\sTotal\nperformance\sgain\sfor\slarge\sJSONs\sso\sfar\son\sthis\sbranch\sis\sabout\s11%.
|
||||||
D 2023-07-19T13:50:31.934
|
D 2023-07-19T15:06:29.535
|
||||||
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
|
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
|
||||||
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
|
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
|
||||||
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
|
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
|
||||||
@@ -597,7 +597,7 @@ F src/hash.h 3340ab6e1d13e725571d7cee6d3e3135f0779a7d8e76a9ce0a85971fa3953c51
|
|||||||
F src/hwtime.h f9c2dfb84dce7acf95ce6d289e46f5f9d3d1afd328e53da8f8e9008e3b3caae6
|
F src/hwtime.h f9c2dfb84dce7acf95ce6d289e46f5f9d3d1afd328e53da8f8e9008e3b3caae6
|
||||||
F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71
|
F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71
|
||||||
F src/insert.c 3f0a94082d978bbdd33c38fefea15346c6c6bffb70bc645a71dc0f1f87dd3276
|
F src/insert.c 3f0a94082d978bbdd33c38fefea15346c6c6bffb70bc645a71dc0f1f87dd3276
|
||||||
F src/json.c a1e70adf94131e29cdaabc19523a92b2df72ef6305b80d5493369e278267dfe6
|
F src/json.c 4a875e61ce6aa869bd6028836f198e7ab08d3efceb8ab8d0529bec8061b67e94
|
||||||
F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
|
F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
|
||||||
F src/loadext.c 176d6b2cb18a6ad73b133db17f6fc351c4d9a2d510deebdb76c22bde9cfd1465
|
F src/loadext.c 176d6b2cb18a6ad73b133db17f6fc351c4d9a2d510deebdb76c22bde9cfd1465
|
||||||
F src/main.c 512b1d45bc556edf4471a845afb7ba79e64bd5b832ab222dc195c469534cd002
|
F src/main.c 512b1d45bc556edf4471a845afb7ba79e64bd5b832ab222dc195c469534cd002
|
||||||
@@ -2043,8 +2043,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
|
|||||||
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
|
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
|
||||||
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
|
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
|
||||||
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
|
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
|
||||||
P a0d3e7571aded8d1e03908059d2d5aa5d62ec49bff099cb38f6f35df5e4b18b5
|
P c9fbe0185cd5d64950724b00cd0bfb3a7939a985040465a0f35f445acb6e94a6
|
||||||
R 35635814f4d35d8abae453f3c0b6b67f
|
R dbdd171b9775d82e33a4a9295a6ddced
|
||||||
U drh
|
U drh
|
||||||
Z a4b178ad01a69bfb9f8e963eafa3e384
|
Z 04400ecbf8a4a25997341f5b2d7f0282
|
||||||
# Remove this line to create a well-formed Fossil manifest.
|
# Remove this line to create a well-formed Fossil manifest.
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
c9fbe0185cd5d64950724b00cd0bfb3a7939a985040465a0f35f445acb6e94a6
|
adb4d6b007cbe9d7c9670f5fc196443ebe0f3a89df1f3290ba6247fcf83fe5bd
|
||||||
94
src/json.c
94
src/json.c
@@ -218,12 +218,35 @@ static int jsonGrow(JsonString *p, u32 N){
|
|||||||
|
|
||||||
/* Append N bytes from zIn onto the end of the JsonString string.
|
/* Append N bytes from zIn onto the end of the JsonString string.
|
||||||
*/
|
*/
|
||||||
static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
|
static SQLITE_NOINLINE void jsonAppendExpand(
|
||||||
if( N==0 ) return;
|
JsonString *p,
|
||||||
if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
|
const char *zIn,
|
||||||
|
u32 N
|
||||||
|
){
|
||||||
|
assert( N>0 );
|
||||||
|
if( jsonGrow(p,N) ) return;
|
||||||
memcpy(p->zBuf+p->nUsed, zIn, N);
|
memcpy(p->zBuf+p->nUsed, zIn, N);
|
||||||
p->nUsed += 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.
|
/* 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
|
/* Append a single character
|
||||||
*/
|
*/
|
||||||
static void jsonAppendChar(JsonString *p, char c){
|
static SQLITE_NOINLINE void jsonAppendCharExpand(JsonString *p, char c){
|
||||||
if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
|
if( jsonGrow(p,1) ) return;
|
||||||
p->zBuf[p->nUsed++] = c;
|
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
|
/* Append a comma separator to the output buffer, if the previous
|
||||||
** character is not '[' or '{'.
|
** character is not '[' or '{'.
|
||||||
@@ -250,7 +280,8 @@ static void jsonAppendSeparator(JsonString *p){
|
|||||||
char c;
|
char c;
|
||||||
if( p->nUsed==0 ) return;
|
if( p->nUsed==0 ) return;
|
||||||
c = p->zBuf[p->nUsed-1];
|
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
|
/* 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 ){
|
while( N>0 ){
|
||||||
for(i=0; i<N && zIn[i]!='\\'; i++){}
|
for(i=0; i<N && zIn[i]!='\\'; i++){}
|
||||||
if( i>0 ){
|
if( i>0 ){
|
||||||
jsonAppendRaw(p, zIn, i);
|
jsonAppendRawNZ(p, zIn, i);
|
||||||
zIn += i;
|
zIn += i;
|
||||||
N -= i;
|
N -= i;
|
||||||
if( N==0 ) break;
|
if( N==0 ) break;
|
||||||
@@ -321,16 +352,16 @@ static void jsonAppendNormalizedString(JsonString *p, const char *zIn, u32 N){
|
|||||||
jsonAppendChar(p, '\'');
|
jsonAppendChar(p, '\'');
|
||||||
break;
|
break;
|
||||||
case 'v':
|
case 'v':
|
||||||
jsonAppendRaw(p, "\\u0009", 6);
|
jsonAppendRawNZ(p, "\\u0009", 6);
|
||||||
break;
|
break;
|
||||||
case 'x':
|
case 'x':
|
||||||
jsonAppendRaw(p, "\\u00", 4);
|
jsonAppendRawNZ(p, "\\u00", 4);
|
||||||
jsonAppendRaw(p, &zIn[2], 2);
|
jsonAppendRawNZ(p, &zIn[2], 2);
|
||||||
zIn += 2;
|
zIn += 2;
|
||||||
N -= 2;
|
N -= 2;
|
||||||
break;
|
break;
|
||||||
case '0':
|
case '0':
|
||||||
jsonAppendRaw(p, "\\u0000", 6);
|
jsonAppendRawNZ(p, "\\u0000", 6);
|
||||||
break;
|
break;
|
||||||
case '\r':
|
case '\r':
|
||||||
if( zIn[2]=='\n' ){
|
if( zIn[2]=='\n' ){
|
||||||
@@ -348,7 +379,7 @@ static void jsonAppendNormalizedString(JsonString *p, const char *zIn, u32 N){
|
|||||||
N -= 2;
|
N -= 2;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
jsonAppendRaw(p, zIn, 2);
|
jsonAppendRawNZ(p, zIn, 2);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
zIn += 2;
|
zIn += 2;
|
||||||
@@ -378,11 +409,12 @@ static void jsonAppendNormalizedInt(JsonString *p, const char *zIn, u32 N){
|
|||||||
jsonPrintf(100,p,"%lld",i);
|
jsonPrintf(100,p,"%lld",i);
|
||||||
}else{
|
}else{
|
||||||
assert( rc==2 );
|
assert( rc==2 );
|
||||||
jsonAppendRaw(p, "9.0e999", 7);
|
jsonAppendRawNZ(p, "9.0e999", 7);
|
||||||
}
|
}
|
||||||
return;
|
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 ){
|
if( N>0 ){
|
||||||
jsonAppendRaw(p, zIn, N);
|
jsonAppendRawNZ(p, zIn, N);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -430,7 +462,7 @@ static void jsonAppendValue(
|
|||||||
){
|
){
|
||||||
switch( sqlite3_value_type(pValue) ){
|
switch( sqlite3_value_type(pValue) ){
|
||||||
case SQLITE_NULL: {
|
case SQLITE_NULL: {
|
||||||
jsonAppendRaw(p, "null", 4);
|
jsonAppendRawNZ(p, "null", 4);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SQLITE_FLOAT: {
|
case SQLITE_FLOAT: {
|
||||||
@@ -538,15 +570,15 @@ static void jsonRenderNode(
|
|||||||
switch( pNode->eType ){
|
switch( pNode->eType ){
|
||||||
default: {
|
default: {
|
||||||
assert( pNode->eType==JSON_NULL );
|
assert( pNode->eType==JSON_NULL );
|
||||||
jsonAppendRaw(pOut, "null", 4);
|
jsonAppendRawNZ(pOut, "null", 4);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case JSON_TRUE: {
|
case JSON_TRUE: {
|
||||||
jsonAppendRaw(pOut, "true", 4);
|
jsonAppendRawNZ(pOut, "true", 4);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case JSON_FALSE: {
|
case JSON_FALSE: {
|
||||||
jsonAppendRaw(pOut, "false", 5);
|
jsonAppendRawNZ(pOut, "false", 5);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case JSON_STRING: {
|
case JSON_STRING: {
|
||||||
@@ -562,7 +594,8 @@ static void jsonRenderNode(
|
|||||||
}else if( pNode->jnFlags & JNODE_JSON5 ){
|
}else if( pNode->jnFlags & JNODE_JSON5 ){
|
||||||
jsonAppendNormalizedString(pOut, pNode->u.zJContent, pNode->n);
|
jsonAppendNormalizedString(pOut, pNode->u.zJContent, pNode->n);
|
||||||
}else{
|
}else{
|
||||||
jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
|
assert( pNode->n>0 );
|
||||||
|
jsonAppendRawNZ(pOut, pNode->u.zJContent, pNode->n);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -571,7 +604,8 @@ static void jsonRenderNode(
|
|||||||
if( pNode->jnFlags & JNODE_JSON5 ){
|
if( pNode->jnFlags & JNODE_JSON5 ){
|
||||||
jsonAppendNormalizedReal(pOut, pNode->u.zJContent, pNode->n);
|
jsonAppendNormalizedReal(pOut, pNode->u.zJContent, pNode->n);
|
||||||
}else{
|
}else{
|
||||||
jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
|
assert( pNode->n>0 );
|
||||||
|
jsonAppendRawNZ(pOut, pNode->u.zJContent, pNode->n);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -580,7 +614,8 @@ static void jsonRenderNode(
|
|||||||
if( pNode->jnFlags & JNODE_JSON5 ){
|
if( pNode->jnFlags & JNODE_JSON5 ){
|
||||||
jsonAppendNormalizedInt(pOut, pNode->u.zJContent, pNode->n);
|
jsonAppendNormalizedInt(pOut, pNode->u.zJContent, pNode->n);
|
||||||
}else{
|
}else{
|
||||||
jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
|
assert( pNode->n>0 );
|
||||||
|
jsonAppendRawNZ(pOut, pNode->u.zJContent, pNode->n);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -882,7 +917,8 @@ static int jsonParseAddNode(
|
|||||||
const char *zContent /* Content */
|
const char *zContent /* Content */
|
||||||
){
|
){
|
||||||
JsonNode *p;
|
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);
|
return jsonParseAddNodeExpand(pParse, eType, n, zContent);
|
||||||
}
|
}
|
||||||
p = &pParse->aNode[pParse->nNode];
|
p = &pParse->aNode[pParse->nNode];
|
||||||
@@ -2002,12 +2038,12 @@ static void jsonParseFunc(
|
|||||||
assert( x.aNode[i].eU==0 || x.aNode[i].eU==1 );
|
assert( x.aNode[i].eU==0 || x.aNode[i].eU==1 );
|
||||||
if( x.aNode[i].u.zJContent!=0 ){
|
if( x.aNode[i].u.zJContent!=0 ){
|
||||||
assert( x.aNode[i].eU==1 );
|
assert( x.aNode[i].eU==1 );
|
||||||
jsonAppendRaw(&s, " ", 1);
|
jsonAppendChar(&s, ' ');
|
||||||
jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
|
jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
|
||||||
}else{
|
}else{
|
||||||
assert( x.aNode[i].eU==0 );
|
assert( x.aNode[i].eU==0 );
|
||||||
}
|
}
|
||||||
jsonAppendRaw(&s, "\n", 1);
|
jsonAppendChar(&s, '\n');
|
||||||
}
|
}
|
||||||
jsonParseReset(&x);
|
jsonParseReset(&x);
|
||||||
jsonResult(&s);
|
jsonResult(&s);
|
||||||
@@ -2174,11 +2210,11 @@ static void jsonExtractFunc(
|
|||||||
*/
|
*/
|
||||||
jsonInit(&jx, ctx);
|
jsonInit(&jx, ctx);
|
||||||
if( sqlite3Isdigit(zPath[0]) ){
|
if( sqlite3Isdigit(zPath[0]) ){
|
||||||
jsonAppendRaw(&jx, "$[", 2);
|
jsonAppendRawNZ(&jx, "$[", 2);
|
||||||
jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
|
jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
|
||||||
jsonAppendRaw(&jx, "]", 2);
|
jsonAppendRawNZ(&jx, "]", 2);
|
||||||
}else{
|
}else{
|
||||||
jsonAppendRaw(&jx, "$.", 1 + (zPath[0]!='['));
|
jsonAppendRawNZ(&jx, "$.", 1 + (zPath[0]!='['));
|
||||||
jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
|
jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
|
||||||
jsonAppendChar(&jx, 0);
|
jsonAppendChar(&jx, 0);
|
||||||
}
|
}
|
||||||
@@ -2213,7 +2249,7 @@ static void jsonExtractFunc(
|
|||||||
if( pNode ){
|
if( pNode ){
|
||||||
jsonRenderNode(pNode, &jx, 0);
|
jsonRenderNode(pNode, &jx, 0);
|
||||||
}else{
|
}else{
|
||||||
jsonAppendRaw(&jx, "null", 4);
|
jsonAppendRawNZ(&jx, "null", 4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if( i==argc ){
|
if( i==argc ){
|
||||||
|
|||||||
Reference in New Issue
Block a user