Repository navigation
fix: performance regression when sniffio not installed - #1103
CedricCabessa wants to merge 1 commit into
Conversation
`sniffio` is optional and lazily imported in `current_async_library`, when absent we use `asyncio` However the cost of import failure is paid every time the function is called. This patch moves the import at module level, like it is already done for `anyio` or `trio` Why it happens now?: Httpcore never explicitly declared `sniffio` but it was installed via `anyio` until they stopped depending on it: agronholm/anyio#1021 Here is a sample to reproduce the issue ``` import asyncio import time import httpcore REQUESTS = 500 RESPONSE = b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok" async def handle(reader, writer): try: while await reader.readuntil(b"\r\n\r\n"): writer.write(RESPONSE) await writer.drain() except (asyncio.IncompleteReadError, ConnectionResetError): pass writer.close() async def main(): try: import sniffio # noqa: F401 status = "INSTALLED" except ImportError: status = "ABSENT" server = await asyncio.start_server(handle, "127.0.0.1", 0) url = f"http://127.0.0.1:{server.sockets[0].getsockname()[1]}/" async with httpcore.AsyncConnectionPool() as pool: await pool.request("GET", url) # warm up the pool cpu = time.process_time() for _ in range(REQUESTS): await pool.request("GET", url) cpu = time.process_time() - cpu print(f"sniffio {status}: {cpu / REQUESTS * 1e6:.0f} us CPU per request") server.close() asyncio.run(main()) ``` On my machine: | httpcore | sniffio | CPU / req | |----------|-----------|-----------| | master | installed | ~330 µs | | master | missing | ~500 µs | | patched | missing | ~330 µs | | patched | installed | ~330 µs |
|
Independent reproduction on Windows, where the regression is larger than in the table above (~2.3x vs ~1.5x). Script from the PR description, REQUESTS=3000, 3 runs each.
Windows 11, CPython 3.13.11, anyio 4.15.1, fresh venv. We also hit this in a FastAPI service on Windows, where it showed up as ~0.11 ms per |
* fix: add sniffio runtime dependency to fix async client CPU regression httpcore retries 'import sniffio' on every lock, event and cancel-shield call when sniffio is absent (encode/httpcore#1103), costing ~17% CPU per request. Declare it explicitly and guard with an acceptance test. Backport of #376 to stable/10. Closes #377 Signed-off-by: Josh Wulf <josh@magikcraft.io> * chore: remove leftover ambient-time lint fixture from runtime Signed-off-by: Josh Wulf <josh@magikcraft.io> --------- Signed-off-by: Josh Wulf <josh@magikcraft.io>
httpcore imports sniffio inside current_async_library, which runs several times per HTTP request. Since anyio dropped its sniffio dependency, the import fails and searches sys.path on every call, about 150 us each. Upstream fix: encode/httpcore#1103
|
Independent confirmation from a different angle. I hit this while profiling an async httpx gateway (httpx 0.28.1, httpcore 1.0.9, anyio 4.14.1, CPython 3.12). |
discussion: #1102
sniffiois optional and lazily imported incurrent_async_library, when absent we useasyncioHowever the cost of import failure is paid every time the function is called.
This patch moves the import at module level, like it is already done for
anyioortrioWhy it happens now?:
Httpcore never explicitly declared
sniffiobut it was installed viaanyiountil they stopped depending on it:agronholm/anyio#1021
Here is a sample to reproduce the issue
On my machine: