Repository navigation
gh-163115: Speed up urllib.parse.quote() and quote_plus() for ASCII str inputs - #163116
gatewaytofredom wants to merge 3 commits into
Conversation
…SCII str inputs Bypass the bytes encoding and map(quoter, bs) loop for exact ASCII str inputs using str.rstrip() and str.translate(). Initialize default translation tables lazily, cache custom safe tables via lru_cache, and clear all of them in clear_cache().
|
Misclick. I wanted to say that I believe this complicates the code for something that is usually not the hot path in application. I do not think this is worth it. If users are worried about performance, a 3rd party package written in C is better but this is not the stdlib role here IMO. |
|
Plesse remove the cache stuff so that I can estimate the actual diff. |
|
But unlike the unquote optimization this one seems much more complex. |
|
Removed the globals (STR_QUOTER*) and special-case branches. Mixed ASCII quote() is still 1.60x–1.73x faster and quote_plus() is 1.50x–2.12x faster on my machine. |
| if (type(string) is str and string.isascii() and type(safe) is str and | ||
| (encoding is None or | ||
| (type(encoding) is str and encoding in _ASCII_ENCODINGS)) and | ||
| (errors is None or isinstance(errors, str))): |
There was a problem hiding this comment.
I'll remove that, i was concerned about a bizarre custom class
| (encoding is None or | ||
| (type(encoding) is str and encoding in _ASCII_ENCODINGS)) and |
There was a problem hiding this comment.
this one as well, why checking for encoding being a string? it looks really odd, wa it an LLM suggestion? if so why?
| if isinstance(string, str): | ||
| if not string: | ||
| return string | ||
| if (type(string) is str and string.isascii() and type(safe) is str and |
There was a problem hiding this comment.
I think isinstance checks arr just better
| HTML form values. Plus signs in the original string are escaped unless | ||
| they are included in safe. It also does not have safe default to '/'. | ||
| """ | ||
| if (type(string) is str and string and string.isascii() and |
There was a problem hiding this comment.
this huge check is the sane, why not having a function making the check?
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
|
|
||
| def _quote_ascii(string, safe, encoding, plus=False): | ||
| if (isinstance(string, str) and string.isascii() and | ||
| isinstance(safe, str) and encoding in _ASCII_ENCODINGS): |
There was a problem hiding this comment.
Ah I see that the encoding needs to be ascii. Problem is that encoding can be lowercased, or without dashes. I now wonder whether we really need this as this makes the check more complex.
There was a problem hiding this comment.
I can drop _ASCII_ENCODINGS and just check if encoding is None to cover the default path for quote()/quote_plus()/urlencode(). It would still offer speedups
Bypass the
bytesencoding and per-bytemap(quoter, bs)loop inquote()andquote_plus()for exact ASCIIstrinputs (str.rstrip()when all-safe,str.translate()otherwise). Default translation tables are initialized lazily to avoid import overhead, and all caches are cleared byclear_cache().Note: the three lazy module-level tables (
_STR_QUOTER_*) avoidlru_cachelookup overhead on the defaultsafepaths (~6–15% faster on short mixed strings). We can drop them though and route everything through_str_quoter_table()if you prefer a smaller diff.Benchmarks (
timeit.repeat, min of 7, x86_64 Linux):Benchmark script
mainquote(mixed ASCII path)quote(all-safe ASCII path)quote_plus("hello world foo@bar.com")quote_plus("abcdef123456")urlencode(6-field dict)quote("café /résumé")quote("日本語テスト/パス" * 5)quote_plus("café résumé")quote_plus("café_résumé")quote(b"/api/v1/alice smith")quote_plus(b"hello world")import urllib.parse(self, median of 15)