-2

iOS devices are getting better, have more cores but how can we get benefit out of it while we are parsing JSON?

Currently, I am using JSONDecoder() for JSON Parsing. Is there way we can do it faster? Maybe using multiple threads parsing in parts etc.

Any hints/pointers will be appreciated.

    import Foundation
    
    let filePath = Bundle.main.path(forResource: "json", ofType: "json")
    
    struct Vehicle: Codable {
        private let color: String
        private let tires: [Tire]
        private let number: String
    }
    
    struct Tire: Codable {
        private let company: String
        private let isNew: Bool
    }
    
    func parseData(_ data: Data) {
        let decoder = JSONDecoder()
        try? decoder.decode([Vehicle].self, from: data)
    }
    
    func modifiedParsing(_ data: Data) {
        
    }
    
    let data = try String(contentsOfFile: filePath!).data(using: .utf8)
    
    let date = Date()
    let start = date.timeIntervalSince1970
    
    //parseData(data!)
    
    let end = Date().timeIntervalSince1970
    
    print("Time Taken \(end-start)")
    
    /*
     Initial Times: [3.728722095489502, 3.5913820266723633, 3.5568389892578125, 3.534559965133667, 3.506725311279297]
     After Changes
     */
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Rajesh Budhiraja
  • 192
  • 1
  • 12
  • 2
    A single json message can't be divided into multiple messages and decoded as parts in parallel (at least not by the built in decoder) but if you are decoding a file from the main bundle then there is of course nothing stopping you from splitting that file into multiple files that you read and decode in separate threads – Joakim Danielson May 18 '21 at 14:34
  • JSON is an inherently verbose data structure and hence parsing it isn't the most optimal/fast either. Rather than trying to speed up JSON parsing, if you have such a massive amount of data that parsing it as JSON proves problematic, you should switch to another format for storing your data. – Dávid Pásztor May 18 '21 at 14:41
  • @DávidPásztor What would you suggest? API is returning data in MBs so saving locally is not an option. – Rajesh Budhiraja May 18 '21 at 14:42
  • @RajeshBudhiraja this one of the problems pagination was designed to solve. You won't really be able to solve this with a frontend-only change, an API change will definitely be necessary as well. Whether the change is pagination, a new data format (such as protobuf) or maybe both needs to be decided based on the details of said endpoint/response. – Dávid Pásztor May 18 '21 at 14:59
  • Ever used BSON? – Rajesh Budhiraja May 18 '21 at 15:40
  • I would recommend building a custom parser for this. Since you know the exact format of this data, you don't need a general-purpose JSON parser. You can build your own specialized streaming text parser for this specific format. I doubt you need multiple cores; you just need to start parsing as the data comes down. – Rob Napier May 18 '21 at 15:40
  • @RobNapier Can you please share some reference? – Rajesh Budhiraja May 18 '21 at 15:41
  • 1
    If you want some inspiration, I have an example of a general-purpose JSON parser here: https://github.com/rnapier/RNJSON/tree/main/Sources/RNJSON (look in Tokenizer and Parser). That's one's designed to be flexible rather than particularly efficient, but it gives some of the sense of how you can write a parser. If you search for "swift streaming text parser" you'll find a lot of examples of parsers in Swift. Start as simple as possible, get something working, and then you can work on optimizing it. (Expect to rewrite it from scratch a few times; I always do.) – Rob Napier May 18 '21 at 15:44
  • Actually, I was wrong; the Tokenizer still is built for streaming. I thought I'd taken that out. Note the "possiblyTruncated" property. So you might be able to use Tokenizer as a starting point. (Note that RNJSON is currently at a "Rob's amusement" stage of development. I'm not doing much active work on it right now. I'd do contract work on it, but otherwise it's "as-is.") – Rob Napier May 18 '21 at 15:46

1 Answers1

0

I wanted to make JSON Parsing faster. For anyone who is looking for a good solution please refer: https://github.com/bwhiteley/JSONShootout

Marshal is faster than codable.

Rajesh Budhiraja
  • 192
  • 1
  • 12