Browse Source

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
patch-4
SomberNight 4 years ago
parent
commit
03d9b29eee
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 14
      electrum/lntransport.py

14
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

Loading…
Cancel
Save