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

Slow creating new instance of OkHttpClient() #4337

Closed
tprochazka opened this issue Oct 24, 2018 · 9 comments
Closed

Slow creating new instance of OkHttpClient() #4337

tprochazka opened this issue Oct 24, 2018 · 9 comments
Labels
performance Performance optimisation

Comments

@tprochazka
Copy link

Creating a new instance of OkHttpClient is very slow because initializing HTTPS support probably. It spends more than 500ms in CipherSuite class. I understand that it is probably important, but why it happens directly when a new instance is created and not during first HTTP request which happens every time on a background thread. It is very unusual, to do such time complex operation directly after the instance is created.

@yschimke
Copy link
Collaborator

Can you show a stacktrace of this? I believe you, but good to discuss with a specific code path.

Ultimately the cost of JVM SSL init + OkHttp init is either on creating first client, or first request. Both have drawbacks, e.g. in some cases apps can be doing startup with a loading screen, but then first request is faster which may be better for users.

@tprochazka
Copy link
Author

Here is trace file https://www.dropbox.com/s/50eupuup8bplks8/okhttp.zip?dl=1

@yschimke
Copy link
Collaborator

yschimke commented Oct 26, 2018

@yschimke
Copy link
Collaborator

yschimke commented Oct 26, 2018

I think there is an easy win here. Use a HashMap, change the static fields from a normal lookup to an (assumed to be unique) insert. Then on lookup, check the happy path first, and on failure, do a secondary lookup s/TLS_/SSL_/ or s/SSL_/TLS_/.

@swankjesse thoughts?

@yschimke yschimke added the performance Performance optimisation label Oct 26, 2018
@yschimke yschimke changed the title Slow creating new instnace of OkHttpClient() Slow creating new instance of OkHttpClient() Oct 26, 2018
@yschimke
Copy link
Collaborator

On a desktop mac, roughly representation numbers

Before: Init Took 2.373407ms
After: Init Took 0.453405ms

@yschimke
Copy link
Collaborator

@tprochazka What device did you profile on?

@yschimke
Copy link
Collaborator

This is still a tiny amount of time relative to the first actual use of the native TLS library, which is roughly 100x more expensive and happens on first request.

@tprochazka
Copy link
Author

The Trace is from Samsung S8+

@swankjesse
Copy link
Collaborator

@yschimke thumbs up on making things faster!

@yschimke yschimke closed this as completed Nov 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance optimisation
Projects
None yet
Development

No branches or pull requests

3 participants