Re: Sort functions with specialized comparators

From: Stepan Neretin <sncfmgg(at)gmail(dot)com>
To: "Andrey M(dot) Borodin" <x4mmm(at)yandex-team(dot)ru>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Sort functions with specialized comparators
Date: 2024-06-11 06:32:04
Message-ID: CAN-sa+Dug7-WmT+8Gnoesk8j_pHpE_Ebm59zjOhTvQtiKusnyg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, Jun 8, 2024 at 1:50 AM Stepan Neretin <sncfmgg(at)gmail(dot)com> wrote:

> Hello all.
>
> I am interested in the proposed patch and would like to propose some
> additional changes that would complement it. My changes would introduce
> similar optimizations when working with a list of integers or object
> identifiers. Additionally, my patch includes an extension for benchmarking,
> which shows an average speedup of 30-40%.
>
> postgres=# SELECT bench_oid_sort(1000000);
> bench_oid_sort
>
>
> ----------------------------------------------------------------------------------------------------------------
> Time taken by list_sort: 116990848 ns, Time taken by list_oid_sort:
> 80446640 ns, Percentage difference: 31.24%
> (1 row)
>
> postgres=# SELECT bench_int_sort(1000000);
> bench_int_sort
>
>
> ----------------------------------------------------------------------------------------------------------------
> Time taken by list_sort: 118168506 ns, Time taken by list_int_sort:
> 80523373 ns, Percentage difference: 31.86%
> (1 row)
>
> What do you think about these changes?
>
> Best regards, Stepan Neretin.
>
> On Fri, Jun 7, 2024 at 11:08 PM Andrey M. Borodin <x4mmm(at)yandex-team(dot)ru>
> wrote:
>
>> Hi!
>>
>> In a thread about sorting comparators[0] Andres noted that we have
>> infrastructure to help compiler optimize sorting. PFA attached PoC
>> implementation. I've checked that it indeed works on the benchmark from
>> that thread.
>>
>> postgres=# CREATE TABLE arrays_to_sort AS
>> SELECT array_shuffle(a) arr
>> FROM
>> (SELECT ARRAY(SELECT generate_series(1, 1000000)) a),
>> generate_series(1, 10);
>>
>> postgres=# SELECT (sort(arr))[1] FROM arrays_to_sort; -- original
>> Time: 990.199 ms
>> postgres=# SELECT (sort(arr))[1] FROM arrays_to_sort; -- patched
>> Time: 696.156 ms
>>
>> The benefit seems to be on the order of magnitude with 30% speedup.
>>
>> There's plenty of sorting by TransactionId, BlockNumber, OffsetNumber,
>> Oid etc. But this sorting routines never show up in perf top or something
>> like that.
>>
>> Seems like in most cases we do not spend much time in sorting. But
>> specialization does not cost us much too, only some CPU cycles of a
>> compiler. I think we can further improve speedup by converting inline
>> comparator to value extractor: more compilers will see what is actually
>> going on. But I have no proofs for this reasoning.
>>
>> What do you think?
>>
>>
>> Best regards, Andrey Borodin.
>>
>> [0]
>> https://www.postgresql.org/message-id/flat/20240209184014.sobshkcsfjix6u4r%40awork3.anarazel.de#fc23df2cf314bef35095b632380b4a59
>
>
Hello all.

I have decided to explore more areas in which I can optimize and have added
two new benchmarks. Do you have any thoughts on this?

postgres=# select bench_int16_sort(1000000);
bench_int16_sort

-----------------------------------------------------------------------------------------------------------------
Time taken by usual sort: 66354981 ns, Time taken by optimized sort:
52151523 ns, Percentage difference: 21.41%
(1 row)

postgres=# select bench_float8_sort(1000000);
bench_float8_sort

------------------------------------------------------------------------------------------------------------------
Time taken by usual sort: 121475231 ns, Time taken by optimized sort:
74458545 ns, Percentage difference: 38.70%
(1 row)

postgres=#

Best regards, Stepan Neretin.

Attachment Content-Type Size
v42-0008-Refactor-sorting-of-attribute-numbers-in-pg_publ.patch text/x-patch 2.0 KB
v42-0009-Introduce-benchmarking-function-for-int16-array-.patch text/x-patch 4.2 KB
v42-0010-Implement-Sorting-Template-for-float8-Arrays.patch text/x-patch 1.0 KB
v42-0012-Add-benchmark-comparing-float8-sorting-methods.patch text/x-patch 4.0 KB
v42-0011-Optimize-box-quad-picksplit-with-float8-array-so.patch text/x-patch 1.2 KB
v42-0005-Enhanced-Sorting-Efficiency-for-Integer-Lists.patch text/x-patch 1.8 KB
v42-0007-Consolidate-and-optimize-int16-array-sorting.patch text/x-patch 4.1 KB
v42-0004-Optimized-Int-List-Sorting-by-using-template-sor.patch text/x-patch 2.4 KB
v42-0006-Implemented-benchmarking-for-optimized-sorting.patch text/x-patch 6.9 KB
v42-0003-Enhanced-Sorting-Efficiency-for-Oid-Lists.patch text/x-patch 2.6 KB
v42-0001-Use-specialized-sort-facilities.patch text/x-patch 5.7 KB
v42-0002-Optimized-Oid-List-Sorting-by-using-template-sor.patch text/x-patch 2.2 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Kyotaro Horiguchi 2024-06-11 06:35:23 Re: relfilenode statistics
Previous Message Jeff Davis 2024-06-11 06:27:30 Re: Improve the granularity of PQsocketPoll's timeout parameter?