Skip to content

socket: exploit SOCK_NONBLOCK to eliminate extra system calls #13855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

panjf2000
Copy link
Contributor

@panjf2000 panjf2000 commented Jun 2, 2024

Every time function cf_socket_open() is called to create a socket, curlx_nonblock() is called to make that socket non-blocking. And curlx_nonblock() will cost us 1 or 2 system calls (2 for fcntl(), 1 for ioctl(), etc.), meanwhile, tucking SOCK_NONBLOCK and SOCK_CLOEXEC into the type argument for socket() is widely supported across UNIX-like OS: Linux, *BSD, Solaris, etc. With that ability, we can save 1 or 2 system calls on each socket.

Another change in this PR is to eliminate the redundant curlx_nonblock() call on the socket in cf_udp_setup_quic() as that socket created by cf_socket_open() is already non-blocking.

Ref:

@panjf2000 panjf2000 changed the title socket: use SOCK_NONBLOCK to eliminate extra system call socket: exploit SOCK_NONBLOCK to eliminate extra system call Jun 2, 2024
@panjf2000 panjf2000 changed the title socket: exploit SOCK_NONBLOCK to eliminate extra system call socket: exploit SOCK_NONBLOCK to eliminate extra system call Jun 2, 2024
@panjf2000 panjf2000 changed the title socket: exploit SOCK_NONBLOCK to eliminate extra system call socket: exploit SOCK_NONBLOCK to eliminate extra system calls Jun 2, 2024
@bagder
Copy link
Member

bagder commented Jun 2, 2024

Please provide a description explaining what this change does and why we want it.

@panjf2000
Copy link
Contributor Author

Please provide a description explaining what this change does and why we want it.

Done, please check out the top comment! @bagder

@panjf2000 panjf2000 force-pushed the sock-nonblock branch 2 times, most recently from 2acc06a to 80f9bfb Compare June 3, 2024 21:59
@panjf2000
Copy link
Contributor Author

While you're here, could you take a look at this PR? @bagder

Every time function `cf_socket_open()` is called to create a socket,
`curlx_nonblock()` is called to make that socket non-blocking.
And `curlx_nonblock()` will cost us 1 or 2 system calls (2 for `fcntl()`,
1 for `ioctl()`, etc.), meanwhile, tucking `SOCK_NONBLOCK` and `SOCK_CLOEXEC`
into the `type` argument for `socket()` is widely supported across UNIX-like OS:
Linux, *BSD, Solaris, etc. With that ability, we can save 1 or 2 system calls
on each socket.

Another change in this PR is to eliminate the redundant `curlx_nonblock()` call
on the socket in `cf_udp_setup_quic()` as that socket created by `cf_socket_open()`
is already non-blocking.

Ref:
https://man7.org/linux/man-pages/man2/socket.2.html
https://man.freebsd.org/cgi/man.cgi?socket(2)
https://man.dragonflybsd.org/?command=socket&section=2
https://man.netbsd.org/socket.2
https://man.openbsd.org/socket
https://docs.oracle.com/cd/E88353_01/html/E37843/socket-3c.html
https://illumos.org/man/3SOCKET/socket
...
@bagder bagder closed this in 3392f0f Jun 4, 2024
@bagder
Copy link
Member

bagder commented Jun 4, 2024

Thanks!

@panjf2000 panjf2000 deleted the sock-nonblock branch June 4, 2024 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants