Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2016-04

try..finally vs interfaces performance

9 messages · 6 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mazola Winstrol

Recently i did some maintenance in a code of a colleague. I realized that he designed several classes with interface support so he do not need to protect the code blocks with try..finally to ensure that the instances are released from memory.

Theoretically, which solution has higher performance? The traditional approach (code protected try..finally) or classes with interfaces (no need to protect the code. by otherside, classes with interfaces support have lock operations)?

Regards

Reply by email

Graeme Geldenhuys
to Mazola Winstrol
Mazola Winstrol wrote
Theoretically, which solution has higher performance?

Interesting question. As for the answer I have no idea. Why don't you put a quick test together. Loop the code 10,000 or 100,000 times and time the results. Please share your test code and answer if you don't mind.

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

Michael Van Canneyt
to Mazola Winstrol: Recently i did some maintenance in a code of a colleague. I realized that he designed…
Mazola Winstrol wrote 10 lines
Recently i did some maintenance in a code of a colleague. I realized that he designed several classes with interface support so he do not need to protect the code blocks with try..finally to ensure that the instances are released from memory. Theoretically, which solution has higher performance? The traditional approach (code protected try..finally) or classes with interfaces (no need to protect the code. by otherside, classes with interfaces support have lock operations)?

It should be exactly the same.
As soon as interfaces are used the compiler inserts an implicit try...finally in a procedure. You can see this if you step through the code with GDB, the execution point will jump to the end of the procedure and then back to the beginning...

Michael.

Reply by email

Marcos Douglas
to Mazola Winstrol: Recently i did some maintenance in a code of a colleague. I realized that he designed…
Mazola Winstrol wrote 9 lines
Recently i did some maintenance in a code of a colleague. I realized that he designed several classes with interface support so he do not need to protect the code blocks with try..finally to ensure that the instances are released from memory. Theoretically, which solution has higher performance? The traditional approach (code protected try..finally) or classes with interfaces (no need to protect the code. by otherside, classes with interfaces support have lock operations)?

Hi,

I don't know the answer but I think should be exactly the same. Even if not, I strongly recommend you to use interfaces instead "try-finally" approach
because you gain more than just automatic memory release.

Regards,
Marcos Douglas

PS. I have a blog about Object Pascal. I write about real object-oriented approach.
The blog is written in Portuguese but using Google Translate I guess you won't have
problems to translate.

http://objectpascalprogramming.com/

Reply by email

Graeme Geldenhuys
to Marcos Douglas
Marcos Douglas wrote
because you gain more than just automatic memory release.

It is also worth noting that not all Interface usage means “automatic memory release”. If you use COM-style Interfaces, then yes you get memory management. If you use CORBA-style Interface, then no you don’t get memory management.

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

Marcos Douglas
to Graeme Geldenhuys
Graeme Geldenhuys wrote 7 lines
On 2016-04-14 14:15, Marcos Douglas wrote: > because you gain more than just automatic memory release. It is also worth noting that not all Interface usage means “automatic memory release”. If you use COM-style Interfaces, then yes you get memory management. If you use CORBA-style Interface, then no you don’t get memory management.

You're right, well remembered.
I use only COM-style Interfaces.

Regards,
Marcos Douglas

Reply by email

Vincent Snijders
to Michael Van Canneyt: It should be exactly the same. As soon as interfaces are used the compiler inserts an…

2016-04-14 10:50 GMT+02:00 Michael Van Canneyt :

Show quoted text 21 lines
On Thu, 14 Apr 2016, Mazola Winstrol wrote: Recently i did some maintenance in a code of a colleague. I realized that > he designed several classes with interface support so he do not need to > protect the code blocks with try..finally to ensure that the instances are > released from memory. > > Theoretically, which solution has higher performance? The traditional > approach (code protected try..finally) or classes with interfaces (no need > to protect the code. by otherside, classes with interfaces support have > lock operations)? > > It should be exactly the same. As soon as interfaces are used the compiler inserts an implicit try...finally in a procedure. You can see this if you step through the code with GDB, the execution point will jump to the end of the procedure and then back to the beginning...

I would venture, that manually adding try finally would give you better performance than using interfaces at the cost of writing more code and therefore less clear and more error-prone source.

If you use interfaces, the compiler also adds reference counting, which you don't have if you free the objects at the right places.

It is not sure if you can measure the difference in performance though.

Vincent

Reply by email

Luiz Americo Pereira Camara
to Graeme Geldenhuys: It is also worth noting that not all Interface usage means “automatic memory…

2016-04-14 10:58 GMT-03:00 Graeme Geldenhuys
:

Show quoted text 6 lines
On 2016-04-14 14:15, Marcos Douglas wrote: > because you gain more than just automatic memory release. It is also worth noting that not all Interface usage means “automatic memory release”. If you use COM-style Interfaces, then yes you get memory management.

Not necessarily, you can use COM interfaces with TComponent or other class that implements addref method but does not automatically frees the instance when refcount is zero

Luiz

Reply by email