Skip to content

Commit d27591f

Browse files
committed
ld: Replace list of outgoing_txs with a hash table
We had a quadratic check when processing a block, and this just reduces it to be linear. Combined with the previous fix of adding txs multiple times, this should speed up block processing considerably.
1 parent 351b899 commit d27591f

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

lightningd/chaintopology.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,7 @@ static void next_topology_timer(struct chain_topology *topo)
5353
static bool we_broadcast(const struct chain_topology *topo,
5454
const struct bitcoin_txid *txid)
5555
{
56-
const struct outgoing_tx *otx;
57-
58-
list_for_each(&topo->outgoing_txs, otx, list) {
59-
if (bitcoin_txid_eq(&otx->txid, txid))
60-
return true;
61-
}
62-
return false;
56+
return outgoing_tx_map_get(&topo->outgoing_txs, txid) != NULL;
6357
}
6458

6559
static void filter_block_txs(struct chain_topology *topo, struct block *b)
@@ -160,13 +154,16 @@ static void rebroadcast_txs(struct chain_topology *topo)
160154
/* Copy txs now (peers may go away, and they own txs). */
161155
struct txs_to_broadcast *txs;
162156
struct outgoing_tx *otx;
157+
struct outgoing_tx_map_iter it;
163158

164159
txs = tal(topo, struct txs_to_broadcast);
165160
txs->cmd_id = tal_arr(txs, const char *, 0);
166161

167162
/* Put any txs we want to broadcast in ->txs. */
168163
txs->txs = tal_arr(txs, const char *, 0);
169-
list_for_each(&topo->outgoing_txs, otx, list) {
164+
165+
for (otx = outgoing_tx_map_first(&topo->outgoing_txs, &it); otx;
166+
otx = outgoing_tx_map_next(&topo->outgoing_txs, &it)) {
170167
if (wallet_transaction_height(topo->ld->wallet, &otx->txid))
171168
continue;
172169

@@ -180,9 +177,9 @@ static void rebroadcast_txs(struct chain_topology *topo)
180177
broadcast_remainder(topo->bitcoind, true, "", txs);
181178
}
182179

183-
static void destroy_outgoing_tx(struct outgoing_tx *otx)
180+
static void destroy_outgoing_tx(struct outgoing_tx *otx, struct chain_topology *topo)
184181
{
185-
list_del(&otx->list);
182+
outgoing_tx_map_del(&topo->outgoing_txs, otx);
186183
}
187184

188185
static void clear_otx_channel(struct channel *channel, struct outgoing_tx *otx)
@@ -218,8 +215,8 @@ static void broadcast_done(struct bitcoind *bitcoind,
218215
} else {
219216
/* For continual rebroadcasting, until channel freed. */
220217
tal_steal(otx->channel, otx);
221-
list_add_tail(&bitcoind->ld->topology->outgoing_txs, &otx->list);
222-
tal_add_destructor(otx, destroy_outgoing_tx);
218+
outgoing_tx_map_add(&bitcoind->ld->topology->outgoing_txs, notleak(otx));
219+
tal_add_destructor2(otx, destroy_outgoing_tx, bitcoind->ld->topology);
223220
}
224221
}
225222

@@ -941,13 +938,17 @@ u32 feerate_max(struct lightningd *ld, bool *unknown)
941938
static void destroy_chain_topology(struct chain_topology *topo)
942939
{
943940
struct outgoing_tx *otx;
944-
945-
while ((otx = list_pop(&topo->outgoing_txs, struct outgoing_tx, list)))
941+
struct outgoing_tx_map_iter it;
942+
for (otx = outgoing_tx_map_first(&topo->outgoing_txs, &it); otx;
943+
otx = outgoing_tx_map_next(&topo->outgoing_txs, &it)) {
944+
tal_del_destructor2(otx, destroy_outgoing_tx, topo);
946945
tal_free(otx);
946+
}
947947

948948
/* htable uses malloc, so it would leak here */
949949
txwatch_hash_clear(&topo->txwatches);
950950
txowatch_hash_clear(&topo->txowatches);
951+
outgoing_tx_map_clear(&topo->outgoing_txs);
951952
block_map_clear(&topo->block_map);
952953
}
953954

@@ -957,7 +958,7 @@ struct chain_topology *new_topology(struct lightningd *ld, struct log *log)
957958

958959
topo->ld = ld;
959960
block_map_init(&topo->block_map);
960-
list_head_init(&topo->outgoing_txs);
961+
outgoing_tx_map_init(&topo->outgoing_txs);
961962
txwatch_hash_init(&topo->txwatches);
962963
txowatch_hash_init(&topo->txowatches);
963964
topo->log = log;

lightningd/chaintopology.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ struct txwatch;
1818

1919
/* Off topology->outgoing_txs */
2020
struct outgoing_tx {
21-
struct list_node list;
2221
struct channel *channel;
2322
const char *hextx;
2423
struct bitcoin_txid txid;
@@ -117,7 +116,7 @@ struct chain_topology {
117116
struct oneshot *extend_timer, *updatefee_timer;
118117

119118
/* Bitcoin transactions we're broadcasting */
120-
struct list_head outgoing_txs;
119+
struct outgoing_tx_map outgoing_txs;
121120

122121
/* Transactions/txos we are watching. */
123122
struct txwatch_hash txwatches;

0 commit comments

Comments
 (0)