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;
Marjan Venema
  • 19,136
  • 6
  • 65
  • 79
Rahul W
  • 833
  • 11
  • 26
  • 1
    @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. –  Feb 12 '13 at 07:32
  • 1
    See [`"Using Generic containers in Delphi XE - always?"`](http://stackoverflow.com/q/5313756/576719). Better performance since optimization can do a better job. See also [`"Overview of Generics"`](http://docwiki.embarcadero.com/RADStudio/XE3/en/Overview_of_Generics). At instantiation there might be a performance penalty. – LU RD Feb 12 '13 at 07:40
  • 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. – David Heffernan Feb 12 '13 at 07:57
  • 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. – ain Feb 12 '13 at 09:13
  • 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). – Dan Bartlett Feb 12 '13 at 13:04

1 Answers1

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.

Community
  • 1
  • 1
Cosmin Prund
  • 25,498
  • 2
  • 60
  • 104
  • @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. – Cosmin Prund Feb 12 '13 at 09:35
  • 1
    I found this article which touches on the subject: http://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`, that no longer has the source code for `TList`. – David Heffernan Feb 12 '13 at 11:11
  • @DavidHeffernan, thanks for the link. "Touches" indeed - I wish I had more information. – Cosmin Prund Feb 12 '13 at 12:55
  • @DavidHeffernan Thanks for the informative link..I would test the performance with the generics and typed lists.. – Rahul W Feb 13 '13 at 06:09