Re: A qsort template
John Naylor <john.naylor@enterprisedb.com>
From: John Naylor <john.naylor@enterprisedb.com>
To: Peter Geoghegan <pg@bowt.ie>
Cc: Thomas Munro <thomas.munro@gmail.com>,
pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2022-02-01T02:37:27Z
Lists: pgsql-hackers
Attachments
- test-results-insert-threshold-20220131.txt (text/plain)
- jcn-0012-addendum-20220131.txt (text/plain)
I wrote: > 0010 - Thresholds on my TODO list. I did some basic tests on the insertion sort thresholds, and it looks like we could safely and profitably increase the current value from 7 to 20 or so, in line with other more recent implementations. I've attached an addendum on top of 0012 and the full test results on an Intel Coffee Lake machine with gcc 11.1. I found that the object test setup in 0012 had some kind of bug that was comparing the pointer of the object array. Rather than fix that, I decided to use Datums, but with the two extremes in comparator: simple branching with machine instructions vs. a SQL-callable function. The papers I've read indicate the results for Datum sizes would not be much different for small structs. The largest existing sort element is SortTuple, but that's only 24 bytes and has a bulky comparator as well. The first thing to note is that I rejected outright any testing of a "middle value" where the pivot is simply the middle of the array. Even the Bently and McIlroy paper which is the reference for our implementation says "The range that consists of the single integer 7 could be eliminated, but has been left adjustable because on some machines larger ranges are a few percent better". I tested thresholds up to 64, which is where I guessed results to get worse (most implementations are smaller than that). Here are the best thresholds at a quick glance: - elementary comparator: random: 16 or greater decreasing, rotate: get noticeably better all the way up to 64 organ: little difference, but seems to get better all the way up to 64 0/1: seems to get worse above 20 - SQL-callable comparator: random: between 12 and 20, but slight differences until 32 decreasing, rotate: get noticeably better all the way up to 64 organ: seems best at 12, but slight differences until 32 0/1: slight differences Based on these tests and this machine, it seems 20 is a good default value. I'll repeat this test on one older Intel and one non-Intel platform with older compilers. -- Running tally of patchset: 0001 - bsearch and unique is good to have, and we can keep the return type pending further tests -- if none happen this cycle, suggest committing this without the return type symbol. 0002/3 - I've yet to see a case where branchless comparators win, but other than that, these are good. Notational improvement and not performance sensitive. 0004/5 - Computing the arguments slows it down, but accessing the underlying int16s gives an improvement. [1] Haven't done an in-situ test on VACUUM. Could be worth it for pg15, since I imagine the proposals for dead tuple storage won't be ready this cycle. 0006 - I expect this to be slower too. I also wonder if this could also use the global function in 0004 once it's improved. 0007 - untested 0008 - Good performance in microbenchmarks, no in-situ testing. Inlined reversal is not worth the binary space or notational overhead. 0009 - Based on 0004, I would guess that computing the arguments is too slow. Not sure how to test in-situ to see if specializing helps. 0010 - Suggest leaving out the middle threshold and setting the insertion sort threshold to ~20. Might also name them ST_INSERTION_SORT_THRESHOLD and ST_NINTHER_THRESHOLD. (TODO: test on other platforms) 0011 - Committed. v3-0001 comparators for abbreviated keys - Clearly a win in this state already, especially for the "unsigned" case [2]. (gist untested) There are additional possible improvements mentioned, but they seem like a PG16 project(s). [1] https://www.postgresql.org/message-id/CA%2BhUKG%2BS5SMoG8Z2PHj0bsK70CxVLgqQR1orQJq6Cjgibu26vA%40mail.gmail.com [2] https://www.postgresql.org/message-id/CAFBsxsEFGAJ9eBpQVb5a86BE93WER3497zn2OT5wbjm1HHcqgA%40mail.gmail.com (TODO: refine test) -- John Naylor EDB: http://www.enterprisedb.com
Commits
-
Remove debug messages from tuplesort_sort_memtuples()
- 6e647ef0e750 15.0 landed
-
Fix performance regression in tuplesort specializations
- 99c754129d78 15.0 landed
-
Fix tuplesort optimization for CLUSTER-on-expression.
- cc58eecc5d75 15.0 landed
-
Initial pgindent and pgperltidy run for v14.
- def5b065ff22 14.0 cited
-
Specialize checkpointer sort functions.
- 1b88b8908e75 14.0 landed
-
Use sort_template.h for qsort() and qsort_arg().
- f374f4d66425 14.0 landed
-
Use sort_template.h for qsort_tuple() and qsort_ssup().
- 8eda3eba3063 14.0 landed
-
Add sort_template.h for making sort functions.
- 0a1f1d3cac6b 14.0 landed
-
Use abbreviated keys for faster sorting of text datums.
- 4ea51cdfe85c 9.5.0 cited