I am mainly interested in stable sorting large arrays of strings. With real-world data I succeed in beating Tarray.sort by a factor 10-100 (even though the latter is not stable). As an exercise I tried to repeat this for simple types. Works fine. And then to bundle it into a generic class as in Tarray. Consider my starting point:
procedure sortArray(var values : array of byte); overload;
var temp : array of byte;
function getSequence(from: integer) : integer;
procedure doThis(...);
procedure doThat(...);
begin
setLength(temp,high(values)+1);
// actual code calling one of the procedures
end;
procedure sortArray(var values : array of double); overload;
var temp : array of double;
... etc ...
All the local subroutines have access to values and temp.
It seemed to me like an ideal situation to try out how generics work. In a generic class you can't have local subroutines, so these are all changed into methods. I then have:
type
TsimpleArray<T> = class
private
class function getSequence(var values,temp : array of T; from: integer) : integer;
class procedure doThis(var values,temp : array of T; ...);
...
public
class procedure sort(var values : array of T);
end;
Class functions can not have access to global temp storage, so that has to be passed as a parameter to all these methods, as well as the source array. All comaprisons if v>values[k] and the like to be changed into if Tcomparer<T>.default.compare(v,values[k])>0.
Well, it works. But it is now around 10 times slower then my simple procedure approach. (Still a lot faster than Tarray.sort.) I guess this is the prize we pay for better maintainable generic code. Or are there things I could do better?