qsort, once again

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-hackers(at)postgreSQL(dot)org
Cc: Jerry Sievers <jerry(at)jerrysievers(dot)com>
Subject: qsort, once again
Date: 2006-03-16 19:37:55
Message-ID: 19164.1142537875@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I was just looking at the behavior of src/port/qsort.c on the test case
that Jerry Sievers was complaining about in pgsql-admin this morning.
I found out what the real weak spot is: it's got nothing directly to do
with good or bad pivots, it's this code right here:

if (swap_cnt == 0)
{ /* Switch to insertion sort */
for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
pl -= es)
swap(pl, pl - es);
return;
}

In other words, if qsort hits a subfile for which the chosen pivot is a
perfect pivot (no swaps are necessary), it switches to insertion sort.
Which is O(N^2). In Jerry's test case this happens on a subfile of
736357 elements, and you can say goodnight to that process ....

What I'm thinking is that we ought to have a limit on this, ie not
switch to insertion sort if n is larger than 1000 or so, ie

- if (swap_cnt == 0)
+ if (swap_cnt == 0 && n < 1000)

I'm wondering what the authors were expecting the insertion sort to
handle exactly. Does anyone have a copy of the paper that's referenced
in the code comment?

/*
* Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
*/

I tried looking for this at ACM but they seem not to have it.

regards, tom lane

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Alvaro Herrera 2006-03-16 19:41:52 Re: BETWEEN optimizer problems with single-value
Previous Message Simon Riggs 2006-03-16 19:37:07 Re: Separate BLCKSZ for data and logging