Re: WTF? How can this work?
Re: WTF? How can this work?
- Subject: Re: WTF? How can this work?
- From: Rainer Brockerhoff <email@hidden>
- Date: Sun, 19 Aug 2001 20:51:34 -0300
>
Date: Sun, 19 Aug 2001 11:11:40 -0700
>
From: Chris Kane <email@hidden>
>
>
On Sunday, August 19, 2001, at 09:31 AM, Ondra Cada wrote:
>
>>>>>>> John Hornkvist (JH) wrote at Sun, 19 Aug 2001 12:34:11 +0200:
>
> JH> In essence, the problem is that Apple has chosen to implement
>
> NSArray
>
> JH> and NSMutableArray with the same class. I regard this as a bug.
>
>
>
> Whew! That *IS* a bug all right. Perhaps I'm starting to have some idea
>
> why
>
> the OS X is that terribly slow and memory-hungry! Especially if they
>
> happened
>
> to use this bright idea for other immutable classes as well :(((
>
>
That's a perfectly legitimate implementation decision. Having something
>
typed "NSArray *" does NOT guarantee that it is immutable, though many
>
people have this misconception. The effect of the extra memory is a
>
couple percent. Internal fragmentation due to the 16-byte alignment of
>
heap-allocated blocks is a much larger effect (~9% in one test I
>
recall). In 10.1, the memory "loss" and immutability are recovered.
Chris, could you expand that last sentence a little more? How are they "recovered"?
I'm relieved to read your reply on the mutable question. Had you said the implementations were different, I'd be asking you now - rather plaintively - about the "secret method" Cocoa uses internally to construct immutable non-empty NSArrays.
As it is, it seems I'm doing the right thing in my own classes. Whenever I need to return an NSArray from my method, I declare an NSMutableArray internally, fill it with the information, then pass it to the caller saying it's an NSArray. He's obliged to treat it as such.
As a side-note, this is one of the reasons we should always declare object types instead of "id"s wherever possible - so the compiler can complain that addObject: shouldn't be used here, and so forth. I even change all the NSOutlet types to the correct type - I wonder why IB doesn't do that already.
--
Rainer Brockerhoff <email@hidden>
Belo Horizonte, Brazil
"In the affairs of others even fools are wise
In their own business even sages err."
http://www.brockerhoff.net/ (updated July 2000)