diff --git a/electrum/transaction.py b/electrum/transaction.py index cca06ff189db..b50e6c6b8192 100644 --- a/electrum/transaction.py +++ b/electrum/transaction.py @@ -325,7 +325,7 @@ class TxInput: _is_coinbase_output: bool def __init__(self, *, - prevout: TxOutpoint, + prevout: 'TxOutpoint', script_sig: bytes = None, nsequence: int = 0xffffffff - 1, witness: bytes = None, @@ -449,7 +449,13 @@ def witness_elements(self) -> Sequence[bytes]: vds = BCDataStream() vds.write(self.witness) n = vds.read_compact_size() - return list(vds.read_bytes(vds.read_compact_size()) for i in range(n)) + # OPTIMIZATION: Pre-allocate the result list to avoid list resizing, and avoid repeated attribute access + result = [None] * n + read_bytes = vds.read_bytes + read_compact_size = vds.read_compact_size + for i in range(n): + result[i] = read_bytes(read_compact_size()) + return result def is_segwit(self, *, guess_for_address=False) -> bool: if self.witness not in (b'\x00', b'', None): @@ -625,9 +631,11 @@ def read_uint64(self): return self._read_num('