Skip to content

Commit a5536c3

Browse files
committed
core: eliminate some quadratric behaviour from IoLogger
This is the same problem as used to afflict Stream: large input buffers containing many small messages cause intense string copying. This eliminates the worst of it.
1 parent 77564fd commit a5536c3

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

‎mitogen/core.py‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,7 +2405,7 @@ class IoLogger(BasicStream):
24052405
:class:`BasicStream` subclass that sets up redirection of a standard
24062406
UNIX file descriptor back into the Python :mod:`logging` package.
24072407
"""
2408-
_buf = ''
2408+
_trailer = u''
24092409

24102410
def __init__(self, broker, name, dest_fd):
24112411
self._broker = broker
@@ -2427,10 +2427,17 @@ def __init__(self, broker, name, dest_fd):
24272427
def __repr__(self):
24282428
return '<IoLogger %s>' % (self._name,)
24292429

2430-
def _log_lines(self):
2431-
while self._buf.find('\n') != -1:
2432-
line, _, self._buf = str_partition(self._buf, '\n')
2433-
self._log.info('%s', line.rstrip('\n'))
2430+
def _log_lines(self, buf):
2431+
start = 0
2432+
while True:
2433+
nl = min(buf.find('\n', start), start+1024)
2434+
if nl == -1:
2435+
break
2436+
self._log.info('%s', buf[start:nl])
2437+
start = nl + 1
2438+
2439+
if start:
2440+
self._trailer = buf[start:]
24342441

24352442
def on_shutdown(self, broker):
24362443
"""Shut down the write end of the logging socket."""
@@ -2447,8 +2454,7 @@ def on_receive(self, broker):
24472454
if not buf:
24482455
return self.on_disconnect(broker)
24492456

2450-
self._buf += buf.decode('latin1')
2451-
self._log_lines()
2457+
self._log_lines(self._trailer + buf.decode('latin1'))
24522458

24532459

24542460
class Router(object):

0 commit comments

Comments
 (0)