Re: Copy and release
Re: Copy and release
- Subject: Re: Copy and release
- From: Nicko van Someren <email@hidden>
- Date: Fri, 22 Oct 2004 14:50:00 +0100
On 22 Oct 2004, at 13:24, Paul Sargent wrote:
Nicko van Someren wrote:
...
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.
A question I've had is when a container is said to be immutable, is it
still fair game to change the contents of the objects that are
contained?
For example: An NSArray containing NSMutableString objects, or an
NSData object. Can you / Should you change the contents of the
NSMutableStrings in the NSArray or the Memory block pointed to by the
NSData?
Should items contained in immultable container classes be considered
immutable?
I would submit that if I have a mutable object and I make a change to
it then it's still the same object, it's just that its state has
changed. Thus an immutable array of mutable strings can reasonably
have its strings altered. If you think of the case of I have a fixed
list of who my parents are but their addresses might change this might
make more sense.
It is worth noting that the NSDictionary class cluster deals with the
problem that the keys might be mutable by stating that the keys must
support the copying protocol and the keys actually get copied when
added to the dictionary whereas the values are merely retained. If the
keys are immutable then the copying may well be very efficient (as for
immutable NSString objects) but there is still a copy message sent.
It's actually the dictionary case for which the copyAndRelease call
would have most effect since copying a mutable dictionary re-copies all
the keys and if you're keying off anything the requires a full copy the
conversion from a mutable dictionary to an immutable one can be very
costly.
Nicko
_______________________________________________
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