Make syntax highlighting for XML lazy, to only call pygments when debug logs are enabled.

Makes poezio about 11% faster when sending/receiving messages.
This commit is contained in:
Emmanuel Gil Peyrot 2015-05-06 13:03:47 +02:00
parent b8d7b9520c
commit 116a33ba51
2 changed files with 11 additions and 4 deletions

View file

@ -169,7 +169,14 @@ def _get_highlight():
LEXER = get_lexer_by_name('xml') LEXER = get_lexer_by_name('xml')
FORMATTER = Terminal256Formatter() FORMATTER = Terminal256Formatter()
return lambda x: highlight(x, LEXER, FORMATTER) class Highlighter:
__slots__ = ['string']
def __init__(self, string):
self.string = string
def __str__(self):
return highlight(str(self.string).strip(), LEXER, FORMATTER)
return Highlighter
except ImportError: except ImportError:
return lambda x: x return lambda x: x

View file

@ -354,7 +354,7 @@ class XMLStream(asyncio.BaseProtocol):
log.debug('RECV: %s', highlight(tostring(self.xml_root, xmlns=self.default_ns, log.debug('RECV: %s', highlight(tostring(self.xml_root, xmlns=self.default_ns,
stream=self, stream=self,
top_level=True, top_level=True,
open_only=True)).strip()) open_only=True)))
self.start_stream_handler(self.xml_root) self.start_stream_handler(self.xml_root)
self.xml_depth += 1 self.xml_depth += 1
if event == 'end': if event == 'end':
@ -845,7 +845,7 @@ class XMLStream(asyncio.BaseProtocol):
:param string data: Any bytes or utf-8 string value. :param string data: Any bytes or utf-8 string value.
""" """
log.debug("SEND: %s", highlight(data).strip()) log.debug("SEND: %s", highlight(data))
if not self.transport: if not self.transport:
raise NotConnectedError() raise NotConnectedError()
if isinstance(data, str): if isinstance(data, str):
@ -898,7 +898,7 @@ class XMLStream(asyncio.BaseProtocol):
if stanza is None: if stanza is None:
return return
log.debug("RECV: %s", highlight(str(stanza)).strip()) log.debug("RECV: %s", highlight(stanza))
# Match the stanza against registered handlers. Handlers marked # Match the stanza against registered handlers. Handlers marked
# to run "in stream" will be executed immediately; the rest will # to run "in stream" will be executed immediately; the rest will