Re: Is it a bad idea to invoke "init..." over and over on the same object?
Re: Is it a bad idea to invoke "init..." over and over on the same object?
- Subject: Re: Is it a bad idea to invoke "init..." over and over on the same object?
- From: glenn andreas <email@hidden>
- Date: Wed, 2 Nov 2005 09:35:00 -0600
On Nov 2, 2005, at 9:25 AM, Andrei Tchijov wrote:
Daniel (and Jack)
Thanks for quick reply. I did consider use of
"dataWithBytes:length:" but I was trying to avoid allocation of
new NSData object altogether. How about this aproach:
...
int bigIoBufferSize = xxx;
ioData = [[ NSMutableData alloc ] initWithCapacity:
bigIoBufferSize ];
...
- (NSData*) readData {
char* bigIoBuffer = [ ioData mutableBytes ];
...
read data into bigIoBuffer
...
[ ioData setLength: bytesRead ];
return ioData;
}
...
In this scenario question is:
- Can we expect that NSMutableData will not re-allocate "bytes" as
a result of "setLength" if "length" is always less or equal initial
capacity?
You can expect lots of things, but unless they are actually
documented behavior, be prepared to be disappointed.
The bigger question is "why are you concerned"? (about things like
avoiding an allocation of NSData, etc...)
And before you say "I'm worried about the performance overhead of
allocating/freeing memory" - stop.
Worry about getting your program working _first_, and then, and only
then, see if there really is a performance problem, and use Shark to
find where it is. If there is, the odds are highly unlikely that
it's going to be in memory allocation routines (or object allocation,
for that matter).
Don't try to subvert standard usage idioms and patterns unless you've
actually encountered problems, and not before.
Glenn Andreas email@hidden
<http://www.gandreas.com/> wicked fun!
Widgetarium | the quickest path to widgets
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden