Do generics in Delphi cause performance bottlenecks?
Asked Answered
S

1

9

Recently i have been developing an application and wanted to have a collections of several types. I don't want to declare and implement a new collection class for it's type. So, i thought of going with generics, but wasn't sure about the performance of Generics compared to normal typed instances. Performance is the major thing that i am looking at. My application is time critical and even loosing few 100 milliseconds is also not advisable.

I am using Delphi XE3

For eg:

ICollectionItem = interface
  function GetID : string;
  property ID : string read GetId;
end;

TGenericCollection<T: ICollectionItem> = class
  function Add(T) : Integer;
end;

compared to

TSomeClass = class(TInterfacedObject, ICollectionItem)
  function GetId : string;
end;

TSomeClassList = class
  function Add(item : TSomeClass) : Integer;
end;
Sympathize answered 12/2, 2013 at 7:24 Comment(5)
@LievenKeersmaekers I don't have XE3 either, so I didn't answer, but in earlier versions, generics aren't optimised very well at all. They seem to get compiled to some sort of pseudo-assembly that gets patched up when the generic gets instantiated, and as a result, optimisations that could otherwise be used (even as simple as evaluating SizeOf(T) + 1 at compile time) aren't.Granados
See "Using Generic containers in Delphi XE - always?". Better performance since optimization can do a better job. See also "Overview of Generics". At instantiation there might be a performance penalty.Gelatin
Did you verify that your collection code is your application's bottleneck? Also, TSomeClassList.Add is wrong in the Q. Parameter needs to be ICollectionItem. Is that just a typo? If you want to compare performance, what's stopping you. Trying to pre-judge it only gets you so far. Write two versions of the code and time them both.Tristis
I don't think there is any noticeable difference in speed but there is in size. Currently compiler generates code for each concrete type, even if some types could share the implementation.Gosse
Agree with hvd. In theory generics should be the same or better - in practice the compiler isn't good enough + often produces much slower code when using generics. eg. it won't inline functions that use generics (in XE2 anyway).Tridimensional
F
7

No performance bottleneck

Delphi generics are compiled. The compiler knows the concrete types at compile time and it's going to do it's best to provide you with the best code it can. There should be no differences between generic and non-generic code when inspecting the generated code at run time.

There's a good chance to get better code with generics, because you're more likely to use ready-made, efficient, type-safe data structures. When rolling your own you're likely to cut corners because lets be honest, writing sorting algorithms, efficient allocation, etc is hard.

Fulkerson answered 12/2, 2013 at 7:42 Comment(4)
@DavidHeffernan, just read Barry's blog all the way back to 2007. No mention of how the two-stage compilation works. It makes some sense as the units declaring generic types need to be compiled to DCU and that can't possibly contain final code, but I found nothing on it. If you have a link, please share.Fulkerson
I found this article which touches on the subject: blogs.teamb.com/craigstuntz/2009/10/01/38465 If you think about it, it has to be a two stage process. For example, when you compile against the DCU file containing TList<T>, that no longer has the source code for TList<T>.Tristis
@DavidHeffernan, thanks for the link. "Touches" indeed - I wish I had more information.Fulkerson
@DavidHeffernan Thanks for the informative link..I would test the performance with the generics and typed lists..Sympathize

© 2022 - 2024 — McMap. All rights reserved.