1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-09 14:21:03 +03:00

Reduce the number of memory allocations when parsing JSON.

FossilOrigin-Name: 9edd67162113df57dae21d4683f9495611e2cf4717c6d12f5b7b8e44156d5fe3
This commit is contained in:
drh
2023-07-26 23:22:32 +00:00
parent f7a164f345
commit e94e132994
3 changed files with 15 additions and 30 deletions

View File

@@ -1,5 +1,5 @@
C Minor\schanges\sto\smake\scoverage\stesting\seasier.
D 2023-07-26T21:53:09.569
C Reduce\sthe\snumber\sof\smemory\sallocations\swhen\sparsing\sJSON.
D 2023-07-26T23:22:32.813
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -598,7 +598,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 8c21f66368929975ed41e6f5aff141b5ff2706bc0f817b0a99dfea9cb6b1b7cd
F src/json.c bea467f5bb75311759f2a5f8a7bbf517384f6489bcbe85a41b7c39e8faeea698
F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
F src/loadext.c 176d6b2cb18a6ad73b133db17f6fc351c4d9a2d510deebdb76c22bde9cfd1465
F src/main.c 512b1d45bc556edf4471a845afb7ba79e64bd5b832ab222dc195c469534cd002
@@ -2044,8 +2044,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 04f497074b9210326030f36107a43d6490a2a59c8a574e2c5429cd9bde681bf7
R b26ff6d575d69d55cdae9f68086f2a13
P ec8b43382e5402e15d9f2dda3cf21ac8be8c1589ddbe6c9433c33eef0036f764
R 60fa67ed7b82b4d2c0db2475f139f2f9
U drh
Z a6ad53b5bf3028b66640cc0d3b3e396d
Z 0d5cf9ec935b3fe18206d9fca33e2d88
# Remove this line to create a well-formed Fossil manifest.

View File

@@ -1 +1 @@
ec8b43382e5402e15d9f2dda3cf21ac8be8c1589ddbe6c9433c33eef0036f764
9edd67162113df57dae21d4683f9495611e2cf4717c6d12f5b7b8e44156d5fe3

View File

@@ -144,8 +144,7 @@ struct JsonNode {
/* A parsed and possibly edited JSON string. Lifecycle:
**
** 1. JSON comes in and is parsed into an array aNode[]. The original
** JSON text is stored in zJson. This object may or may not be the
** owner of the input JSON - the bOwnsJson variables determines which.
** JSON text is stored in zJson.
**
** 2. Zero or more changes are made (via json_remove() or json_replace()
** or similar) to the aNode[] array.
@@ -176,7 +175,6 @@ struct JsonParse {
u8 nErr; /* Number of errors seen */
u8 oom; /* Set to true if out of memory */
u8 hasNonstd; /* True if input uses non-standard features like JSON5 */
u8 bOwnsJson; /* This object owns zJson and response for freeing it */
u8 useMod; /* Actually use the edits contain inside aNode */
u8 hasMod; /* aNode contains edits from the original zJson */
u32 nJPRef; /* Number of references to this object */
@@ -619,11 +617,6 @@ static void jsonParseReset(JsonParse *pParse){
sqlite3RCStrUnref(pParse->zAlt);
pParse->zAlt = 0;
}
if( pParse->bOwnsJson ){
pParse->bOwnsJson = 0;
sqlite3RCStrUnref(pParse->zJson);
pParse->zJson = 0;
}
}
/*
@@ -1759,14 +1752,12 @@ json_parse_restart:
static int jsonParse(
JsonParse *pParse, /* Initialize and fill this JsonParse object */
sqlite3_context *pCtx, /* Report errors here */
char *zJson, /* Input JSON text to be parsed */
int bTakeJson /* Assume ownership of zJson if true */
char *zJson /* Input JSON text to be parsed */
){
int i;
memset(pParse, 0, sizeof(*pParse));
if( zJson==0 ) return 1;
pParse->zJson = zJson;
pParse->bOwnsJson = bTakeJson;
pParse->nJPRef = 1;
i = jsonParseValue(pParse, 0);
if( pParse->oom ) i = -1;
@@ -1933,21 +1924,16 @@ static JsonParse *jsonParseCached(
/* The input JSON was not found anywhere in the cache. We will need
** to parse it ourselves and generate a new JsonParse object.
*/
p = sqlite3_malloc64( sizeof(*p) );
p = sqlite3_malloc64( sizeof(*p) + nJson + 1 );
if( p==0 ){
sqlite3_result_error_nomem(pCtx);
return 0;
}
memset(p, 0, sizeof(*p));
p->zJson = sqlite3RCStrNew( nJson );
if( p->zJson==0 ){
sqlite3_free(p);
sqlite3_result_error_nomem(pCtx);
return 0;
}
p->zJson = (char*)&p[1];
memcpy(p->zJson, zJson, nJson);
p->zJson[nJson] = 0;
if( jsonParse(p, pErrCtx, p->zJson, 1) ){
if( jsonParse(p, pErrCtx, p->zJson) ){
if( pErrCtx==0 ){
p->nErr = 1;
assert( p->nJPRef==1 ); /* Caller will own the new JsonParse object p */
@@ -2365,7 +2351,6 @@ static void jsonParseFunc(
printf("nErr = %u\n", p->nErr);
printf("oom = %u\n", p->oom);
printf("hasNonstd = %u\n", p->hasNonstd);
printf("bOwnsJson = %u\n", p->bOwnsJson);
printf("useMod = %u\n", p->useMod);
printf("hasMod = %u\n", p->hasMod);
printf("nJPRef = %u\n", p->nJPRef);
@@ -2677,8 +2662,8 @@ static void jsonPatchFunc(
JsonNode *pResult; /* The result of the merge */
UNUSED_PARAMETER(argc);
if( jsonParse(&x, ctx, (char*)sqlite3_value_text(argv[0]), 0) ) return;
if( jsonParse(&y, ctx, (char*)sqlite3_value_text(argv[1]), 0) ){
if( jsonParse(&x, ctx, (char*)sqlite3_value_text(argv[0])) ) return;
if( jsonParse(&y, ctx, (char*)sqlite3_value_text(argv[1])) ){
jsonParseReset(&x);
return;
}
@@ -3645,7 +3630,7 @@ static int jsonEachFilter(
p->zJson = sqlite3_malloc64( n+1 );
if( p->zJson==0 ) return SQLITE_NOMEM;
memcpy(p->zJson, z, (size_t)n+1);
if( jsonParse(&p->sParse, 0, p->zJson, 0) ){
if( jsonParse(&p->sParse, 0, p->zJson) ){
int rc = SQLITE_NOMEM;
if( p->sParse.oom==0 ){
sqlite3_free(cur->pVtab->zErrMsg);