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

@@ -999,7 +999,7 @@ static struct channel *add_peer(struct lightningd *ld, int n,
memset(&peer->id, n, sizeof(peer->id));
list_head_init(&peer->channels);
list_add_tail(&ld->peers, &peer->list);
peer_node_id_map_add(ld->peers, peer);
peer->ld = ld;
c->state = state;
@@ -1036,7 +1036,8 @@ int main(int argc, char *argv[])
common_setup(argv[0]);
ld = tal(tmpctx, struct lightningd);
list_head_init(&ld->peers);
ld->peers = tal(ld, struct peer_node_id_map);
peer_node_id_map_init(ld->peers);
ld->htlcs_in = tal(ld, struct htlc_in_map);
htlc_in_map_init(ld->htlcs_in);
chainparams = chainparams_for_network("regtest");