[Libwebsockets] lws_write speed

Roger Light roger at atchoo.org
Tue May 17 16:39:03 CEST 2016

On Tue, May 17, 2016 at 1:43 AM, Andy Green <andy at warmcat.com> wrote:

> But with a single-threaded / event driven system the goal is to make sure it
> never blocks.  If the OS isn't able to accept your whole buffer on the
> socket, actually lws steps in and copies the rest into a buffer and
> auto-drains it, emulating the threaded model (and consequently making
> everything infefficient, if functional).  But that's a backup for
> emergencies, needed because there is no way to know how much the socket will
> accept until after you did the write, it's not how it should work.
> How it should work is write stuff in chunks that are usually accepted by the
> socket, for example 2KB or 4KB.  You can do that once per WRITEABLE
> callback, and if there's more, ask to be called back when writable.  If the
> system is otherwise idle and more can be written, you'll be called back
> immediately.

Is this what happens in all cases? It doesn't match my experience, but
maybe something changed. I see the calls to lws_write() as operating
in a similar (but not identical...) manner to write(), i.e. I do
roughly this:

pos = 0;
len = 100000;

count = lws_write(wsi, &buf[pos], len, LWS_WRITE_BINARY);
pos += count;
len -= count;
if(len) lws_callback_on_writable(context, wsi);

If it was making a copy of the rest into a buffer then this wouldn't
work. The time I could think when what you're saying would apply is if
there are extensions enabled.



More information about the Libwebsockets mailing list