public inbox for [email protected]  
help / color / mirror / Atom feed
From: Chao Li <[email protected]>
To: Tom Lane <[email protected]>
Cc: jian he <[email protected]>
Cc: [email protected]
Subject: Re: Making jsonb_agg() faster
Date: Wed, 27 Aug 2025 09:22:17 +0800
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>
	<CACJufxHuE-P0s1PxiRja26ZMjKUZiCAgWa1jedswo94SFzjXMg@mail.gmail.com>
	<[email protected]>
	<CACJufxGHLYtvOAB7uA59C9Lz+rDPta5Egiii-B5opsZbw6eDDQ@mail.gmail.com>
	<[email protected]>
	<[email protected]>
	<[email protected]>


>> On Aug 23, 2025, at 03:11, Tom Lane <[email protected]> wrote:
>> 
>> 
>> v2-0001 takes care of that, and also adopts your suggestion in [1]
>> about not using two calls of pushJsonbValueScalar where one would do.
>> I also did a bit more micro-optimization in appendKey, appendValue,
>> appendElement to avoid redundant copying, because perf testing showed
>> that appendElement is still a hot-spot for jsonb_agg.  Patches 0002
>> and 0003 are unchanged.
>> 
> 
> 

A few more suggestions for pushJsonValue():

+       /* If an object or array is pushed, recursively push its contents */
+       if (jbval->type == jbvObject)
        {
                pushJsonbValue(pstate, WJB_BEGIN_OBJECT, NULL);
                for (i = 0; i < jbval->val.object.nPairs; i++)
@@ -581,32 +607,29 @@ pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq,
                        pushJsonbValue(pstate, WJB_KEY, &jbval->val.object.pairs[i].key);
                        pushJsonbValue(pstate, WJB_VALUE, &jbval->val.object.pairs[i].value);
                }
-
-               return pushJsonbValue(pstate, WJB_END_OBJECT, NULL);
+               pushJsonbValue(pstate, WJB_END_OBJECT, NULL);
+               return;
        }

To push WJB_BEGIN_OBJECT and WJB_END_OBJECT, we can directly call pushJsonValueScalar(), because once entering pushJsonbValue, they will meet the check of  (seq != WJB_ELEM && seq != WJB_VALUE). Directly calling pushJsonValueScalar() will saves one level of recursion.

-       if (jbval && (seq == WJB_ELEM || seq == WJB_VALUE) && jbval->type == jbvArray)
+       if (jbval->type == jbvArray)
        {
                pushJsonbValue(pstate, WJB_BEGIN_ARRAY, NULL);
                for (i = 0; i < jbval->val.array.nElems; i++)
                {
                        pushJsonbValue(pstate, WJB_ELEM, &jbval->val.array.elems[i]);
                }
-
-               return pushJsonbValue(pstate, WJB_END_ARRAY, NULL);
+               pushJsonbValue(pstate, WJB_END_ARRAY, NULL);
+               return;
        }

Same thing for pushing WJB_BEGIN_ARRAY and WJB_END_ARRAY.

And for pushJsonbValueScalar():

-                               (*pstate)->size = 4;
+                               ppstate->size = 4;      /* initial guess at array size */

Can we do lazy allocation? Initially assume size = 0, only allocate memory when pushing the first element? This way, we won’t allocate memory for empty objects and arrays. 


Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/






view thread (25+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected], [email protected]
  Subject: Re: Making jsonb_agg() faster
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox