Skip to content

⚡ perf(link): skip URL roundtrip when path is already safe#13986

Closed
gaborbernat wants to merge 1 commit into
pypa:mainfrom
gaborbernat:pip-tools-ensure-quoted-url-fast-path
Closed

⚡ perf(link): skip URL roundtrip when path is already safe#13986
gaborbernat wants to merge 1 commit into
pypa:mainfrom
gaborbernat:pip-tools-ensure-quoted-url-fast-path

Conversation

@gaborbernat

@gaborbernat gaborbernat commented May 6, 2026

Copy link
Copy Markdown

_ensure_quoted_url always pays a urllib.parse.urlunsplit plus a SplitResult._replace plus a string slice to round-trip every link returned by an index, 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 on every Simple-API response Warehouse hands out. ⚡

The patch checks the path component against a negative-class regex after the existing urlsplit call (which is already cache-warmed by stdlib's internal _parse_cache) and returns the URL unchanged when the scheme is http(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_url drops 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, alternating upstream/main vs upstream/main + this patch): user-CPU mean falls 6.2% (median 150 ms reduction), 8/8 paired runs faster.

`_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
gaborbernat force-pushed the pip-tools-ensure-quoted-url-fast-path branch from 5f5d2b0 to 05e2d86 Compare May 6, 2026 18:58
@notatallshaw

Copy link
Copy Markdown
Member

Why are you opening duplicate PRs?

@gaborbernat

Copy link
Copy Markdown
Author

They are targeting different parts of the code 🤔

@ichard26

Copy link
Copy Markdown
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.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jun 26, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants