Search code examples
c#asynchronousjil

Can one deserialize a web response as it comes in chunks with Jil without thread blocking?


Deserializing a web response chunk-by-chunk is free as far as wall clock time is concerned, because waits for chunks are typically much longer than deserialization times. However, the existing solution:

var streamReader = new System.IO.StreamReader(responseStream);
var deserialized = Jil.JSON.Deserialize<Result>(streamReader);

blocks the thread until the response is complete, which grinds the system to a halt in high concurrency scenarios. To use Jil with an asynchronous response read, we would need something like

var chunkDeserializer = new JilChunkDeserializer<Response>();    
chunkDeserializer.Consume(buffer, offset, length); //with baked-in string encoding
//chunkDeserializer.Consume(stringChunk); alternative with one extra allocation per chunk

with var deserialised = chunkDeserializer.Finalize() called when all data has been written. Is there something similar in Jil? If not, would it be possible to implement it? Moderate loss of performance would be meaningless due to it being absorbed by the chunk wait times.


Solution

  • This isn't currently possible with Jil, it can only deserialize complete JSON documents.

    Speaking hypothetically: I'm not sure this would actually be any quicker either, because Jil would have to be able to stash its state (to subsequently resume) when it ran out of input. Right now most of that state is on the stack, moving it out to the heap may cost more in runtime than you'd save.