Skip to content

Commit

Permalink
Another micro-optimization.
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonspeed committed Jan 22, 2024
1 parent 837d445 commit c06c9fd
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/twisted/protocols/tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ def __init__(self, write: Callable[[bytes], object], clock: IReactorTime):
self._write = write
self._clock = clock
self._buffer: list[bytes] = []
self._bufferLen = 0
self._bufferLeft = self.MAX_BUFFER_SIZE
self._scheduled: Optional[IDelayedCall] = None

def write(self, data: bytes) -> None:
Expand All @@ -719,9 +719,9 @@ def write(self, data: bytes) -> None:
Accumulating too much data can result in higher memory usage.
"""
self._buffer.append(data)
self._bufferLen += len(data)
self._bufferLeft -= len(data)

if self._bufferLen > self.MAX_BUFFER_SIZE:
if self._bufferLeft < 0:
# We've accumulated enough we should just write it out. No need to
# schedule a flush, since we just flushed everything.
self.flush()
Expand All @@ -744,7 +744,7 @@ def _scheduledFlush(self) -> None:
def flush(self) -> None:
"""Flush any buffered writes."""
if self._buffer:
self._bufferLen = 0
self._bufferLeft = self.MAX_BUFFER_SIZE
self._write(b"".join(self._buffer))
del self._buffer[:]

Expand Down

0 comments on commit c06c9fd

Please sign in to comment.