Skip to content

Krate#moshiPref - why using typeOf instead of class? #31

@mikef-dk

Description

@mikef-dk

Recently we've done some tracing regarding our app start performance. What we noticed is that the very first time typeOf is invoked it takes a long time (~1 second). While we were able to eliminate those calls in our code base, we were noticing that the Moshi extension for Krate is doing it as well:

@OptIn(ExperimentalStdlibApi::class)
public inline fun <reified T : Any> Krate.moshiPref(
    key: String? = null,
): KeyedKratePropertyProvider<T?> {
    return moshiPrefImpl(key, typeOf<T>().javaType)
}

I'm wondering: Is there any good reason to use typeOf<T>().javaType over T::class.java?

Lastly a screenshot of the trace proving the point that typeOf really takes that long:

SCR-20230908-mqwq

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions