NSArchiver vs. NSKeyedArchiver Performance

Why is NSKeyedArchiver's performance so poor? Size doubles against NSArchiver.

I encode NSMutableArray objects with the following line

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

NSMutableArray contains custom objects that have 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 correctly, and then I can unlock it. They comprise about 3,000 objects, and the output file is 900 KB

The problem occurs when I change my archiving string to:

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

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

Why am I asking about this? because I am developing an iOS application and therefore losing support for NSArchiver.

+4
source share
3 answers

You get both forward and backward compatibility. This is keyed , which means that the archive must store additional information to perform a keyword search. See Here: Archives and Serialization

+1
source

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

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

0
source

Regarding downsizing, I believe it has something to do with NSKeyedArchiver storing links, not actual values.

I compared NSKeyedArchiver and NSArchiver, as well as the new cocotron-based NSKeylessArchiver class (NSArchiver may be disabled on iOS due to API private status). NSKeylessArchiver uses sizing links for data that has duplicate rows or objects, and decodes the alternative much faster if you don't need keys.

Performance for a simple root object without children and 20,000 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| 

See the Github report and blog post for more details.

0
source

Source: https://habr.com/ru/post/1390268/


All Articles