I would like to change some routines in MySQL to get better performance.
It would be great if you could read the below and give me your feedback.
SHORT:
Today I'm looking for the optimal strncmp function to compare strings of 120 chars.
LONG
I'm looking for a the best sort algorythm for strings of size of about 120 chars.
WHY - A big part of MySQL involves sorting.
Often the database server fetches a number of records which need then to be sorted
The sorting function in MySQL is called "filesort".
Filesort it is a sort which normally operates on a memory buffer
but can operate with limited memory by storing sorted buffers to disk.
Filesort uses quicksort or radixsort depending on the number of rows to sort.
The majority of the sorts are done using the quicksort algorythm with a memory buffer. So my hope is to optimize this case.
I think that optimizing this common case will be worthwhile as e.g.:
the Sysbench database benchmark spents 20% of its time sorting arrays
of 100 element containing strings of 120 char length.
BUT HOW CAN WE OPTIMIZE THIS EASELY?
The mysql quicksort currently uses a scaler strncmp function which works on bytes.
I feel that this functions could be easely improved.
The function looks like this:
#define cmp(N) if (first[N] != last[N]) return (int) first[N] - (int) last[N]
static int ptr_compare_0(uint *compare_length,uchar **a, uchar **b)
{
reg3 int length= *compare_length;
reg1 uchar *first,*last;
first= *a; last= *b;
loop:
cmp(0);
cmp(1);
cmp(2);
cmp(3);
if ((length-=4))
{
first+=4;
last+=4;
goto loop;
}
return (0);
}
The main work loop runs over 4 bytes.
If the strings are not divideable by 4 then single cmp are done before the loop begins
Example:
static int ptr_compare_1(uint *compare_length,uchar **a, uchar **b)
{
reg3 int length= *compare_length-1;
reg1 uchar *first,*last;
first= *a+1; last= *b+1;
cmp(-1); /// COMPARE BEFORE THE LOOP
loop:
cmp(0);
cmp(1);
cmp(2);
cmp(3);
if ((length-=4))
{
first+=4;
last+=4;
goto loop;
}
return (0);
}
You can find the complete source in the mysql source archive in "/mysys/mf_sort.c" and "/mysys/ptr_cmp.c"
Working on single bytes is probably not the most effective solution.
I assume that running on longwords will be a lot faster.
At which strlength will the usage of altivec give an advantage?
Cheers
Gunnar