BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Automatic objects for delphi
Goto page 1, 2  Next
 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design
View previous topic :: View next topic  
Author Message
Vivek
Guest





PostPosted: Sat Oct 22, 2005 6:50 pm    Post subject: Automatic objects for delphi Reply with quote




Please see http://cc.borland.com/Item.aspx?ID=23718
for a cool automatic objects hack for delphi 6 and above

Back to top
Bob Dawson
Guest





PostPosted: Sat Oct 22, 2005 7:46 pm    Post subject: Re: Automatic objects for delphi Reply with quote



"Vivek" wrote

Quote:
Please see http://cc.borland.com/Item.aspx?ID=23718

Sorry, don't see the point. ISTM that Free works fine, runs faster, and is
easier to read:

bobD



Back to top
Marc Rohloff [TeamB]
Guest





PostPosted: Sat Oct 22, 2005 9:27 pm    Post subject: Re: Automatic objects for delphi Reply with quote



On Sat, 22 Oct 2005 14:46:44 -0500, Bob Dawson wrote:

Quote:
"Vivek" wrote

Please see http://cc.borland.com/Item.aspx?ID=23718

Sorry, don't see the point. ISTM that Free works fine, runs faster, and is
easier to read:

Just to play devil's advocate here, at least it doesn't require
exception handling.

--
Marc Rohloff [TeamB]
marc rohloff -at- myrealbox -dot- com

Back to top
Bob Dawson
Guest





PostPosted: Sat Oct 22, 2005 11:45 pm    Post subject: Re: Automatic objects for delphi Reply with quote

"Marc Rohloff [TeamB]" wrote
Quote:

Just to play devil's advocate here, at least it doesn't require
exception handling.

Admit I didn't try it, but would be willing to bet that the try finally
block is faster than the interfaced wrapper.

bobD



Back to top
Marc Rohloff [TeamB]
Guest





PostPosted: Sun Oct 23, 2005 12:30 am    Post subject: Re: Automatic objects for delphi Reply with quote

On Sat, 22 Oct 2005 18:45:29 -0500, Bob Dawson wrote:

Quote:
Just to play devil's advocate here, at least it doesn't require
exception handling.

Admit I didn't try it, but would be willing to bet that the try finally
block is faster than the interfaced wrapper.

I don't know exception handling is *really* expensive and you could
even possibly create your interface wrapper without reference counting
since its should never get more than one AddRef.

Although to be honest here I should mention that the moment you add an
interface ref to a procedure it will get a hidden try..finally block
to handle it (although this block is probably already created since it
also needs to exist for strings and dynamic arrays)

--
Marc Rohloff [TeamB]
marc rohloff -at- myrealbox -dot- com

Back to top
Bob Dawson
Guest





PostPosted: Sun Oct 23, 2005 12:55 am    Post subject: Re: Automatic objects for delphi Reply with quote

"Marc Rohloff [TeamB]" wrote
Quote:

I don't know exception handling is *really* expensive

Alright--I'll try to run a test or two tonight or tomorrow and report back.

bobD



Back to top
Rob Kennedy
Guest





PostPosted: Sun Oct 23, 2005 4:32 am    Post subject: Re: Automatic objects for delphi Reply with quote

Bob Dawson wrote:
Quote:
"Marc Rohloff [TeamB]" wrote
Just to play devil's advocate here, at least it doesn't require
exception handling.

Admit I didn't try it, but would be willing to bet that the try finally
block is faster than the interfaced wrapper.

When you use interfaces in a function, the compiler automatically wraps
the body of the function in a try-finally handler so it can call
_Release on the interface. It's the same try-finally the compiler
inserts to ensure that AnsiStrings, WideStrings, dynamic arrays, and
Variants get freed.

So, although you don't actually type the words "try" and "finally,"
there's still a try-finally block there, and it still gets triggered on
exceptions.

--
Rob

Back to top
Vivek
Guest





PostPosted: Sun Oct 23, 2005 6:22 am    Post subject: Re: Automatic objects for delphi Reply with quote


Hi

Sure it might be a little inefficient because of all the wrapping, but just consider that you can jump out of the method using Exit, without bothering that youve left something unfreed ,this reduces in many cases the level of nesting of if blocks.

For me, coming from a C++ background, the convenience of this is worth the slight inefficiency. Maybe the functions can be inlined if performance is an issue.

Regards

Back to top
Thomas Mueller
Guest





PostPosted: Sun Oct 23, 2005 10:42 am    Post subject: Re: Automatic objects for delphi Reply with quote

Hi,

Vivek wrote:

Quote:
Sure it might be a little inefficient because of all the wrapping, but
just consider that you can jump out of the method using Exit, without
bothering that youve left something unfreed ,this reduces in many cases
the level of nesting of if blocks.

You could instead just use one try..finally

begin
MyObj := TMyClass.Create;
try
[more code here]
exit; // -> this will also execute the finally block
[more code here]
finally
MyObj.Free;
end;
end;

But I have to admit, that I have started using more and more interfaces.

twm


Back to top
Abdullah Kauchali
Guest





PostPosted: Sun Oct 23, 2005 10:55 am    Post subject: Re: Automatic objects for delphi Reply with quote

Vivek wrote:
Quote:
Please see http://cc.borland.com/Item.aspx?ID=23718
for a cool automatic objects hack for delphi 6 and above



Thanks for that!

Just one question: why D6 and above? What's missing in D5?

Back to top
Vivek
Guest





PostPosted: Sun Oct 23, 2005 12:52 pm    Post subject: Re: Automatic objects for delphi Reply with quote


Abdullah Kauchali <non (AT) non (DOT) com> wrote:
Quote:
Thanks for that!
Just one question: why D6 and above? What's missing in D5?

I believe TInterfacedObject was provided only in D6.
There might be some other similar class which can be substituted for it in D5.

Cheerio




Back to top
Vivek
Guest





PostPosted: Sun Oct 23, 2005 1:57 pm    Post subject: Re: Automatic objects for delphi Reply with quote


Thomas Mueller <spam (AT) dummzeuch (DOT) de> wrote:
Quote:

You could instead just use one try..finally

.... Snip ...
But I have to admit, that I have started using more and more interfaces.

Well, the fact that it goes to the finally block on Exit was new to me!

What my hack allows is that you dont have to bother making any interfaces, You can make it work with any TObject descendent.
Why type all that extra try..finally code just for freeing objects when the compiler adds it for you?

BTW preformance testing on d6 and d2005 trial show :

D2005 (TStringList)
Auto objects - 1.18 Million objects per second
Normal objects - 2 Million objects per second

D6
Auto objects - 1.26 Million objects per second
Normal objects - 2.05 Million objects per second

using register and/or inline didn't make any difference.

So there is a 5:3 performance penalty...

However for TBitmap, All return identical results of about 1.14 million objects per second.

Regards




Back to top
Bob Dawson
Guest





PostPosted: Sun Oct 23, 2005 3:58 pm    Post subject: Re: Automatic objects for delphi Reply with quote

"Vivek" wrote
Quote:

[...] just consider that you can jump out of the method using Exit,
without bothering


So one advantage to the technique is that it makes it easier to write poorly
structured code? <g>

bobD



Back to top
Krisztian Pinter
Guest





PostPosted: Sun Oct 23, 2005 4:19 pm    Post subject: Re: Automatic objects for delphi Reply with quote

On 22 Oct 2005 11:50:56 -0700, Vivek <rep.movsd (AT) gmail (DOT) com> wrote:

Quote:
Please see http://cc.borland.com/Item.aspx?ID=23718
for a cool automatic objects hack for delphi 6 and above


can anyone please tell me where in the documentation is precisely
described the automatic cleanup policy?

afaik, it is not fixed, so compiler can optimize your code, and
might release the interface and thus the object:

1. on TestIt routine exit. that's ok.
2. immediately after calling MakeAutoObject, as you don't use it
anymore
3. inside MakeAutoObject, after the Make call, as you don't use it
anymore

it can be the case, that the current implementation of the compiler
does not recognise these possibilities, but

A. how can you be sure, if not told? reverse engineer compiler?
B. how can you tell it will not change in future?


Back to top
Bob Dawson
Guest





PostPosted: Sun Oct 23, 2005 4:36 pm    Post subject: Re: Automatic objects for delphi Reply with quote

"Krisztian Pinter" wrote
Quote:

afaik, it is not fixed, so compiler can optimize your code, and
might release the interface and thus the object:

Keen observation--I'm pretty sure it's not guaranteed to be the last thing
before the final 'end;'.

Would be interesting to play around with the routine to see if your could
affect that--perhaps by adding wait states, making other allocation calls,
or some other technique that might trigger the compiler (or runtime, if
reference management is handled opportunistically) to handle cleanup
earlier.

bobD



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.