Обсуждение: Optimize JsonbContainerTypeName by reordering type checks

Поиск
Список
Период
Сортировка

Optimize JsonbContainerTypeName by reordering type checks

От
Chao Li
Дата:
Hi Hacker,

While reading jsonb related code, I found JsonbContainerTypeName() can 
be optimized. The function currently checks for the less common scalar 
container type before checking for objects and arrays.

This patch 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.

I did the following test:

```

CREATE TABLE test_jsonb_types (
     id SERIAL PRIMARY KEY,
     data JSONB
);


INSERT INTO test_jsonb_types (data) VALUES
('{"name": "Alice", "age": 30}'),
('[1, 2, "three"]'),
('"hello world"'),
('12345'),
('true'),
('null');


evantest=# SELECT id, data, jsonb_typeof(data) AS data_type FROM 
test_jsonb_types;
  id |             data             | data_type
----+------------------------------+-----------
   1 | {"age": 30, "name": "Alice"} | object
   2 | [1, 2, "three"]              | array
   3 | "hello world"                | string
   4 | 12345                        | number
   5 | true                         | boolean
   6 | null                         | null
(6 rows)

```

Best regards,

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

Вложения

Re: Optimize JsonbContainerTypeName by reordering type checks

От
Chao Li
Дата:
Hi Hacker,

This is an innocent refactor that doesn't change logic and improve performance of jsonb_typeof() a little bit.

I have done a performance test that proves a tiny performance improvement:

With the master branch:
```
% pgbench -n --no-vacuum -T 120 -c 8 -j 4 -f test_jsonb_type.sql evantest
tps = 83290.213196 (without initial connection time)

# second run
tps = 83709.266846 (without initial connection time)

# third run
tps = 83531.014990 (without initial connection time)
```

With this patch:
```
% pgbench -n --no-vacuum -T 120 -c 8 -j 4 -f test_jsonb_type.sql evantest
tps = 84172.324715 (without initial connection time)

# second run
tps = 84082.619469 (without initial connection time)

# third run
tps = 84260.671778 (without initial connection time)
```

With 3 runs average tps, this patch makes ~0.8% improvement for jsonb_typeof().

My test script is attached, it can run against any database. And attached v2 patch file has a tiny change over v1.

Best regards,
Chao Li (Evan)
---------------------
HighGo Software Co., Ltd.
https://www.highgo.com/
Вложения