5

Why is NSKeyedArchiver performance so poor? The size doubles vs using NSArchiver.

I am encoding an NSMutableArray of objects with the following line

BOOL result = [NSArchiver archiveRootObject:self.appDataObject.materias toFile:archivePath];

the NSMutableArray contain custom objects that have their corresponding encodeWithCoder and initWithCoder

-(void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject: _fileName];
    [aCoder encodeObject: _categoria];
    [aCoder encodeObject: _materia];
    [aCoder encodeObject: _nombre];

    [aCoder encodeObject: _position];
    [aCoder encodeValueOfObjCType:@encode(BOOL) at:&_favorite];

}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    if (self=[super init]) {
        [self setFileName:[aDecoder decodeObject]];
        [self setCategoria:[aDecoder decodeObject]];
        [self setMateria:[aDecoder decodeObject]];
        [self setNombre:[aDecoder decodeObject]];

        [self setPosition:[aDecoder decodeObject]];
        [aDecoder decodeValueOfObjCType:@encode(BOOL) at:&_favorite];
    }

    return self;
}

it works fine as it saves the file properly and then I am able to unarchive it. They are around 3000 objects and the output file is about 900kB

The problem occurs when I change my archiving line to:

BOOL result = [NSKeyedArchiver archiveRootObject:self.appDataObject.materias toFile:archivePath];

Everything magically works BUT the file size more than doubles to 2MB!

Why am I asking this? because I am developing a iOS application and therefore lose support of NSArchiver.

3 Answers 3

2

As for the size reduction, I believe it has something to do with NSKeyedArchiver storing references as opposed to actual values.

I compared the NSKeyedArchiver and NSArchiver, along with a new class called NSKeylessArchiver that is based off of cocotron (NSArchiver may be off-limits on iOS due to private API status). NSKeylessArchiver uses references to reduce size for data that has repeated strings or objects, and is much faster decoding alternative if you do not require keys.

Performance for simple root object without children and 20000 ints:

|                 |encoding (min/max/avg secs)|decoding (min/max/avg secs)|
|-----------------|:-------------------------:|:-------------------------:|
|NSKeyedArchiver  |  0.2048/0.2453/0.2165     |  6.8919/6.9238/6.9037|
|NSKeylessArchiver|  0.0407/0.0506/0.0451     |  0.0253/0.0330/0.0287|
|NSArchiver       |  0.0094/0.0114/0.0102     |  0.0019/0.0025/0.0020|

For more details, see the Github repo and blog post.

1

You are gaining both forward and backward compatibility. It's keyed, which means the archive has to store more information to do keyed lookups. See the ref here: Archives and Serialization

4
  • Yes, I understand the advantages of the NSKeyedArchiver, but what if I don't need the backward compatibility? only need the performance? In this example I am not even using keys, the objects are still saved in series, using the same coders, I am only using the "Keyed" command but still saving the old way. The file is taking more than 15 seconds to load in an Iphone, which for user experience, is a lot.
    – Ben Quan
    Jan 10, 2012 at 18:24
  • Have you considered alternate storage techniques, such as Code Data? Do you need all 3000 objects available at once, or can you access them in pages or chunks?
    – Rayfleck
    Jan 10, 2012 at 18:34
  • I went with NSArchiver for its simplicity. NSPropertyListSerialization does not allow custom objects. And I am constantly querying over the 3000 items, so they must be in memory. Can't remember why I stopped using Core Data, is it generally more efficient than Archiver?
    – Ben Quan
    Jan 10, 2012 at 19:22
  • 2
    If you're constantly querying 3000 items, should you be using CoreData or something other than keeping 3000 items in active memory?
    – Kudit
    Mar 7, 2013 at 1:17
0

I know this thread is old, but you my want to consider using NSPropertyListSerialization...

Take a look at the performance difference as mentioned in this thread: http://www.cocoabuilder.com/archive/cocoa/2221-nspropertylistserialization-vs-nskeyedarchiver-performance.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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