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
5df8a8a1fd
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user