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;