-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 While profiling a page full of dynamically-generated links, I was interested to see Python's 'urllib._fast_quote' show up quite high in the total time list. After looking at it, I have found a pretty huge speed win available, posted to Python's SF tracker as bug #1285086:
https://sourceforge.net/tracker/index.php?func=detail&aid=1285086&group_id=5470&atid=105470 Zope makes *heavy* use of urllib.quote (quoting each element of the path in 'absolute_url', for instance), which makes it a particularly interesting speedup for us. I'm attaching the speed test and the patch, for consideration by the Zope development community: if this is enough of a win, we might consider including a patched urllib in Zope2/Zope3 (or monkey-patching urllib.quote). Tres. - -- =================================================================== Tres Seaver +1 202-558-7113 [EMAIL PROTECTED] Palladion Software "Excellence by Design" http://palladion.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.5 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFDIG9X+gerLs4ltQ4RArBaAJwIWac5xvHjmQg2S3oXXIFWzrecjACghGuw 5NnQQGNw55JDWjrzRyCEitA= =iIpp -----END PGP SIGNATURE-----
urllib_fast_quote_speed_test.py
Description: application/httpd-cgi
*** urllib.py.org 2005-09-08 11:08:59.866913960 -0400
--- urllib.py 2005-09-08 12:27:19.054528856 -0400
***************
*** 27,32 ****
--- 27,33 ----
import os
import time
import sys
+ import re
__all__ = ["urlopen", "URLopener", "FancyURLopener", "urlretrieve",
"urlcleanup", "quote", "quote_plus", "unquote", "unquote_plus",
***************
*** 1112,1131 ****
'0123456789' '_.-')
_fast_safe_test = always_safe + '/'
! _fast_safe = None
! def _fast_quote(s):
! global _fast_safe
! if _fast_safe is None:
! _fast_safe = {}
! for c in _fast_safe_test:
! _fast_safe[c] = c
! res = list(s)
! for i in range(len(res)):
! c = res[i]
! if not c in _fast_safe:
! res[i] = '%%%02X' % ord(c)
! return ''.join(res)
def quote(s, safe = '/'):
"""quote('abc def') -> 'abc%20def'
--- 1113,1124 ----
'0123456789' '_.-')
_fast_safe_test = always_safe + '/'
! _fast_safe = dict(zip(_fast_safe_test, _fast_safe_test))
! _must_quote = re.compile(r'[^%s]' % _fast_safe_test)
! for c in [chr(i) for i in range(256)]:
! if c not in _fast_safe:
! _fast_safe[c] = '%%%02X' % ord(c)
def quote(s, safe = '/'):
"""quote('abc def') -> 'abc%20def'
***************
*** 1148,1156 ****
called on a path where the existing slash characters are used as
reserved characters.
"""
safe = always_safe + safe
- if _fast_safe_test == safe:
- return _fast_quote(s)
res = list(s)
for i in range(len(res)):
c = res[i]
--- 1141,1151 ----
called on a path where the existing slash characters are used as
reserved characters.
"""
+ if safe == '/': # optimize usual case
+ return (s and (not _must_quote.search(s)
+ and s or ''.join(map(_fast_safe.get, s))
+ ))
safe = always_safe + safe
res = list(s)
for i in range(len(res)):
c = res[i]
_______________________________________________ Zope-Dev maillist - [email protected] http://mail.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope )
