• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Is it a bad idea to invoke "init..." over and over on the same object?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Is it a bad idea to invoke "init..." over and over on the same object?
      • From: Andrei Tchijov <email@hidden>
References: 
 >Is it a bad idea to invoke "init..." over and over on the same object? (From: Andrei Tchijov <email@hidden>)
 >Re: Is it a bad idea to invoke "init..." over and over on the same object? (From: Daniel Jalkut <email@hidden>)
 >Re: Is it a bad idea to invoke "init..." over and over on the same object? (From: Andrei Tchijov <email@hidden>)

  • Prev by Date: Re: Is it a bad idea to invoke "init..." over and over on the same object?
  • Next by Date: NSOutlineView: expand all items. How?
  • Previous by thread: Re: Is it a bad idea to invoke "init..." over and over on the same object?
  • Next by thread: Re: Is it a bad idea to invoke "init..." over and over on the same object?
  • Index(es):
    • Date
    • Thread