Add streaming deserialization support for kotlinx.serialization.json#4166
Add streaming deserialization support for kotlinx.serialization.json#41660neel wants to merge 7 commits intosquare:trunkfrom
Conversation
| @ExperimentalSerializationApi | ||
| class SerializerFromJson(override val format: Json) : Serializer() { | ||
| override fun <T> fromResponseBody(loader: DeserializationStrategy<T>, body: ResponseBody): T { | ||
| val stream = body.byteStream() |
There was a problem hiding this comment.
This incurs a lot of copies. It's probably better to depend on the Okio version of the json artifact and use the Okio-based steaming APIs.
There was a problem hiding this comment.
Migrated to the Okio-base APIs. Could you please re-check?
| import okhttp3.ResponseBody | ||
|
|
||
| internal sealed class Serializer { | ||
| abstract class Serializer { |
There was a problem hiding this comment.
Keep this and Factory internal to this module. We don't need to share anything—the implementation isn't very big and we don't need as much indirection.
There was a problem hiding this comment.
Ok, created module-specific Factory and converters implementations.
80bf187 to
0941c81
Compare
|
@JakeWharton is there anything else I can do to proceed with the PR? |
|
Sorry I haven't forgotten about this. I was waffling on what I wanted to do based on this comment: Kotlin/kotlinx.serialization#253 (comment). Specifically,
|
|
I guess in that case we can simply deprecate this converter and add the streaming to the normal one, assuming the APIs make themselves available on the high-level "format"-suffixed types... |
Sounds reasonable. Until then we could use this Json-specific implementation. |
|
Yep I'll get it in here soon. Probably Monday |
|
Any news? This hasn't been merged, nor is the streaming API in the regular KotlinX Serialization Converter. |
|
@JakeWharton is there plans to merge this? |
|
The Okio-based API of Kotlinx Serialization has terrible performance (more than 2 times slower than String last time I checked) because it basically decodes one codepoint at a time; I would advise not to use it and use the java |
|
Streaming is always slower than buffered, yes. The point of using the streaming APIs is that you get to amortize the cost of serialization and deserialization across the cost of the network transfer, which is orders of magnitude slower. So instead of paying 100ms to serialize and 1000ms to transfer you effectively only pay 1000ms with streaming. Whether serialization took 100ms, 200ms, or 500ms becomes irrelevant because the limiting factor is always the network. |
|
Why not pay even less, especially when reading the response back from the disk cache? |
|
Okio costs less than |
It costs less when performing comparable operations. But last time I checked, it takes much more CPU time to decode chars one by one from a See related issue in the Kotlin Serialization project: Kotlin/kotlinx.serialization#2743 |
|
Is this becoming relevant with the new version taking a |
|
@JakeWharton do you guys plan on mergin this PR? |
Follows up JakeWharton/retrofit2-kotlinx-serialization-converter#43 by introducing a new ConverterFactory specific for the Json format.
It uses Okio-based streaming API to deserialize responses.