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?
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With