Swiftest packing

Kresimir Prcela
minus5
Published in
2 min readJun 2, 2016

Every programmer has heard about JSON format. It is nice, easy to understand, adoptable. In most cases it is used for communication between server and client applications.

But… you may realize that it is not the best option when you have large data set for fast transfer, it can’t be really fast serialized or deserialized to or from native model. Thus, there are some other, but not yet so popular formats.

For example, Google is using format called Flatbuffers. Originally, they created it for game development and other performance-critical applications. Now, Facebook uses it for data transfer.

In our company we discussed what format should be optimal for JSON replacement on both, client and server side. Due to small size and good performances we observed the MessagePack format.

I had visited known MessagePack implementations on github in order to integrate it into our swift project. After some straggling and few trials with stucks, I finally found an a2 message packer. It became trivial to pack and unpack our complex hierarchies of structures and classes.

The complete and detailed technical tutorial how I use this packer can be found at wiki github page.

After integration we had this performances:

Optimization flag = fast, iPhone 4, JSON file size 2.7 MB (zip 190kB)

Loading NSData from JSON file = 0.0921s
Converting NSData to JSON object = 1.203s
JSON to native model = 3.2691s
Native model packing to byte array = 3.051s
Byte array to NSData = 0.0033s
NSData to Byte array = 0.00390s
Byte array to native model = 2.6518s
Writing NSData to file = 0.101s

For testing purposes we have one large JSON file.

The byte array is representation of data packed with MessagePack format.

The byte array conversion to native model is noticeable faster than standard JSON object conversion to native model. JSON object representation in swift is actually a NSDictionary or NSArray.

Also, the packed model can be easily converted to NSData and written to file. File size of packed data is about 743kB which is about 3,5x smaller than original JSON file. It is zipped to 200kB which is similarly to size of zipped JSON file.

I believe that MessagePack module can be dramatically faster with some tricks like it is done for FlatBuffers format, as you can see in this post.

The native model app in swift application can be also saved to file using the NSKeyedArchiever class from system foundation, but it proves to be few times slower than likewise MessagePack-ing.

This packing concept reliefs your swift code from Cocoa framework and NSObject classes.

Only imperfection of the MessagePack format is its schemeless. It is hard to add or remove a property after the packed data goes into production.

Regardless, the MessagePack is great. So, go with it.

Happy coding

--

--