Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Copy and release



On 21 Oct 2004, at 21:05, Evan Schoenberg wrote:

On Oct 21, 2004, at 10:35 AM, Nicko van Someren wrote:

It is quite common when writing programs for Cocoa that a data structure needs to be mutable while it is being filled but after that it would be useful for it become immutable in order to take advantage of the efficiencies that affords once the object has been set up.

This has been a wonder of mine for a long time in Cocoa.. do you have information on how precisely the immutable objects are more efficient? I've heard the claim before but never seen hard data either confirming or denying it.

If you make immutable copies of immutable NSString, NSDate, NSArray or NSDictionary objects (to pick a few important examples) you simply get the same object with the reference count incremented. This saves some data copying for simple objects and by the time you get to complex objects like dictionaries the cost of copying can get pretty high. You can test this with something like:

NSDictionary *d1 = [NSDictionary dictionaryWithObjectsAndKeys: @"Blah", @"Blah", nil];
NSDictionary *d2 = [d1 copy];
NSLog(@"The objects are %p and %p", d1, d2);

Nicko

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Copy and release (From: Nicko van Someren <email@hidden>)
 >Re: Copy and release (From: Evan Schoenberg <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.