 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Vivek Guest
|
|
| Back to top |
|
 |
Bob Dawson Guest
|
Posted: Sat Oct 22, 2005 7:46 pm Post subject: Re: Automatic objects for delphi |
|
|
"Vivek" wrote
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
|
Posted: Sat Oct 22, 2005 9:27 pm Post subject: Re: Automatic objects for delphi |
|
|
On Sat, 22 Oct 2005 14:46:44 -0500, Bob Dawson wrote:
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
|
Posted: Sat Oct 22, 2005 11:45 pm Post subject: Re: Automatic objects for delphi |
|
|
"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
|
Posted: Sun Oct 23, 2005 12:30 am Post subject: Re: Automatic objects for delphi |
|
|
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
|
Posted: Sun Oct 23, 2005 12:55 am Post subject: Re: Automatic objects for delphi |
|
|
"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
|
Posted: Sun Oct 23, 2005 4:32 am Post subject: Re: Automatic objects for delphi |
|
|
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
|
Posted: Sun Oct 23, 2005 6:22 am Post subject: Re: Automatic objects for delphi |
|
|
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
|
Posted: Sun Oct 23, 2005 10:42 am Post subject: Re: Automatic objects for delphi |
|
|
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
|
Posted: Sun Oct 23, 2005 10:55 am Post subject: Re: Automatic objects for delphi |
|
|
Vivek wrote:
Thanks for that!
Just one question: why D6 and above? What's missing in D5?
|
|
| Back to top |
|
 |
Vivek Guest
|
Posted: Sun Oct 23, 2005 12:52 pm Post subject: Re: Automatic objects for delphi |
|
|
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
|
Posted: Sun Oct 23, 2005 1:57 pm Post subject: Re: Automatic objects for delphi |
|
|
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
|
Posted: Sun Oct 23, 2005 3:58 pm Post subject: Re: Automatic objects for delphi |
|
|
"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
|
Posted: Sun Oct 23, 2005 4:19 pm Post subject: Re: Automatic objects for delphi |
|
|
On 22 Oct 2005 11:50:56 -0700, Vivek <rep.movsd (AT) gmail (DOT) com> wrote:
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
|
Posted: Sun Oct 23, 2005 4:36 pm Post subject: Re: Automatic objects for delphi |
|
|
"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 |
|
 |
|
|
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
|
|