Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5729738

Browse files
author
Nikita Glukhov
committed
Remove JsonPathAnlStats.entries
1 parent e959727 commit 5729738

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

src/backend/utils/adt/jsonb_typanalyze.c

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,6 @@ typedef struct JsonPathAnlStats
156156
char *pathstr;
157157
double freq;
158158
int depth;
159-
160-
JsonPathEntry **entries;
161-
int nentries;
162159
} JsonPathAnlStats;
163160

164161
/* various bits needed while analyzing JSON */
@@ -273,8 +270,6 @@ jsonAnalyzeAddPath(JsonAnalyzeContext *ctx, JsonPathEntry *parent,
273270
stats->stats = NULL;
274271
stats->freq = 0.0;
275272
stats->depth = parent->depth + 1;
276-
stats->entries = NULL;
277-
stats->nentries = 0;
278273

279274
/* update maximal depth */
280275
if (ctx->maxdepth < stats->depth)
@@ -504,18 +499,19 @@ jsonAnalyzeCollectPaths(JsonAnalyzeContext *ctx, Jsonb *jb, void *param)
504499
*/
505500
static void
506501
jsonAnalyzeCollectSubpath(JsonAnalyzeContext *ctx, JsonPathAnlStats *pstats,
507-
JsonbValue *jbv, int n)
502+
JsonbValue *jbv, JsonPathEntry **entries,
503+
int start_entry)
508504
{
509505
JsonbValue scalar;
510506
int i;
511507

512-
for (i = n; i < pstats->depth; i++)
508+
for (i = start_entry; i < pstats->depth; i++)
513509
{
514-
JsonPathEntry *entry = pstats->entries[i];
510+
JsonPathEntry *entry = entries[i];
515511
JsonbContainer *jbc = jbv->val.binary.data;
516512
JsonbValueType type = jbv->type;
517513

518-
if (i > n)
514+
if (i > start_entry)
519515
pfree(jbv);
520516

521517
if (type != jbvBinary)
@@ -535,7 +531,7 @@ jsonAnalyzeCollectSubpath(JsonAnalyzeContext *ctx, JsonPathAnlStats *pstats,
535531
while ((r = JsonbIteratorNext(&it, &elem, true)) != WJB_DONE)
536532
{
537533
if (r == WJB_ELEM)
538-
jsonAnalyzeCollectSubpath(ctx, pstats, &elem, i + 1);
534+
jsonAnalyzeCollectSubpath(ctx, pstats, &elem, entries, i + 1);
539535
}
540536

541537
return;
@@ -553,14 +549,14 @@ jsonAnalyzeCollectSubpath(JsonAnalyzeContext *ctx, JsonPathAnlStats *pstats,
553549
}
554550
}
555551

556-
if (i == n &&
552+
if (i == start_entry &&
557553
jbv->type == jbvBinary &&
558554
JsonbExtractScalar(jbv->val.binary.data, &scalar))
559555
jbv = &scalar;
560556

561557
jsonAnalyzeJsonValue(ctx, &pstats->vstats, jbv);
562558

563-
if (i > n)
559+
if (i > start_entry)
564560
pfree(jbv);
565561
}
566562

@@ -571,25 +567,24 @@ jsonAnalyzeCollectSubpath(JsonAnalyzeContext *ctx, JsonPathAnlStats *pstats,
571567
static void
572568
jsonAnalyzeCollectPath(JsonAnalyzeContext *ctx, Jsonb *jb, void *param)
573569
{
574-
JsonPathAnlStats *pstats = (JsonPathAnlStats *) param;
575-
JsonbValue jbvtmp;
576-
JsonbValue *jbv = JsonValueInitBinary(&jbvtmp, jb);
570+
JsonPathAnlStats *pstats = (JsonPathAnlStats *) param;
571+
JsonbValue jbvtmp;
572+
JsonbValue *jbv = JsonValueInitBinary(&jbvtmp, jb);
577573
JsonPathEntry *path;
574+
JsonPathEntry **entries;
575+
int i;
578576

579-
if (!pstats->entries)
580-
{
581-
int i;
577+
entries = palloc(sizeof(*entries) * pstats->depth);
582578

583-
pstats->entries = MemoryContextAlloc(ctx->mcxt,
584-
sizeof(*pstats->entries) * pstats->depth);
579+
/* Build entry array in direct order */
580+
for (path = &pstats->path, i = pstats->depth - 1;
581+
path->parent && i >= 0;
582+
path = path->parent, i--)
583+
entries[i] = path;
585584

586-
for (path = &pstats->path, i = pstats->depth - 1;
587-
path->parent && i >= 0;
588-
path = path->parent, i--)
589-
pstats->entries[i] = path;
590-
}
585+
jsonAnalyzeCollectSubpath(ctx, pstats, jbv, entries, 0);
591586

592-
jsonAnalyzeCollectSubpath(ctx, pstats, jbv, 0);
587+
pfree(entries);
593588
}
594589

595590
static Datum

0 commit comments

Comments
 (0)