views:

150

answers:

1

I'm using NSKeyedArchiver in Mac OS X program which generates data for iPhone application. I found out that by default, resulting archives are much bigger than I expected. Example:

NSMutableArray * ar = [NSMutableArray arrayWithCapacity:10];

for (int i = 0; i < 100000; i++) {
    NSString * s = [NSString stringWithFormat:@"item%06d", i];
    [ar addObject:s];
}
[NSKeyedArchiver archiveRootObject:ar toFile: @"NSKeyedArchiver.test"];

This stores 10 * 100000 = 1M bytes of useful data, yet the size of the resulting file is almost three megabytes. The overhead seems to be growing with number of items in the array. In this case, for 1000 items, the file was about 22k.

"file" reports that it is a "Apple binary property list" (not the XML format).

Is there an simple way to prevent this huge overhead? I wanted to use the NSKeyedArchiver for the simplicity it provides. I can write data to my own, non-generic, binary format, but that's not very elegant. Also, aggregating the data into large chunks and feeding these to the NSKeyedArchiver should work, but again, that kinda beats the point of using simple&easy&ready to use archiver. Am I missing some method call or usage pattern that would reduce this overhead?

+1  A: 

I filed a bug to track this.

That aside, NSKeyedArchiver is designed for archiving object networks. Like, if an object appears twice in the graph, on unarchiving you'll still find that is the case. You're probably seeing overhead for this kind of uniquing.

For hierarchical structured data as opposed to arbitrary object networks, try NSPropertyListSerialization. I see 1.8MB for a binary plist.

Ken
Thanks 8) You are right, plist should do the trick. I forgot it has a binary format too.