From 62918fcb3bca08b44c364a617d5702c6028231e4 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 10 Apr 2019 17:01:18 +0930 Subject: [PATCH] gossip_store: avoid gratuitous copy on load. Doesn't make measurable difference, but an obvious optimization. Signed-off-by: Rusty Russell --- gossipd/gossip_store.c | 8 +++++--- gossipd/routing.c | 12 ++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/gossipd/gossip_store.c b/gossipd/gossip_store.c index 601ce9960..b37a9be78 100644 --- a/gossipd/gossip_store.c +++ b/gossipd/gossip_store.c @@ -327,7 +327,7 @@ void gossip_store_load(struct routing_state *rstate, struct gossip_store *gs) &gossip_msg, &satoshis)) { if (!routing_add_channel_announcement(rstate, - gossip_msg, + take(gossip_msg), satoshis)) { bad = "Bad channel_announcement"; goto truncate; @@ -335,14 +335,16 @@ void gossip_store_load(struct routing_state *rstate, struct gossip_store *gs) stats[0]++; } else if (fromwire_gossip_store_channel_update(msg, msg, &gossip_msg)) { - if (!routing_add_channel_update(rstate, gossip_msg)) { + if (!routing_add_channel_update(rstate, + take(gossip_msg))) { bad = "Bad channel_update"; goto truncate; } stats[1]++; } else if (fromwire_gossip_store_node_announcement(msg, msg, &gossip_msg)) { - if (!routing_add_node_announcement(rstate, gossip_msg)) { + if (!routing_add_node_announcement(rstate, + take(gossip_msg))) { bad = "Bad node_announcement"; goto truncate; } diff --git a/gossipd/routing.c b/gossipd/routing.c index 79c8f374c..8f2ee3589 100644 --- a/gossipd/routing.c +++ b/gossipd/routing.c @@ -889,6 +889,10 @@ bool routing_add_channel_announcement(struct routing_state *rstate, struct pubkey bitcoin_key_1; struct pubkey bitcoin_key_2; + /* Make sure we own msg, even if we don't save it. */ + if (taken(msg)) + tal_steal(tmpctx, msg); + if (!fromwire_channel_announcement( tmpctx, msg, &node_signature_1, &node_signature_2, &bitcoin_signature_1, &bitcoin_signature_2, &features, &chain_hash, @@ -1228,6 +1232,10 @@ bool routing_add_channel_update(struct routing_state *rstate, struct chan *chan; u8 direction; + /* Make sure we own msg, even if we don't save it. */ + if (taken(update)) + tal_steal(tmpctx, update); + if (!fromwire_channel_update(update, &signature, &chain_hash, &short_channel_id, ×tamp, &message_flags, &channel_flags, @@ -1499,6 +1507,10 @@ bool routing_add_node_announcement(struct routing_state *rstate, const u8 *msg T u8 *features, *addresses; struct wireaddr *wireaddrs; + /* Make sure we own msg, even if we don't save it. */ + if (taken(msg)) + tal_steal(tmpctx, msg); + /* Note: validity of node_id is already checked. */ if (!fromwire_node_announcement(tmpctx, msg, &signature, &features, ×tamp,