I'm wondering if there are any plans to support concurrent JSON decoding/encoding in the
JSONEncoder API, such as with a concurrent
DispatchQueue. I'm currently using this with great success in vanilla JSON parsing code (grabbing values out of
[String: Any], ~4x faster parsing on iPhoneX), and think it would be a free performance win for most apps. Speed is the main reason I haven't completely switched over to the new Codable API.
I tried implementing my own
ConcurrentJSONDecoder class that conforms to the Swift
Decoder protocols, but some leaky abstractions gave me trouble so I think this might need to be done at the stdlib-level. For example,
UnkeyedDecodingContainer contains a
currentIndex property which implies synchronous parsing. With modern phones, I don't see why this work can't be spread across the available cores.
To clarify what I mean since the above isn't very clear, I'm trying to write my own
ConcurrentUnkeyedDecodingContainer that conforms to
UnkeyedDecodingContainer, so eventually I can create my own
ConcurrentJSONDecoder and have everything magically work with the
The idea is that
ConcurrentUnkeyedDecodingContainer can parse JSON array values in parallel with a concurrent queue (depending on your CPU cores), and then join the results at the end. The array parsing is still synchronous, it just parses chunks in parallel for faster execution.
It doesn't look like this is possible with the current
UnkeyedDecodingContainer protocol because it has a
currentIndex property, meaning values can't be decoded out of order. I think this is very limiting to decoding implementations and am asking if we can re-think this API a bit.