perf: Rewrite _assign_requests_to_connections as a single-pass loop#974
Open
mbeijen wants to merge 1 commit into
Open
perf: Rewrite _assign_requests_to_connections as a single-pass loop#974mbeijen wants to merge 1 commit into
_assign_requests_to_connections as a single-pass loop#974mbeijen wants to merge 1 commit into
Conversation
The previous implementation re-scanned `self._connections` per queued request to rebuild `available_connections` and `idle_connections` lists, giving O(N*M) behaviour when many requests are queued against a populated pool. The keepalive-eviction step in pass 1 was also quadratic, since `sum(... is_idle() ...)` was recomputed for every connection. This rewrite: - Walks `self._connections` once to drop closed connections and schedule expired ones for close. - Counts idle connections once, then evicts surplus idle connections in a single pass to enforce `max_keepalive_connections`. - Snapshots `available_connections` once before the queued-request loop, and consults it (rather than re-filtering `self._connections`) for each request. Behaviour is preserved exactly: - The same connection may still be assigned to multiple HTTP/1.1 requests in a single call. The pool's existing `ConnectionNotAvailable` retry loop in `handle_async_request` handles that case as before. - HTTP/2 multiplexing onto a single connection is preserved for the same reason — connections are not removed from `available_connections` on assignment. Inspired by encode/httpcore#1035 by @VictorPrins Co-Authored-By: Victor Prins <32959052+VictorPrins@users.noreply.github.com>
_assign_requests_to_connections as a single-pass loop_assign_requests_to_connections as a single-pass loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The previous implementation re-scanned
self._connectionsper queued request to rebuildavailable_connectionsandidle_connectionslists, giving O(N*M) behaviour when many requests are queued against a populated pool. The keepalive-eviction step in pass 1 was also quadratic, sincesum(... is_idle() ...)was recomputed for every connection.This rewrite:
self._connectionsonce to drop closed connections and schedule expired ones for close.max_keepalive_connections.available_connectionsonce before the queued-request loop, and consults it (rather than re-filteringself._connections) for each request.Behaviour is preserved exactly:
ConnectionNotAvailableretry loop inhandle_async_requesthandles that case as before.available_connectionson assignment.Inspired by encode/httpcore#1035 by @VictorPrins.
Citing some issues related to this, including in downstream libraries relying on
httpcore: