lightningd: use hash map for peers instead of linked list.

After connecting 100,000 peers with one channel each (not all at
once!), we see various places where we exhibit O(N^2) behaviour.

Fix these by keeping a map of id->peer instead of a simple
linked-list.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2023-01-18 15:34:32 +10:30
committed by Alex Myers
parent 17aa047b17
commit cfa632b0e9
11 changed files with 143 additions and 51 deletions

View File

@@ -15,10 +15,7 @@ struct peer_fd;
struct wally_psbt;
struct peer {
/* Inside ld->peers. */
struct list_node list;
/* Master context */
/* Master context (we're in the hashtable ld->peers) */
struct lightningd *ld;
/* Database ID of the peer */
@@ -143,4 +140,20 @@ command_find_channel(struct command *cmd,
/* Ancient (0.7.0 and before) releases could create invalid commitment txs! */
bool invalid_last_tx(const struct bitcoin_tx *tx);
static const struct node_id *peer_node_id(const struct peer *peer)
{
return &peer->id;
}
static bool peer_node_id_eq(const struct peer *peer,
const struct node_id *node_id)
{
return node_id_eq(&peer->id, node_id);
}
/* Defines struct peer_node_id_map */
HTABLE_DEFINE_TYPE(struct peer,
peer_node_id, node_id_hash, peer_node_id_eq,
peer_node_id_map);
#endif /* LIGHTNING_LIGHTNINGD_PEER_CONTROL_H */