Skip to content

Commit

Permalink
Minor optimization for url_quote (#1085)
Browse files Browse the repository at this point in the history
* Minor optimization for url_quote

Since we're working with bytes here anyway, there's no need to use
strings and encode. This yields ~9% performance gain with python3 and
~23% performance gain with python2.

* Further improve url_quote performance

This variant fixes compatibility with python <=3.4 and yields
additional performance improvement (total 50% with python 2, 22% with python 3)
  • Loading branch information
AMDmi3 authored and untitaker committed Mar 22, 2017
1 parent 5791ced commit 769ff09
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion werkzeug/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
((a + b).encode(), int(a + b, 16))
for a in _hexdigits for b in _hexdigits
)
_bytetohex = [
('%%%02X' % char).encode('ascii') for char in range(256)
]


_URLTuple = fix_tuple_repr(namedtuple(
Expand Down Expand Up @@ -471,7 +474,7 @@ def url_quote(string, charset='utf-8', errors='strict', safe='/:', unsafe=''):
if char in safe:
rv.append(char)
else:
rv.extend(('%%%02X' % char).encode('ascii'))
rv.extend(_bytetohex[char])
return to_native(bytes(rv))


Expand Down

0 comments on commit 769ff09

Please sign in to comment.