Marcello Galhardo
04/28/2025, 8:29 PMserializer<T>()
that throws if no serializer is found, why isn’t there a serializerOrNull<T>()
as well? I know there’s a serializerOrNull(KType)
, but as I understand it, typeOf<T>()
relies on reflection and has performance implications.Marcello Galhardo
04/28/2025, 8:31 PMephemient
04/29/2025, 12:39 AMtypeOf<T>()
is an intrinsic, what performance implications?ephemient
04/29/2025, 12:42 AMkotlin.reflect
implementation objects but it isn't runtime reflectionephemient
04/29/2025, 12:45 AMeneim
07/14/2025, 2:56 AMtypeOf
. In my observation, for the very first cold start,
// Mimicing the Json.decodeFromStream(InputStream) extension, but using MyClass::class.java instead.
json.decodeFromStream(
deserializer = json.serializersModule.serializer(MyClass::class.java),
stream = dataInputStream,
)
is way faster than
// Same as the Json.decodeFromStream(InputStream) extension
json.decodeFromStream(
deserializer = json.serializersModule.serializer(typeOf<MyClass>()).cast(),
stream = dataInputStream,
)
(I'm building Android app and using macro-benchmark to get test this).
I understand the result of typeOf<MyClass>()
is cached, but for the critical cold start it is still significant (for one class, it is <2ms versus >=20ms or slower in old devices).
Beside the multi-platform nature, is there any other advantages of the later that I should know of? I'm considering to use the serializer(T::class.java)
for my Android-only application but it could be great to understand if this is not a over-optimization.