From 417e1bab7d58f05aebb72825063e97b09fb8a6b9 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 8 Apr 2019 14:12:43 +0930 Subject: [PATCH] gossipd: use iterator helpers for iterating node channels. Makes the next step easier. MCP results from 5 runs, min-max(mean +/- stddev): store_load_msec:45791-46917(46330.4+/-3.6e+02) vsz_kb:2641316 store_rewrite_sec:47.040000-48.720000(47.684+/-0.57) listnodes_sec:1.140000-1.340000(1.2+/-0.072) listchannels_sec:50.970000-54.250000(52.698+/-1.3) routing_sec:29.950000-31.010000(30.332+/-0.37) peer_write_all_sec:51.570000-52.970000(52.1+/-0.54) Signed-off-by: Rusty Russell --- gossipd/gossip_store.c | 4 +--- gossipd/gossipd.c | 24 ++++++------------------ gossipd/routing.c | 32 +++++++++++++++++--------------- gossipd/routing.h | 4 ++++ gossipd/test/run-find_route.c | 4 +--- 5 files changed, 29 insertions(+), 39 deletions(-) diff --git a/gossipd/gossip_store.c b/gossipd/gossip_store.c index aec25a4cb..cfd07d9ae 100644 --- a/gossipd/gossip_store.c +++ b/gossipd/gossip_store.c @@ -163,9 +163,7 @@ static bool add_local_unnannounced(int fd, struct chan_map_iter i; struct chan *c; - for (c = chan_map_first(&self->chans, &i); - c; - c = chan_map_next(&self->chans, &i)) { + for (c = first_chan(self, &i); c; c = next_chan(self, &i)) { struct node *peer = other_node(self, c); const u8 *msg; diff --git a/gossipd/gossipd.c b/gossipd/gossipd.c index 6213eda7f..008ff3d92 100644 --- a/gossipd/gossipd.c +++ b/gossipd/gossipd.c @@ -178,9 +178,7 @@ static void peer_disable_channels(struct daemon *daemon, struct node *node) struct chan_map_iter i; struct chan *c; - for (c = chan_map_first(&node->chans, &i); - c; - c = chan_map_next(&node->chans, &i)) { + for (c = first_chan(node, &i); c; c = next_chan(node, &i)) { if (pubkey_eq(&other_node(node, c)->id, &daemon->id)) c->local_disabled = true; } @@ -1805,9 +1803,7 @@ static void gossip_refresh_network(struct daemon *daemon) struct chan_map_iter i; struct chan *c; - for (c = chan_map_first(&n->chans, &i); - c; - c = chan_map_next(&n->chans, &i)) { + for (c = first_chan(n, &i); c; c = next_chan(n, &i)) { struct half_chan *hc = half_chan_from(n, c); if (!is_halfchan_defined(hc)) { @@ -1847,9 +1843,7 @@ static void gossip_disable_local_channels(struct daemon *daemon) if (!local_node) return; - for (c = chan_map_first(&local_node->chans, &i); - c; - c = chan_map_next(&local_node->chans, &i)) + for (c = first_chan(local_node, &i); c; c = next_chan(local_node, &i)) c->local_disabled = true; } @@ -2035,9 +2029,7 @@ static struct io_plan *getchannels_req(struct io_conn *conn, struct chan_map_iter i; struct chan *c; - for (c = chan_map_first(&s->chans, &i); - c; - c = chan_map_next(&s->chans, &i)) { + for (c = first_chan(s, &i); c; c = next_chan(s, &i)) { append_half_channel(&entries, c, !half_chan_to(s, c)); @@ -2179,9 +2171,7 @@ static bool node_has_public_channels(const struct node *peer, struct chan_map_iter i; struct chan *c; - for (c = chan_map_first(&peer->chans, &i); - c; - c = chan_map_next(&peer->chans, &i)) { + for (c = first_chan(peer, &i); c; c = next_chan(peer, &i)) { if (c == exclude) continue; if (is_chan_public(c)) @@ -2232,9 +2222,7 @@ static struct io_plan *get_incoming_channels(struct io_conn *conn, struct chan_map_iter i; struct chan *c; - for (c = chan_map_first(&node->chans, &i); - c; - c = chan_map_next(&node->chans, &i)) { + for (c = first_chan(node, &i); c; c = next_chan(node, &i)) { const struct half_chan *hc; struct route_info ri; diff --git a/gossipd/routing.c b/gossipd/routing.c index 0495b78d4..d6eea40e1 100644 --- a/gossipd/routing.c +++ b/gossipd/routing.c @@ -79,6 +79,16 @@ static struct node_map *empty_node_map(const tal_t *ctx) return map; } +struct chan *first_chan(const struct node *node, struct chan_map_iter *i) +{ + return chan_map_first(&node->chans, i); +} + +struct chan *next_chan(const struct node *node, struct chan_map_iter *i) +{ + return chan_map_next(&node->chans, i); +} + struct routing_state *new_routing_state(const tal_t *ctx, const struct chainparams *chainparams, const struct pubkey *local_id, @@ -137,7 +147,7 @@ static void destroy_node(struct node *node, struct routing_state *rstate) node_map_del(rstate->nodes, node); /* These remove themselves from the map. */ - while ((c = chan_map_first(&node->chans, &i)) != NULL) + while ((c = first_chan(node, &i)) != NULL) tal_free(c); chan_map_clear(&node->chans); } @@ -174,9 +184,7 @@ static bool node_has_public_channels(struct node *node) struct chan_map_iter i; struct chan *c; - for (c = chan_map_first(&node->chans, &i); - c; - c = chan_map_next(&node->chans, &i)) { + for (c = first_chan(node, &i); c; c = next_chan(node, &i)) { if (is_chan_public(c)) return true; } @@ -190,9 +198,7 @@ static bool node_has_broadcastable_channels(struct node *node) struct chan_map_iter i; struct chan *c; - for (c = chan_map_first(&node->chans, &i); - c; - c = chan_map_next(&node->chans, &i)) { + for (c = first_chan(node, &i); c; c = next_chan(node, &i)) { if (!is_chan_public(c)) continue; if (is_halfchan_defined(&c->half[0]) @@ -207,9 +213,7 @@ static bool node_announce_predates_channels(const struct node *node) struct chan_map_iter i; struct chan *c; - for (c = chan_map_first(&node->chans, &i); - c; - c = chan_map_next(&node->chans, &i)) { + for (c = first_chan(node, &i); c; c = next_chan(node, &i)) { if (!is_chan_announced(c)) continue; @@ -538,9 +542,9 @@ find_route(const tal_t *ctx, struct routing_state *rstate, struct chan_map_iter i; struct chan *chan; - for (chan = chan_map_first(&n->chans, &i); + for (chan = first_chan(n, &i); chan; - chan = chan_map_next(&n->chans, &i)) { + chan = next_chan(n, &i)) { int idx = half_chan_to(n, chan); SUPERVERBOSE("Node %s edge %s", @@ -1710,9 +1714,7 @@ void routing_failure(struct routing_state *rstate, type_to_string(tmpctx, struct pubkey, &node->id)); - for (c = chan_map_first(&node->chans, &i); - c; - c = chan_map_next(&node->chans, &i)) { + for (c = first_chan(node, &i); c; c = next_chan(node, &i)) { /* Set it up to be pruned. */ tal_steal(tmpctx, c); } diff --git a/gossipd/routing.h b/gossipd/routing.h index fcc35b6a6..612fe307b 100644 --- a/gossipd/routing.h +++ b/gossipd/routing.h @@ -280,6 +280,10 @@ void handle_pending_cannouncement(struct routing_state *rstate, const struct amount_sat sat, const u8 *txscript); +/* Iterate through channels in a node */ +struct chan *first_chan(const struct node *node, struct chan_map_iter *i); +struct chan *next_chan(const struct node *node, struct chan_map_iter *i); + /* Returns NULL if all OK, otherwise an error for the peer which sent. */ u8 *handle_channel_update(struct routing_state *rstate, const u8 *update TAKES, const char *source); diff --git a/gossipd/test/run-find_route.c b/gossipd/test/run-find_route.c index 6d644c482..53cf69cd1 100644 --- a/gossipd/test/run-find_route.c +++ b/gossipd/test/run-find_route.c @@ -157,9 +157,7 @@ static struct chan *find_channel(struct routing_state *rstate UNUSED, *idx = pubkey_idx(&from->id, &to->id); - for (c = chan_map_first(&to->chans, &i); - c; - c = chan_map_next(&to->chans, &i)) { + for (c = first_chan(to, &i); c; c = next_chan(to, &i)) { if (c->nodes[*idx] == from) return c; }