Re: jsonb access operators inefficiency

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Teodor Sigaev <teodor(at)sigaev(dot)ru>
Cc: Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: jsonb access operators inefficiency
Date: 2014-05-30 16:22:55
Message-ID: 5388B05F.8050203@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


On 05/30/2014 11:08 AM, Andrew Dunstan wrote:
>
> On 05/30/2014 09:35 AM, Teodor Sigaev wrote:
>> Hi!
>>
>> jsonb operators ->text, ->>text,->int, ->>int use inefficient methods
>> to access to needed field, proportional O(N/2). Attached patch
>> suggests for text operators O(log(N)) and for integer - O(1). The
>> fuctions with fast access already are existed in current code and are
>> used in contains operation, for example. Attached patch uses that
>> functions instead of sequentual loop over object/array.
>
> Teodor, thanks for this.
>
> My only worry is this sort of code, which in a quick search I didn't
> find used elsewhere
>
> - (void) JsonbToCString(jtext, &tjb->root , -1);
> - result = cstring_to_text_with_len(jtext->data,
> jtext->len);
> + appendStringInfoSpaces(jtext, VARHDRSZ);
> + (void) JsonbToCString(jtext, v->val.binary.data,
> -1);
> +
> + result = (text*)jtext->data;
> + SET_VARSIZE(result, jtext->len);
>
>
> If we're going to construct varlena objects inside a StringInfo, maybe
> we need a proper API for it. Isn't there a danger that data member of
> the StringInfo won't be properly aligned to allow us to do this? In
> any case, we should get most of the benefit of your patch without this
> "optimization".

I see that palloc.h says:

The result of palloc() is always word-aligned

so maybe my alignment fear is misplaced. So my remaining question is
whether this is OK stylistically.

cheers

andrew

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Teodor Sigaev 2014-05-30 16:27:48 Re: jsonb access operators inefficiency
Previous Message Marc Mamin 2014-05-30 16:21:16 Re: "pivot aggregation" with a patched intarray