Is it just me or is Json.decodeFromBufferedSource ...
# serialization
e
Is it just me or is Json.decodeFromBufferedSource much slower than Json.decodeFromStream or Json.decodeFromString?
I have a use case in my application where I'm serializing/deserializing JSON with compression applied, and thought the new kotlinx-serialization-json-okio artifact would be a nice simplification. but the performance delta was surprising
j
What is your source data?
If you're testing on an in-memory string then it absolutely will be slower
Okio has to UTF-8 encode the string to bytes (hopefully not part of your test measurement) and then it has to decode those bytes back into character data (definitely part of your measurement)
Not to mention all the overhead of shuffling of Segments and position math internally to track the stream.
In comparison to a simple string it absolutely will be slower. A string is the ideal representation on which to decode JSON
Okio is for when you data source is slow enough that you can amortize the cost of JSON deserializaton over it (making it virtually free) and/or for when you cannot fit the raw string contents in memory.
e
the source is a
okio.ByteString
- sometimes from a database and sometimes in memory
I do expect some difference to String, but not 2.5-4x like I'm seeing
j
In both of those cases a string is a much better source for kotlinx.serialization