• 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: Andrei Tchijov <email@hidden>
  • Date: Wed, 2 Nov 2005 10:59:26 -0500

Application in question is kind of "socket multiplexor", so after initial setup is done it does not do much but reads from one socket and writes to another. Shark was used and it did point on allocation of new NSData object as a biggest performance issue (outside of setup/tear-down phases).

By the way, this version (below) does work wonderfully and it made "readData" method completely "disappear" from the list of "performance issues" from Shark point of view.

- (NSData*) readData {
if( _iobuffer == nil ) {
return [ self _readDataToNewBuffer ];
} else {
int bytesCount;
if(( bytesCount = [ self _readDataToBuffer: _iobuffer ofLength: _iobuffercapacity ]) <= 0 ) {
return nil;
}
return [ _iodata initWithBytesNoCopy: _iobuffer length: bytesCount freeWhenDone: NO ];
}
}


I am not sure if I will insist on using this approach (as you can see using pre-allocated _iobuffer is an option), but it does produce some noticeable performance gains.
Thanks a lot for your comments


Andrei Tchijov
Leaping Bytes, LLC

     web: www.leapingbytes.com
   email: email@hidden
      AIM: leapingbytes
Google: email@hidden
      ICQ: 151229319




On Nov 2, 2005, at 10:35 AM, glenn andreas wrote:


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
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>)
 >Re: Is it a bad idea to invoke "init..." over and over on the same object? (From: glenn andreas <email@hidden>)

  • Prev by Date: NSOutlineView: expand all items. How?
  • Next by Date: Re: Is it a bad idea to invoke "init..." over and over on the same object?
  • 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