2

I'm doing some unit testing on an improved version of quicksort.
The (hopefully) faster version is implemented using:

TArrayHelper = class helper for System.Generics.Collections.TArray
....
   class procedure Sort<T>(var Values: array of T); overload; static;
   class procedure Sort<T>(var Values: array of T; Comparer: IComparer<T>); overload; static;
....

I know for now I can just rename the Sort into SortNew for the purpose of testing, but at some point I have to fix the method names to sort.

If there is a class helper in scope, how do I call the original methods?

Is there a way using rtti or some other roundabout method?

Johan
  • 74,508
  • 24
  • 191
  • 319
  • Class helper seems to *rule them all*. I don't see a point of using the same name for a method in a helper though. I'm taking helpers as a tool for extending, not for overriding. – TLama Jun 15 '15 at 12:00
  • Why do you not replace/implement the class procedure QuickSort directly in your helper? – LU RD Jun 15 '15 at 12:01
  • 1
    @TLama, the point is to foist faster `sort` methods on people without them knowing. It's part of a government plot. That way the NSA can spy on you without them having to rewrite your code. – Johan Jun 15 '15 at 12:04
  • Fair enough :-) P.S. the only way I've been thinking of failed (typecast the instance with the fully qualified name), hence I said that it seems they rule them all. – TLama Jun 15 '15 at 12:12
  • @LURD, Excellent Idea, but -I just remembered- I cannot, because the IComparer in my unit is not the same IComparer as in System.Generics.Collections. I'm bending the rules. – Johan Jun 15 '15 at 12:17
  • Speeding up the Quick Sort using generics? That I gotta see. If you manage to pull this off I might change my current thinking about Generics as being bad for performance. – SilverWarior Jun 15 '15 at 13:13
  • @SilverWarior He's not claiming that generics can be used to speed up quicksort. He's talking about a more efficient implementation of `TArray.Sort`. Generics generally has no performance implications. Why do you think otherwise? – David Heffernan Jun 15 '15 at 13:22
  • @SilverWarior, generic code is **not** slower than normal code. However the implementation that Emba uses does cause code bloat, which can cause issues with the cache. In order to combat this clever tricks have to be used. – Johan Jun 15 '15 at 14:45
  • @DavidHeffernan It is my existing experience so far with generics that are making me think about them in such way. – SilverWarior Jun 15 '15 at 15:28
  • @SilverWarior I suspect you are drawing erroneous conclusions – David Heffernan Jun 15 '15 at 16:12
  • @DavidHeffernan Perhaps. I assume you have probably done some performance testing of Generics yourself. Right? Well so far results of my tests were against the favor of Generics. But then again I might have used the generics in wrong way. – SilverWarior Jun 15 '15 at 19:59
  • Generics code is identical to non generic – David Heffernan Jun 15 '15 at 20:01

1 Answers1

2

I think that the only way to achieve this with pure Pascal code is to call Sort from a scope in which your class helper is not active. The point being that if your class helper is active, then Sort refers to the method in the helper.

For instance like this:

unit ScopeBuster;

interface;

uses
  System.Generics.Collections;

type
  TArrayScopeBuster = class
    class procedure Sort<T>(...); overload; inline; static;
  end;

implementation

class procedure TArrayScopeBuster.Sort<T>(...); 
begin
  TArray.Sort(...);
end;

end.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490