In principle, using generics (as I suggested too) should not have a speed penalty at all.
In principle. In practice I find that debug sessions on my code regularly take me through the generics unit, which suggests that it /does/ interpose code in various places even when not used explicitly.
MarkMLl
I believe this is only true if you use one of the generic
libraries, NOT for completely self defined generic code as one can see from the generated assembler.
E.g. this code, that only uses the system unit:
{$mode delphi}
type
TIntArray = TArray<integer>;
TStringArray = TArray<string>;
procedure swap<T>(var left,right:T);inline;
var
temp:T;
begin
temp:=left;
left:=right;
right:=temp;
end;
procedure Reverse<T>(var Value:TArray<T>);inline;
var
i: Integer = 0;
begin
if Length(Value) > 0 then
for i := Low(Value) to High(Value) div 2 do
Swap<T>(Value[i],Value[High(Value) - i]);
end;
var
i:integer;
s:string;
si:TintArray = [1,2,3,4,5,6,7,8,9,10];
ss:TStringArray = ['there', 'are', 'more', 'things', 'in', 'heaven', 'and', 'earth',', ', 'Horatio'];
begin
reverse<integer>(si);
for i in si do write(i:2);
writeln;
reverse<string>(ss);
for s in ss do write(s:length(s)+1);
end.
The assembler shows that the "templates" are filled in *before* any assembler code is generated.
There is simply no reference to anything "generics" in the generated code apart from possibly internal namings.
There is simply no codepath related to generics at all.
I compiled this with -al -CX -XXs -O4
I also tested a non-generic version if Int reverse: Hey, near identical code!