Skip to content

Commit b9ee2d6

Browse files
Chao Li (Evan)Commitfest Bot
authored andcommitted
jsonb: Optimize JsonbContainerTypeName by reordering type checks
The JsonbContainerTypeName() function currently checks for the less common scalar container type before checking for objects and arrays. This commit reorders the checks to prioritize the most common cases. The macros JsonContainerIsArray() and JsonContainerIsObject() are simple bit checks and are now evaluated first. This avoids the overhead of calling the JsonbExtractScalar() function in the vast majority of use cases. Author: Chao Li <[email protected]>
1 parent a95e3d8 commit b9ee2d6

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/backend/utils/adt/jsonb.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ JsonbContainerTypeName(JsonbContainer *jbc)
160160
{
161161
JsonbValue scalar;
162162

163-
if (JsonbExtractScalar(jbc, &scalar))
164-
return JsonbTypeName(&scalar);
165-
else if (JsonContainerIsArray(jbc))
166-
return "array";
167-
else if (JsonContainerIsObject(jbc))
163+
if (JsonContainerIsObject(jbc))
168164
return "object";
165+
else if (!JsonContainerIsScalar(jbc) && JsonContainerIsArray(jbc))
166+
return "array";
167+
else if (JsonbExtractScalar(jbc, &scalar))
168+
return JsonbTypeName(&scalar);
169169
else
170170
{
171171
elog(ERROR, "invalid jsonb container type: 0x%08x", jbc->header);

0 commit comments

Comments
 (0)