Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init of Adapter at "runtime" takes more time than actual parsing. #1570

Open
ArchanaPrabhu opened this issue Aug 26, 2022 · 2 comments
Open
Labels

Comments

@ArchanaPrabhu
Copy link

I am aware that in order to use the generated JsonAdapters without "reflection", we need to create a JsonAdaptor.Factory that maps the <Type, JsonAdapter>.
(If we do not use a factory to provide, Moshi internally uses reflection to find these generated classes -> "Class.forName()", which can add to the latency)

In order to finally parse a string, we need to get the JsonAdapter from the Moshi instance and call fromJson.

Observations
It takes a long time to create the JsonAdapter for the very first time. I could see that JsonReader$Options.of is taking considerable time.

Screenshot 2022-08-26 at 10 20 31 AM

My metrics after migration to Moshi looks like this :
Time taken to create adapter : 100 ms
Time taken to parse : 30 ms

(Previous code) time taken by GSON : 70 ms

Since 130 ms > 70 ms, I do not actually see the gains here.
I understand that the cost of adapter creation is one time, but I am optimizing for app launch.

I do not see anyway to reduce the adapter creation time as well. I think we should consider optimizing adapter creation latency.

@ZacSweers
Copy link
Collaborator

This needs more information

  • What version of Moshi?
  • How is the gson adapter written?
  • What's a graph of gson look like?

In general, if startup is this critical to your path, you should likely write your own custom adapters that are optimized for the data coming in. For example, if you only have a couple of keys, it could be not worth the up-front tradeoff to use okio.Options like this chart is doing.

@ArchanaPrabhu
Copy link
Author

ArchanaPrabhu commented Aug 27, 2022

Thanks for responding.

  1. Moshi Version : 1.13.0
  2. I have not used any adapter with GSON, so it parses by reflection.
  3. GSON uses reflection, trace for the same.

Screenshot 2022-08-27 at 9 38 38 AM

Yes, then I guess custom adapters are the way to go here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants