Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which effect do interfaces have on execution speed in Delphi?

If I replace all object references in my Delphi program with interface references, and use objects which are inherited from TInterfacedObject, will the resulting application run with the same speed as before? Or does the reference counting add a significant execution overhead at run time?

like image 330
mjn Avatar asked Aug 16 '10 11:08

mjn


2 Answers

The reference counting can affect you if you do a lot of assignments of those interfaces (or pass them as non-const, non-var parameters in function calls).

However the real trouble often isn't the reference counting itself, but the implicit try-finally constructs the compiler adds to protect the reference counting, which will add up to your calls overhead, and can be most painful if you have many simple methods (vs a single big procedure with all your code inside, which you don't really want).

To mitigate that aspect, always pass interfaces as either const or var parameters, avoid returning interfaces as function call result, and minimize usage of local variables of interface type, prefer const parameters and direct object field usages.

like image 174
Eric Grange Avatar answered Oct 25 '22 10:10

Eric Grange


Interfaced classes do carry an overhead of incrementing and releasing the reference count of each instance you create, pass and destroy, but unless you are creating, destroying and passing references in tight loops, you shouldn't experience any significant slow down.

You can of course disable reference counting as such by returning -1 in the _AddRef and _Release overrides, but that doesn't prevent the compiler from generating those calls...

like image 20
Marjan Venema Avatar answered Oct 25 '22 10:10

Marjan Venema