From f8b3915ee13e63e169fc75ce98995c1b1f6d5808 Mon Sep 17 00:00:00 2001 From: Joris Vink Date: Sun, 24 Dec 2017 17:28:17 +0100 Subject: [PATCH] make kore_websocket_send() slightly faster. build the frame in a kore_buf on the stack and use net_send_stream() to push it out rather then making yet another copy in net_send(). saves several cpu cycles before the frame starts going out as we're not allocating a kore_buf, its contents and then memcpy()ing that contents to a new netbuf. --- src/websocket.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/websocket.c b/src/websocket.c index 208b030..f1d0dac 100644 --- a/src/websocket.c +++ b/src/websocket.c @@ -138,12 +138,16 @@ void kore_websocket_send(struct connection *c, u_int8_t op, const void *data, size_t len) { - struct kore_buf *frame; + struct kore_buf frame; - frame = kore_buf_alloc(len); - websocket_frame_build(frame, op, data, len); - net_send_queue(c, frame->data, frame->offset); - kore_buf_free(frame); + kore_buf_init(&frame, len); + + websocket_frame_build(&frame, op, data, len); + net_send_stream(c, frame.data, frame.offset, NULL, NULL); + + /* net_send_stream() takes over the buffer data pointer. */ + frame.data = NULL; + kore_buf_cleanup(&frame); net_send_flush(c); }