common/msg_queue: use membuf for greater efficiency.

Based on CPU consumption in memmove with the current naive approach.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2024-10-28 10:55:49 +10:30
parent 859f795168
commit 1051e97d69

View File

@ -1,13 +1,14 @@
#include "config.h"
#include <assert.h>
#include <ccan/cast/cast.h>
#include <ccan/membuf/membuf.h>
#include <common/msg_queue.h>
#include <common/utils.h>
#include <wire/wire.h>
struct msg_queue {
bool fd_passing;
const u8 **q;
MEMBUF(const u8 *) mb;
};
static int extract_fd(const u8 *msg)
@ -24,18 +25,29 @@ static int extract_fd(const u8 *msg)
/* Close any fds left in queue! */
static void destroy_msg_queue(struct msg_queue *q)
{
for (size_t i = 0; i < tal_count(q->q); i++) {
int fd = extract_fd(q->q[i]);
const u8 **elems = membuf_elems(&q->mb);
for (size_t i = 0; i < membuf_num_elems(&q->mb); i++) {
int fd = extract_fd(elems[i]);
if (fd != -1)
close(fd);
}
}
/* Realloc helper for tal membufs */
static void *membuf_tal_realloc(struct membuf *mb, void *rawelems,
size_t newsize)
{
char *p = rawelems;
tal_resize(&p, newsize);
return p;
}
struct msg_queue *msg_queue_new(const tal_t *ctx, bool fd_passing)
{
struct msg_queue *q = tal(ctx, struct msg_queue);
q->fd_passing = fd_passing;
q->q = tal_arr(q, const u8 *, 0);
membuf_init(&q->mb, tal_arr(q, const u8 *, 0), 0, membuf_tal_realloc);
if (q->fd_passing)
tal_add_destructor(q, destroy_msg_queue);
@ -44,7 +56,9 @@ struct msg_queue *msg_queue_new(const tal_t *ctx, bool fd_passing)
static void do_enqueue(struct msg_queue *q, const u8 *add TAKES)
{
tal_arr_expand(&q->q, tal_dup_talarr(q, u8, add));
const u8 **msg = membuf_add(&q->mb, 1);
*msg = tal_dup_talarr(q, u8, add);
/* In case someone is waiting */
io_wake(q);
@ -52,7 +66,7 @@ static void do_enqueue(struct msg_queue *q, const u8 *add TAKES)
size_t msg_queue_length(const struct msg_queue *q)
{
return tal_count(q->q);
return membuf_num_elems(&q->mb);
}
void msg_enqueue(struct msg_queue *q, const u8 *add)
@ -73,16 +87,12 @@ void msg_enqueue_fd(struct msg_queue *q, int fd)
const u8 *msg_dequeue(struct msg_queue *q)
{
size_t n = tal_count(q->q);
const u8 *msg;
size_t n = msg_queue_length(q);
if (!n)
return NULL;
msg = q->q[0];
memmove(q->q, q->q + 1, sizeof(*q->q) * (n-1));
tal_resize(&q->q, n-1);
return msg;
return membuf_consume(&q->mb, 1)[0];
}
int msg_extract_fd(const struct msg_queue *q, const u8 *msg)