Reduce memory footprint
- Use tuples in preference to lists They are always at least 11% smaller memory footprint, rising to 35% for short arrays
This commit is contained in:
parent
7485d0aaf0
commit
316f642a08
@ -118,7 +118,7 @@ class MemPool(object):
|
|||||||
unspent.difference_update(tx.prevouts)
|
unspent.difference_update(tx.prevouts)
|
||||||
|
|
||||||
# Save the in_pairs, compute the fee, and accept the TX
|
# Save the in_pairs, compute the fee, and accept the TX
|
||||||
tx.in_pairs = in_pairs
|
tx.in_pairs = tuple(in_pairs)
|
||||||
tx.fee = (sum(v for hashX, v in tx.in_pairs) -
|
tx.fee = (sum(v for hashX, v in tx.in_pairs) -
|
||||||
sum(v for hashX, v in tx.out_pairs))
|
sum(v for hashX, v in tx.out_pairs))
|
||||||
txs[hash] = tx
|
txs[hash] = tx
|
||||||
@ -198,13 +198,12 @@ class MemPool(object):
|
|||||||
|
|
||||||
async def _fetch_and_accept(self, hashes, all_hashes, touched):
|
async def _fetch_and_accept(self, hashes, all_hashes, touched):
|
||||||
'''Fetch a list of mempool transactions.'''
|
'''Fetch a list of mempool transactions.'''
|
||||||
hex_hashes = [hash_to_hex_str(hash) for hash in hashes]
|
hex_hashes_iter = (hash_to_hex_str(hash) for hash in hashes)
|
||||||
raw_txs = await self.daemon.getrawtransactions(hex_hashes)
|
raw_txs = await self.daemon.getrawtransactions(hex_hashes_iter)
|
||||||
count = len([raw_tx for raw_tx in raw_txs if raw_tx])
|
|
||||||
|
|
||||||
def deserialize_txs():
|
def deserialize_txs():
|
||||||
# This function is pure
|
# This function is pure
|
||||||
script_hashX = self.coin.hashX_from_script
|
to_hashX = self.coin.hashX_from_script
|
||||||
deserializer = self.coin.DESERIALIZER
|
deserializer = self.coin.DESERIALIZER
|
||||||
|
|
||||||
txs = {}
|
txs = {}
|
||||||
@ -216,12 +215,12 @@ class MemPool(object):
|
|||||||
tx, tx_size = deserializer(raw_tx).read_tx_and_vsize()
|
tx, tx_size = deserializer(raw_tx).read_tx_and_vsize()
|
||||||
|
|
||||||
# Convert the tx outputs into (hashX, value) pairs
|
# Convert the tx outputs into (hashX, value) pairs
|
||||||
txout_pairs = [(script_hashX(txout.pk_script), txout.value)
|
txout_pairs = tuple((to_hashX(txout.pk_script), txout.value)
|
||||||
for txout in tx.outputs]
|
for txout in tx.outputs)
|
||||||
|
|
||||||
# Convert the tx inputs to (prev_hash, prev_idx) pairs
|
# Convert the tx inputs to (prev_hash, prev_idx) pairs
|
||||||
txin_pairs = [(txin.prev_hash, txin.prev_idx)
|
txin_pairs = tuple((txin.prev_hash, txin.prev_idx)
|
||||||
for txin in tx.inputs]
|
for txin in tx.inputs)
|
||||||
|
|
||||||
txs[hash] = MemPoolTx(txin_pairs, None, txout_pairs,
|
txs[hash] = MemPoolTx(txin_pairs, None, txout_pairs,
|
||||||
0, tx_size)
|
0, tx_size)
|
||||||
@ -233,13 +232,12 @@ class MemPool(object):
|
|||||||
# Determine all prevouts not in the mempool, and fetch the
|
# Determine all prevouts not in the mempool, and fetch the
|
||||||
# UTXO information from the database. Failed prevout lookups
|
# UTXO information from the database. Failed prevout lookups
|
||||||
# return None - concurrent database updates happen
|
# return None - concurrent database updates happen
|
||||||
prevouts = [prevout for tx in tx_map.values()
|
prevouts = tuple(prevout for tx in tx_map.values()
|
||||||
for prevout in tx.prevouts
|
for prevout in tx.prevouts
|
||||||
if prevout[0] not in all_hashes]
|
if prevout[0] not in all_hashes)
|
||||||
utxos = await self.lookup_utxos(prevouts)
|
utxos = await self.lookup_utxos(prevouts)
|
||||||
utxo_map = {prevout: utxo for prevout, utxo in zip(prevouts, utxos)}
|
utxo_map = {prevout: utxo for prevout, utxo in zip(prevouts, utxos)}
|
||||||
|
|
||||||
# Attempt to complete processing of txs
|
|
||||||
return self._accept_transactions(tx_map, utxo_map, touched)
|
return self._accept_transactions(tx_map, utxo_map, touched)
|
||||||
|
|
||||||
# External interface
|
# External interface
|
||||||
@ -282,7 +280,7 @@ class MemPool(object):
|
|||||||
None, some or all of these may be spends of the hashX.
|
None, some or all of these may be spends of the hashX.
|
||||||
'''
|
'''
|
||||||
result = set()
|
result = set()
|
||||||
for tx_hash in self.hashXs.get(hashX, []):
|
for tx_hash in self.hashXs.get(hashX, ()):
|
||||||
tx = self.txs[tx_hash]
|
tx = self.txs[tx_hash]
|
||||||
result.update(tx.prevouts)
|
result.update(tx.prevouts)
|
||||||
return result
|
return result
|
||||||
@ -295,7 +293,7 @@ class MemPool(object):
|
|||||||
'''
|
'''
|
||||||
# hashXs is a defaultdict, so use get() to query
|
# hashXs is a defaultdict, so use get() to query
|
||||||
result = []
|
result = []
|
||||||
for tx_hash in self.hashXs.get(hashX, []):
|
for tx_hash in self.hashXs.get(hashX, ()):
|
||||||
tx = self.txs[tx_hash]
|
tx = self.txs[tx_hash]
|
||||||
unconfirmed = any(prev_hash in self.txs
|
unconfirmed = any(prev_hash in self.txs
|
||||||
for prev_hash, prev_idx in tx.prevouts)
|
for prev_hash, prev_idx in tx.prevouts)
|
||||||
@ -311,7 +309,7 @@ class MemPool(object):
|
|||||||
'''
|
'''
|
||||||
utxos = []
|
utxos = []
|
||||||
# hashXs is a defaultdict, so use get() to query
|
# hashXs is a defaultdict, so use get() to query
|
||||||
for tx_hash in self.hashXs.get(hashX, []):
|
for tx_hash in self.hashXs.get(hashX, ()):
|
||||||
tx = self.txs.get(tx_hash)
|
tx = self.txs.get(tx_hash)
|
||||||
if not tx:
|
if not tx:
|
||||||
continue
|
continue
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user