From 03d9b29eeecc102ffa610f4b35cd8e47d5357f45 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Sun, 21 Mar 2021 22:19:37 +0100 Subject: [PATCH] lntransport: optimise read_messages implementation Not great to use a 'bytes' object as a FIFO buffer, as every slice copies the whole thing. With bytearray, extending it from the right is fast, and with the correct syntax, popping from the left is fast too. see https://stackoverflow.com/a/57748513 https://bugs.python.org/issue19087 https://github.com/python/cpython/commit/5df8a8a1fd6cc6f4469dc7d3994d06e2aea24c52 --- electrum/lntransport.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/electrum/lntransport.py b/electrum/lntransport.py index f3c111112..72a5f172c 100644 --- a/electrum/lntransport.py +++ b/electrum/lntransport.py @@ -105,19 +105,19 @@ class LNTransportBase: self.writer.write(lc+c) async def read_messages(self): - read_buffer = b'' + buffer = bytearray() while True: rn_l, rk_l = self.rn() rn_m, rk_m = self.rn() while True: - if len(read_buffer) >= 18: - lc = read_buffer[:18] + if len(buffer) >= 18: + lc = bytes(buffer[:18]) l = aead_decrypt(rk_l, rn_l, b'', lc) length = int.from_bytes(l, 'big') offset = 18 + length + 16 - if len(read_buffer) >= offset: - c = read_buffer[18:offset] - read_buffer = read_buffer[offset:] + if len(buffer) >= offset: + c = bytes(buffer[18:offset]) + del buffer[:offset] # much faster than: buffer=buffer[offset:] msg = aead_decrypt(rk_m, rn_m, b'', c) yield msg break @@ -129,7 +129,7 @@ class LNTransportBase: s = None if not s: raise LightningPeerConnectionClosed() - read_buffer += s + buffer += s def rn(self): o = self._rn, self.rk