From 752ac30ec820bc0ef4dfea698fd7119a8a4aa14c Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 24 May 2012 14:31:50 +0200 Subject: [PATCH] unix: don't pass sockaddr to accept() Shaves a few nanoseconds off the accept() syscall. --- src/unix/core.c | 12 +++++++++--- src/unix/internal.h | 2 +- src/unix/pipe.c | 3 +-- src/unix/stream.c | 3 +-- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/unix/core.c b/src/unix/core.c index 39292634..923629a2 100644 --- a/src/unix/core.c +++ b/src/unix/core.c @@ -445,14 +445,18 @@ out: } -int uv__accept(int sockfd, struct sockaddr* saddr, socklen_t slen) { +int uv__accept(int sockfd) { int peerfd; assert(sockfd >= 0); while (1) { #if __linux__ - peerfd = uv__accept4(sockfd, saddr, &slen, UV__SOCK_NONBLOCK|UV__SOCK_CLOEXEC); + peerfd = uv__accept4(sockfd, + NULL, + NULL, + UV__SOCK_NONBLOCK|UV__SOCK_CLOEXEC); + if (peerfd != -1) break; @@ -463,7 +467,9 @@ int uv__accept(int sockfd, struct sockaddr* saddr, socklen_t slen) { break; #endif - if ((peerfd = accept(sockfd, saddr, &slen)) == -1) { + peerfd = accept(sockfd, NULL, NULL); + + if (peerfd == -1) { if (errno == EINTR) continue; else diff --git a/src/unix/internal.h b/src/unix/internal.h index f42fead0..551d6904 100644 --- a/src/unix/internal.h +++ b/src/unix/internal.h @@ -156,7 +156,7 @@ void uv__stream_init(uv_loop_t* loop, uv_stream_t* stream, int uv__stream_open(uv_stream_t*, int fd, int flags); void uv__stream_destroy(uv_stream_t* stream); void uv__server_io(uv_loop_t* loop, uv__io_t* watcher, int events); -int uv__accept(int sockfd, struct sockaddr* saddr, socklen_t len); +int uv__accept(int sockfd); int uv__connect(uv_connect_t* req, uv_stream_t* stream, struct sockaddr* addr, socklen_t addrlen, uv_connect_cb cb); diff --git a/src/unix/pipe.c b/src/unix/pipe.c index d88ac392..666ebec8 100644 --- a/src/unix/pipe.c +++ b/src/unix/pipe.c @@ -229,7 +229,6 @@ out: /* TODO merge with uv__server_io()? */ static void uv__pipe_accept(uv_loop_t* loop, uv__io_t* w, int events) { - struct sockaddr_un saddr; uv_pipe_t* pipe; int saved_errno; int sockfd; @@ -239,7 +238,7 @@ static void uv__pipe_accept(uv_loop_t* loop, uv__io_t* w, int events) { assert(pipe->type == UV_NAMED_PIPE); - sockfd = uv__accept(pipe->fd, (struct sockaddr *)&saddr, sizeof saddr); + sockfd = uv__accept(pipe->fd); if (sockfd == -1) { if (errno != EAGAIN && errno != EWOULDBLOCK) { uv__set_sys_error(pipe->loop, errno); diff --git a/src/unix/stream.c b/src/unix/stream.c index 516a4690..7ea44874 100644 --- a/src/unix/stream.c +++ b/src/unix/stream.c @@ -165,7 +165,6 @@ void uv__stream_destroy(uv_stream_t* stream) { void uv__server_io(uv_loop_t* loop, uv__io_t* w, int events) { int fd; - struct sockaddr_storage addr; uv_stream_t* stream = container_of(w, uv_stream_t, read_watcher); assert(events == UV__IO_READ); @@ -181,7 +180,7 @@ void uv__server_io(uv_loop_t* loop, uv__io_t* w, int events) { */ while (stream->fd != -1) { assert(stream->accepted_fd < 0); - fd = uv__accept(stream->fd, (struct sockaddr*)&addr, sizeof addr); + fd = uv__accept(stream->fd); if (fd < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) {