1

When I started with OSX/iOS I used

    NSMutableArray *        a1 = [NSMutableArray arrayWithCapacity:123] ;
    NSMutableDictionary *   d1 = [NSMutableDictionary dictionaryWithCapacity:123] ;

Then I discovered the 'simpler' version

    NSMutableArray *        a2 = [NSMutableArray array] ;
    NSMutableDictionary *   d2 = [NSMutableDictionary dictionary] ;

I have now moved to:

    NSMutableArray *        a3 = [@[] mutableCopy] ;
    NSMutableDictionary *   d3 = [@{} mutableCopy] ;

Functionally, they all seem identical: once initialized either way, they can be used regardless of how they were created. Where are the differences?

In particular, should I assume that d3/a3 are more similar to d2/a2 than d1/a1 in terms of memory pre-allocation (or lack thereof) ?

Or is this just a matter of style?

3
  • 3
    The third one constructs a non-mutable object and copies it, creating some wasted motion. That wasted motion is not present in the first two. The difference between the first and second is whether the initial size is specified or defaulted. I see no real point in the third -- even I'm not that lazy.
    – Hot Licks
    May 14, 2013 at 3:42
  • 2
    You can also simply call new to instantiate either one.
    – uchuugaka
    May 14, 2013 at 4:40
  • 1
    @HotLicks how certain are you about the actual cost? By definition an NSArray is immutable once created, meaning that every single instance of @[] should all be isEqual. Which in turn means that a very cheap optimisation by the compiler/runtime is to have exactly ONE instance of @[] no matter how often either @[] or [NSArray array] or [NSArray new] appears in the code. How would you go about checking that I'm wrong?
    – verec
    May 14, 2013 at 6:19

3 Answers 3

2

Form one is class factory method that is an optimization useful when you know how large a collection initially needs to be or you know the size will be constant but content may change.

Form two is a class factory method equivalent to calling new. This may or may not be the same as alloc followed by init, but is effectively the same.

Form three is implicitly the same as alloc followed by initWithArray: or initWithDictionary: respectively. It's convenient but generates an unneeded immutable instance that is discarded under ARC it may not be clear when it is discarded.

Use form one or form two generally if you are not going to ever use the immutable instance again elsewhere.

1

a1/d1 are allocated with enough initial space for 123 entries.

a2/d2 are allocated with enough initial space for 5 entries.

If you know that you are going to add a bunch of entries, a1/d1 are more efficient because the dictionary won't have to keep allocating more memory to hold more and more entries.

a3/d2 are closer to a2/d2 but they are really the same as:

NSMutableArray *a4 = [[NSArray array] mutableCopy];

This is less efficient than a2.

3
  • 1
    b.t.w. how do you know a2/d2's default value is "5" entries? is that in Apple docs or their open source somewhere? May 14, 2013 at 3:21
  • 1
    Good question. That is what I thought it was but I just checked the docs and I can't find any mention of that default capacity. I recall it being something I read when I first started iOS development nearly 5 years ago.
    – rmaddy
    May 14, 2013 at 3:28
  • 1
    I suspect that the initial size is based on the amount of space left in a "round" allocation after all the header overhead. I'd guess that they attempt to have it be in the neighborhood of 10-15. In any event, the overhead to allocate more entries is probably way less than the extra object creation overhead of version 3. Given the way that NSMutableArrays are often used, the overhead of allocating more entries is usually trivial in comparison.
    – Hot Licks
    May 14, 2013 at 3:47
1

In the first example, you not only create mutable dictionary and array, but you also specify their capacities. It's very useful if you are making some complicated calculations during which you create a lot of mutable collections, because it gives performance boost in such case. The other two examples are the same. Literals were introduced less than a year ago and they are out there just to make developers' lives easier.

Not the answer you're looking for? Browse other questions tagged or ask your own question.