Skip to content

gh-160690: Speed up urllib.parse.unquote() for ASCII input - #160691

Open
f4hy wants to merge 3 commits into
python:mainfrom
f4hy:unquote-ascii
Open

f4hy wants to merge 3 commits into
python:mainfrom
f4hy:unquote-ascii

Conversation

@f4hy

@f4hy f4hy commented Oct 26, 2026 •

Copy link
Copy Markdown

For pure ASCII input, we can skip the regex/generator path in unquote() and decode the result of _unquote_impl() directly. The regex would match the whole string as a single chunk anyway, so the result is identical.

Benchmarks are in the issue but up to 2x faster in the micro-benchmark for the common short ascii input.

Added a test checking that the fast path and the general path decode escapes the same way.

Resolves #160690

@python-cla-bot

python-cla-bot Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Oct 26, 2026
Comment thread Lib/urllib/parse.py
Comment on lines +856 to +857
if string.isascii():
return _unquote_impl(string).decode(encoding, errors)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if string.isascii():
return _unquote_impl(string).decode(encoding, errors)
if encoding in ('utf-8', 'utf8'):
try:
data = string.encode('utf-8')
except UnicodeEncodeError:
pass # lone surrogates must be passed through verbatim
else:
return _unquote_impl(data).decode(encoding, errors)

The fast path introduced by the PR looks good to me. I am not sure whether ascii only covers the most common / important cases. The suggestion here would cover more cases and also gives a speedup.

(and the two fast paths can also be combined)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Speed up urllib.parse.unquote() for ASCII input

2 participants