How we improved Padgram performance to “incredible” level (2)

As planned, I am going to write about “Choose the right JSON parsing library” in this session.

JSON format is popularly used between client and server communication. I’ve not met any open service without JSON interfaces yet. Padgram does a lot of JSON parsing when talking with Instagram server.

Occasionally, I find in a short period, 59% of CPU is used in parsing JSON data. That’s crazy! So I dig into the JSON library we used – SBJson.

The result is pretty obvious, the performance of SBJson is far from good. It ranks only 4th or 5th place compared with major competitors (SBJson is named “json-framework” in the chart):

  

You can read more here: https://github.com/johnezang/JSONKit

Obviously, JSONKit is the best among all competitors. After switching to JSONKit, I’ve never caught the CPU spike like before. (However, I didn’t get round to write testing program myself, I prefer to trust the genius guys in open source community)

In using JSONKit, there are some tips for speed that you should read on https://github.com/johnezang/JSONKit. Among them, we should use NSData over NSString to avoid the overhead to instantiate NSString objects. Here is our decode wrapper function that deals with this:

– (id)decodeData:(NSData *)data withEncoding:(NSStringEncoding)encoding
{

if (data == nil) {
return nil;
}

// this is important for performance, see here: https://github.com/johnezang/JSONKit
if (encoding == NSUTF8StringEncoding) {
return [[JSONDecoder decoder] mutableObjectWithData:data];
} else {
NSString *string = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:encoding];
return [string mutableObjectFromJSONString];
}

}

With proper JSON library, and incorporating with proper threading framework, Padgram gets great user interaction experience when scrolling at the same time of loading in the background. You could try Padgram from App Store.