From: Ben Darnell Date: Thu, 28 Jul 2011 05:03:50 +0000 (-0700) Subject: Speed up xhtml_escape (template benchmark goes from 49ms to 32ms) X-Git-Tag: v2.1.0~65 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fa5b0f26f1d8bdc83e01da04546c9fe59bf87b7b;p=thirdparty%2Ftornado.git Speed up xhtml_escape (template benchmark goes from 49ms to 32ms) --- diff --git a/tornado/escape.py b/tornado/escape.py index 089f6d485..4010b1c92 100644 --- a/tornado/escape.py +++ b/tornado/escape.py @@ -23,7 +23,6 @@ have crept in over time. import htmlentitydefs import re import sys -import xml.sax.saxutils import urllib # Python3 compatibility: On python2.5, introduce the bytes alias from 2.6 @@ -61,9 +60,12 @@ except Exception: _json_encode = _json_decode +_XHTML_ESCAPE_RE = re.compile('[&<>"]') +_XHTML_ESCAPE_DICT = {'&': '&', '<': '<', '>': '>', '"': '"'} def xhtml_escape(value): """Escapes a string so it is valid within XML or XHTML.""" - return xml.sax.saxutils.escape(to_basestring(value), {'"': """}) + return _XHTML_ESCAPE_RE.sub(lambda match: _XHTML_ESCAPE_DICT[match.group(0)], + to_basestring(value)) def xhtml_unescape(value):