smol_http

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit fa5a482f5c8f7259993c9d0925cef72113bc98c9
parent 4d93ec51f68a60ed9cf836ddc73e1e3875e96c08
Author: Anton Kling <anton@kling.gg>
Date:   Wed,  2 Mar 2022 21:08:54 +0100

Now using sendfile(2) for sending files over the socket.

This should be faster and more efficient as the copying happens
in the kernel directly as opposed to doing it in userland via
read and send write.

From my minimal testing I saw no major speed improvement for
medium sized files though using this function certainly does
not hurt.

Diffstat:
Msmol_http.c | 10+++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/smol_http.c b/smol_http.c @@ -24,6 +24,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/sendfile.h> #include <sys/socket.h> #include <sys/stat.h> #include <unistd.h> @@ -271,11 +272,10 @@ write: } PLEDGE("stdio", NULL); - char rwbuf[4096]; - for (int l; 0 != (l = read(fd, rwbuf, sizeof(rwbuf)));) { - COND_PERROR_EXP(-1 == l, "read", break); - COND_PERROR_EXP(-1 == write(socket_desc, rwbuf, l), "write", break); - } + struct stat buf; + fstat(fd, &buf); + COND_PERROR_EXP(-1 == sendfile(socket_desc, fd, 0, buf.st_size), "sendfile", + /*NOP*/); close(fd); cleanup: