Re: Insertion Sort Improvements - Mailing list pgsql-hackers

From Benjamin Coutu
Subject Re: Insertion Sort Improvements
Date
Msg-id ac56d6b6bb4d3a6f65b5@zeyos.com
Whole thread Raw
In response to Re: Insertion Sort Improvements  (John Naylor <john.naylor@enterprisedb.com>)
Responses Re: Insertion Sort Improvements
List pgsql-hackers
> convenient if you know the type at compile time. See the attached,
> which I had laying around when I was looking at PDQuicksort. I believe
> it's similar to what you have in mind.

That looks very promising.
I also love your recent proposal of partitioning into null and non-null. I suspect that to be a clear winner.

> I think it belongs around 10 now anyway.

Yeah, I think that change is overdue given modern hardware characteristics (even with the current implementation).

Another idea could be to run a binary insertion sort and use a much higher threshold. This could significantly cut down
oncomparisons (especially with presorted runs, which are quite common in real workloads). 

If full binary search turned out to be an issue regarding cache locality, we could do it in smaller chunks, e.g. do a
microbinary search between the current position (I) and position minus chunk size (say 6-12 or so, whatever fits 1 or 2
cachelines)whenever A[I] < A[I-1] and if we don't find the position within that chunk we continue with the previous
chunk,thereby preserving cache locality. 

With less comparisons we should start keeping track of swaps and use that as an efficient way to determine
presortedness.We could change the insertion sort threshold to a swap threshold and do insertion sort until we hit the
swapthreshold. I suspect that would make the current presorted check obsolete (as binary insertion sort without or even
witha few swaps should be faster than the current presorted-check). 

Cheers, Ben



pgsql-hackers by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: standby promotion can create unreadable WAL
Next
From: Robert Haas
Date:
Subject: Re: has_privs_of_role vs. is_member_of_role, redux