Skip to content

Commit 769ff09

Browse files
AMDmi3untitaker
authored andcommitted
Minor optimization for url_quote (#1085)
* 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)
1 parent 5791ced commit 769ff09

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

werkzeug/urls.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
((a + b).encode(), int(a + b, 16))
3939
for a in _hexdigits for b in _hexdigits
4040
)
41+
_bytetohex = [
42+
('%%%02X' % char).encode('ascii') for char in range(256)
43+
]
4144

4245

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

477480

0 commit comments

Comments
 (0)