On Jul 21, 2011, at 12:30 AM, Jens Alfke wrote:

> 
> On Jul 20, 2011, at 2:34 PM, Andreas Grosam wrote:
> 
>> According the doc, the parameter "capacity" in function 
>> CFDictionaryCreateMutable() sets the *maximum number* of key-value pairs 
>> which can be inserted into the container. That is, it's not an *initial* 
>> capacity.
> 
> I think that was a mistake in the docs. The comment in CFDictionary.h in the 
> 10.7 SDK says:
> 
>         @param capacity A hint about the number of values that will be held
>                 by the CFDictionary. Pass 0 for no hint. The implementation 
> may
>                 ignore this hint, or may use it to optimize various
>                 operations. A dictionary's actual capacity is only limited by 
>                 address space and available memory constraints). If this 
>                 parameter is negative, the behavior is undefined.
> 
> Since CFDictionary and NSDictionary have the same implementation under the 
> hood, I think the capacity will have the same effect at runtime whichever API 
> you use.
> 
> —Jens
On Jul 21, 2011, at 1:48 AM, Jean-Daniel Dupas wrote:
> 
> IIRC, the semantic of the capacity parameter changed in 10.5. Before 10.5, it 
> specified the max capacity and trying to insert more elements was documented 
> as an undefined behavior.

OK, thank you both for the infos. I should have mentioned that I'm compiling 
with base SDK 10.6 on Mac OS 10.6.8.
Should test this on 10.7 with Base SDK 10.7 as well.

Nonetheless, when defining a mutable dictionary with CF functions, the 
subsequent insertions take far more time than when using Foundation, e.g.:

            CFMutableDictionaryRef o = 
#if USE_CF             
                CFDictionaryCreateMutable(NULL, count,
                                          &kCFTypeDictionaryKeyCallBacks, 
                                          &kCFTypeDictionaryValueCallBacks);
#else            
                CFMutableDictionaryRef([[NSMutableDictionary alloc] 
initWithCapacity:count]);
#endif            


With Instruments I could figure that the CF version takes more time due to 
additional need for rehashing the container when elements are inserted. This 
results in a huge difference in the runtime (roughly factor 2.5) for inserting 
elements , given the fact that the "count" parameter is actually the final 
number of elements which will be inserted.

So, looking at Instruments profiling a test case (a couple of dictionaries, 
with a couple of elements) comparing CF to Foundation, the difference are as 
follows (Mac OS 10.6.8, Base SDK 10.6):

CF version:
197 ms  ___CFBasicHashFindBucket1
174 ms  __CFBasicHashRehash
377 ms  CFDictionaryAddValue

Foundation version:
80 ms  ___CFBasicHashFindBucket1
21 ms  __CFBasicHashRehash
147 ms  CFDictionaryAddValue

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to