⚡ perf(link): skip URL roundtrip when path is already safe#13986
Closed
gaborbernat wants to merge 1 commit into
Closed
⚡ perf(link): skip URL roundtrip when path is already safe#13986gaborbernat wants to merge 1 commit into
gaborbernat wants to merge 1 commit into
Conversation
`_ensure_quoted_url` always pays a `urllib.parse.urlunsplit` plus a `SplitResult._replace` plus a string slice to round-trip every link, even when the input cannot change. For `http(s)://` URLs whose path component contains only characters in `urllib.parse.quote`'s default-safe alphabet, the round-trip rebuilds the input verbatim and the work is wasted. Recognise that case by checking the path against a negative-class regex after the existing `urlsplit` call (which is already cache-warmed by stdlib internals), and return the URL unchanged when scheme is `http(s)` and the path passes. Other URLs (file URLs, paths carrying `:` or whitespace, VCS refs, anything non-ASCII) fall through to the existing logic untouched. A new `test_ensure_quoted_url_idempotent_for_clean_urls` parametrize asserts the fast path is a true identity on the URL shapes Warehouse and common simple-API responses serve. The existing `test_ensure_quoted_url` cases all carry whitespace, `%`, or unsafe path characters and continue to exercise the slow path.
gaborbernat
force-pushed
the
pip-tools-ensure-quoted-url-fast-path
branch
from
May 6, 2026 18:58
5f5d2b0 to
05e2d86
Compare
Member
|
Why are you opening duplicate PRs? |
Author
|
They are targeting different parts of the code 🤔 |
Member
|
Hi, I took a look at link construction with CPython's new sampling profiler, and I found a way to further speed up the fast path. The rewritten patch can be found in PR #14045. |
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
_ensure_quoted_urlalways pays aurllib.parse.urlunsplitplus aSplitResult._replaceplus a string slice to round-trip every link returned by an index, even when the input cannot change. Forhttp(s)://URLs whose path component contains only characters inurllib.parse.quote's default-safe alphabet, the round-trip rebuilds the input verbatim and the work is wasted on every Simple-API response Warehouse hands out. ⚡The patch checks the path component against a negative-class regex after the existing
urlsplitcall (which is already cache-warmed by stdlib's internal_parse_cache) and returns the URL unchanged when the scheme ishttp(s)and the path passes. Other URLs (file://, paths carrying:or whitespace, VCS refs, anything non-ASCII) fall through to the existing logic with output preserved byte-for-byte.Function-level micro-bench against
https://files.pythonhosted.org/packages/12/34/somepackage-1.2.3-py3-none-any.whl#sha256=…:_ensure_quoted_urldrops from ~1064 ns/call to ~70 ns/call, a 16x speedup on the fast path. End-to-end against a cross-platform lock pipeline iterating ~65000 links across 8 resolver passes (n=8 paired runs, alternatingupstream/mainvsupstream/main + this patch): user-CPU mean falls 6.2% (median 150 ms reduction), 8/8 paired runs faster.