broadcast: Added option to replace a specific index

We are wasting way too much time looking for announcements and updates
in the broadcast. We can just hint where to find the message to be
evicted and safe the traversal.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker 2018-01-31 17:52:12 +01:00 committed by Rusty Russell
parent 60ab947a2f
commit 01b7e2a7c0
2 changed files with 37 additions and 3 deletions

View File

@ -22,6 +22,28 @@ static struct queued_message *new_queued_message(tal_t *ctx,
return msg;
}
bool replace_broadcast(struct broadcast_state *bstate, u64 *index,
const int type, const u8 *tag, const u8 *payload)
{
struct queued_message *msg;
bool evicted = false;
msg = uintmap_get(&bstate->broadcasts, *index);
if (msg && msg->type == type &&
memeq(msg->tag, tal_len(msg->tag), tag, tal_len(tag))) {
uintmap_del(&bstate->broadcasts, *index);
tal_free(msg);
evicted = true;
}
*index = bstate->next_index;
/* Now add the message to the queue */
msg = new_queued_message(bstate, type, tag, payload);
uintmap_add(&bstate->broadcasts, *index, msg);
bstate->next_index++;
return evicted;
}
bool queue_broadcast(struct broadcast_state *bstate,
const int type,
const u8 *tag,
@ -37,7 +59,8 @@ bool queue_broadcast(struct broadcast_state *bstate,
for (msg = uintmap_first(&bstate->broadcasts, &index);
msg;
msg = uintmap_after(&bstate->broadcasts, &index)) {
if (msg->type == type && memcmp(msg->tag, tag, tal_len(tag)) == 0) {
if (msg->type == type &&
memeq(msg->tag, tal_len(msg->tag), tag, tal_len(tag))) {
uintmap_del(&bstate->broadcasts, index);
tal_free(msg);
evicted = true;

View File

@ -20,8 +20,8 @@ struct queued_message {
};
struct broadcast_state {
u32 next_index;
UINTMAP(struct queued_message *) broadcasts;
u32 next_index;
UINTMAP(struct queued_message *) broadcasts;
};
struct broadcast_state *new_broadcast_state(tal_t *ctx);
@ -36,6 +36,17 @@ bool queue_broadcast(struct broadcast_state *bstate,
const u8 *tag,
const u8 *payload);
/* Replace a queued message with @index, if it matches the type and
* tag for the new message. The new message will be queued with the
* next highest index. @index is updated to hold the index of the
* newly queued message*/
bool replace_broadcast(struct broadcast_state *bstate,
u64 *index,
const int type,
const u8 *tag,
const u8 *payload);
struct queued_message *next_broadcast_message(struct broadcast_state *bstate, u64 last_index);
#endif /* LIGHTNING_LIGHTNINGD_GOSSIP_BROADCAST_H */