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

transport: cache default ssl context #3473

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

karolinepauls
Copy link

Summary

Creating a client takes 25ms of CPU time. This is a lot. Most of that time is spent initialising the SSL context. Since the context is private anyway, we can cache it. In order to avoid memory problems and privacy issues, only the default context is cached.

Prior discussion, when the problem was left for later (2022): #2298 (comment). Personally, I'm finding solving this problem in httpx to be a comparable amount of work to solving it in applications.

Before:

In [2]: timeit httpx.AsyncClient()
26.6 ms ± 364 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [3]: timeit httpx.Client()
26.8 ms ± 268 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

After:

In [2]: timeit httpx.AsyncClient()
46.2 µs ± 224 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

In [3]: timeit httpx.Client()
46.5 µs ± 1.14 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

CPU Profile (only before):
image

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

@karolinepauls karolinepauls force-pushed the transport-cache-default-ssl-context branch from c25cff0 to 684e579 Compare January 15, 2025 02:35
@karolinepauls karolinepauls force-pushed the transport-cache-default-ssl-context branch from 684e579 to 19afa7a Compare January 15, 2025 02:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant