On Jul 26, 2009, at 5:38 PM, I. Savant wrote:

On Jul 26, 2009, at 3:52 PM, Aaron Burghardt wrote:

Not necessarily. If the keys are NSStrings, then they are copied when added to the dictionary, but a copy of an immutable string is optimized to just retain it, so the data isn't duplicated (assuming all rows have the same columns in the same order, an assumption you don't seem to be making).

Actually, I temporarily take back my "you're probably right" response. :-) I can't find a reference to this anywhere, but I admittedly only looked in the NSString API reference, the Introduction to Strings Programming Guide for Core Foundation and quickly perused the The Objective-C 2.0 Programming Language.

Would you mind directing me to where this is stated? I'm not saying you're wrong - it sounds plausible - I'm just not sure you're right. :-)


I didn't think it was documented anywhere, but Adam found a reference for at least some forms. But, it's well-known on the list and easy to test (see example below). Caveat: it's an implementation detail that you normally shouldn't be concerned with. But, if you get to the point that you need to optimize memory consumption, it is worthwhile to confirm the behavior. For example, if you pass mutable strings to the method that parses the CSV, then you need to make immutable copies first.

Keep in mind that an NSArrayController created in Interface Builder is defined by default to create an NSMutableDictionary for each item in the dictionary.

 With respect, I don't see how this is relevant to my point.

It was an unnecessary point. I was interpreting your comments to be strongly advocating not using dictionaries, and I was trying to suggest it is more common than that.

It's not unreasonable, and we don't know the OP's performance requirements.


No, it's not unreasonable, but since we don't know the OP's performance requirements, the original blanket statement that NSDictionary is better won't do without these caveats (ie, "it's easier for bindings, but dog-slow on reasonably large files"). Rather than just say "there's more to it than that" as a drive-by correction, I wanted to provide a helpful explanation and workaround, since this is an area in which I have recent and detailed experience. :-)

You brought up some good discussion points, and I probably should have asked the OP to give us more details. I inferred from his question that he didn't have a working solution, so I suggested the simplest working solution that was inline with his current mindset. I didn't mean to imply it was the best solution, but my response was so short it easily could be read that way. I assumed that if performance wasn't adequate, we would get a follow-up question. But, I think it is better to start with an easy-to-understand working solution and then optimize it if necessary.

Regards,

Aaron


#import <Foundation/Foundation.h>

int main(int argc, char *argv[])
{
        NSAutoreleasePool *pool = [NSAutoreleasePool new];

        NSString *string1, *string2;

        // create a string
string1 = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:argv[0] length:strlen(argv[0])];
        NSLog(@"String value: %@", string1);
NSLog(@"String 1 class: %@ address: %p retain count: %ld", NSStringFromClass([string1 class]), string1, [string1 retainCount]);
        string2 = [string1 copy];
NSLog(@"String 2 class: %@ address: %p retain count: %ld", NSStringFromClass([string2 class]), string2, [string2 retainCount]);

        // create a path-optimized string
        string1 = [string1 stringByDeletingLastPathComponent];
        NSLog(@"String value: %@", string1);
NSLog(@"String 1 class: %@ address: %p retain count: %ld", NSStringFromClass([string1 class]), string1, [string1 retainCount]);
        string2 = [string1 copy];
NSLog(@"String 2 class: %@ address: %p retain count: %ld", NSStringFromClass([string2 class]), string2, [string2 retainCount]);
        
        [pool drain];
        return 0;
}

Compile on the command line with:

$ gcc -framework Foundation -o test test.m

An example run:

$ /tmp/stringtest/test
2009-07-26 20:06:25.258 test[15385:903] String value: /tmp/stringtest/ test 2009-07-26 20:06:25.262 test[15385:903] String 1 class: NSCFString address: 0x10010c630 retain count: 1 2009-07-26 20:06:25.266 test[15385:903] String 2 class: NSCFString address: 0x10010c630 retain count: 2
2009-07-26 20:06:25.267 test[15385:903] String value: /tmp/stringtest
2009-07-26 20:06:25.267 test[15385:903] String 1 class: NSPathStore2 address: 0x100110560 retain count: 1 2009-07-26 20:06:25.268 test[15385:903] String 2 class: NSPathStore2 address: 0x100110560 retain count: 2


Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

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