Rollup merge of #111057 - xfix:tcpstream-as-raw-fd-inline, r=m-ou-se

Make sure the implementation of TcpStream::as_raw_fd is fully inlined

Currently the following function:

```rust
use std::os::fd::{AsRawFd, RawFd};
use std::net::TcpStream;

pub fn as_raw_fd(socket: &TcpStream) -> RawFd {
    socket.as_raw_fd()
}
```

Is optimized to the following:

```asm
example::as_raw_fd:
        push    rax
        call    qword ptr [rip + <std::net::tcp::TcpStream as std::sys_common::AsInner<std::sys_common::net::TcpStream>>::as_inner@GOTPCREL]
        mov     rdi, rax
        call    qword ptr [rip + std::sys_common::net::TcpStream::socket@GOTPCREL]
        mov     rdi, rax
        pop     rax
        jmp     qword ptr [rip + _ZN73_$LT$std..sys..unix..net..Socket$u20$as$u20$std..os..fd..raw..AsRawFd$GT$9as_raw_fd17h633bcf7e481df8bbE@GOTPCREL]
```

I think it would make more sense to inline trivial functions used within `TcpStream::AsRawFd`.
This commit is contained in:
Matthias Krüger 2023-05-01 17:10:25 +02:00 committed by GitHub
commit 02134611ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 69 additions and 0 deletions

View File

@ -709,6 +709,7 @@ pub fn set_modified(&self, time: SystemTime) -> io::Result<()> {
// `AsRawHandle`/`IntoRawHandle`/`FromRawHandle` on Windows.
impl AsInner<fs_imp::File> for File {
#[inline]
fn as_inner(&self) -> &fs_imp::File {
&self.inner
}
@ -1087,12 +1088,14 @@ fn _open(&self, path: &Path) -> io::Result<File> {
}
impl AsInner<fs_imp::OpenOptions> for OpenOptions {
#[inline]
fn as_inner(&self) -> &fs_imp::OpenOptions {
&self.0
}
}
impl AsInnerMut<fs_imp::OpenOptions> for OpenOptions {
#[inline]
fn as_inner_mut(&mut self) -> &mut fs_imp::OpenOptions {
&mut self.0
}
@ -1352,6 +1355,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
}
impl AsInner<fs_imp::FileAttr> for Metadata {
#[inline]
fn as_inner(&self) -> &fs_imp::FileAttr {
&self.0
}
@ -1604,6 +1608,7 @@ pub fn is_symlink(&self) -> bool {
}
impl AsInner<fs_imp::FileType> for FileType {
#[inline]
fn as_inner(&self) -> &fs_imp::FileType {
&self.0
}
@ -1616,6 +1621,7 @@ fn from_inner(f: fs_imp::FilePermissions) -> Permissions {
}
impl AsInner<fs_imp::FilePermissions> for Permissions {
#[inline]
fn as_inner(&self) -> &fs_imp::FilePermissions {
&self.0
}
@ -1770,6 +1776,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
}
impl AsInner<fs_imp::DirEntry> for DirEntry {
#[inline]
fn as_inner(&self) -> &fs_imp::DirEntry {
&self.0
}
@ -2510,6 +2517,7 @@ fn create_dir_all(&self, path: &Path) -> io::Result<()> {
}
impl AsInnerMut<fs_imp::DirBuilder> for DirBuilder {
#[inline]
fn as_inner_mut(&mut self) -> &mut fs_imp::DirBuilder {
&mut self.inner
}

View File

@ -691,6 +691,7 @@ fn flush(&mut self) -> io::Result<()> {
}
impl AsInner<net_imp::TcpStream> for TcpStream {
#[inline]
fn as_inner(&self) -> &net_imp::TcpStream {
&self.0
}
@ -1033,6 +1034,7 @@ fn next(&mut self) -> Option<io::Result<TcpStream>> {
impl FusedIterator for IntoIncoming {}
impl AsInner<net_imp::TcpListener> for TcpListener {
#[inline]
fn as_inner(&self) -> &net_imp::TcpListener {
&self.0
}

View File

@ -788,6 +788,7 @@ pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
// `AsRawSocket`/`IntoRawSocket`/`FromRawSocket` on Windows.
impl AsInner<net_imp::UdpSocket> for UdpSocket {
#[inline]
fn as_inner(&self) -> &net_imp::UdpSocket {
&self.0
}

View File

@ -52,6 +52,7 @@ pub struct PidFd {
}
impl AsInner<FileDesc> for PidFd {
#[inline]
fn as_inner(&self) -> &FileDesc {
&self.inner
}
@ -70,6 +71,7 @@ fn into_inner(self) -> FileDesc {
}
impl AsRawFd for PidFd {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.as_inner().as_raw_fd()
}

View File

@ -211,6 +211,7 @@ pub struct Child {
impl crate::sealed::Sealed for Child {}
impl AsInner<imp::Process> for Child {
#[inline]
fn as_inner(&self) -> &imp::Process {
&self.handle
}
@ -304,6 +305,7 @@ fn flush(&mut self) -> io::Result<()> {
}
impl AsInner<AnonPipe> for ChildStdin {
#[inline]
fn as_inner(&self) -> &AnonPipe {
&self.inner
}
@ -373,6 +375,7 @@ fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
}
impl AsInner<AnonPipe> for ChildStdout {
#[inline]
fn as_inner(&self) -> &AnonPipe {
&self.inner
}
@ -438,6 +441,7 @@ fn is_read_vectored(&self) -> bool {
}
impl AsInner<AnonPipe> for ChildStderr {
#[inline]
fn as_inner(&self) -> &AnonPipe {
&self.inner
}
@ -1107,12 +1111,14 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
}
impl AsInner<imp::Command> for Command {
#[inline]
fn as_inner(&self) -> &imp::Command {
&self.inner
}
}
impl AsInnerMut<imp::Command> for Command {
#[inline]
fn as_inner_mut(&mut self) -> &mut imp::Command {
&mut self.inner
}
@ -1605,6 +1611,7 @@ pub fn code(&self) -> Option<i32> {
}
impl AsInner<imp::ExitStatus> for ExitStatus {
#[inline]
fn as_inner(&self) -> &imp::ExitStatus {
&self.0
}
@ -1884,6 +1891,7 @@ fn from(code: u8) -> Self {
}
impl AsInner<imp::ExitCode> for ExitCode {
#[inline]
fn as_inner(&self) -> &imp::ExitCode {
&self.0
}

View File

@ -75,6 +75,7 @@ unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
}
impl AsInner<OwnedFd> for FileDesc {
#[inline]
fn as_inner(&self) -> &OwnedFd {
&self.fd
}

View File

@ -367,12 +367,14 @@ pub fn mkdir(&self, _p: &Path) -> io::Result<()> {
}
impl AsInner<FileDesc> for File {
#[inline]
fn as_inner(&self) -> &FileDesc {
&self.0
}
}
impl AsInnerMut<FileDesc> for File {
#[inline]
fn as_inner_mut(&mut self) -> &mut FileDesc {
&mut self.0
}
@ -397,6 +399,7 @@ fn as_fd(&self) -> BorrowedFd<'_> {
}
impl AsRawFd for File {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}

View File

@ -340,6 +340,7 @@ pub fn as_raw(&self) -> RawFd {
}
impl AsInner<FileDesc> for Socket {
#[inline]
fn as_inner(&self) -> &FileDesc {
&self.0
}
@ -364,6 +365,7 @@ fn as_fd(&self) -> BorrowedFd<'_> {
}
impl AsRawFd for Socket {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}

View File

@ -62,6 +62,7 @@ pub fn flush(&self) -> io::Result<()> {
}
impl AsInner<Fd> for FileDesc {
#[inline]
fn as_inner(&self) -> &Fd {
&self.fd
}

View File

@ -24,6 +24,7 @@ fn new(fd: usercalls::raw::Fd, local_addr: String) -> Socket {
}
impl AsInner<FileDesc> for Socket {
#[inline]
fn as_inner(&self) -> &FileDesc {
&self.inner
}
@ -220,6 +221,7 @@ pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
}
impl AsInner<Socket> for TcpStream {
#[inline]
fn as_inner(&self) -> &Socket {
&self.inner
}
@ -304,6 +306,7 @@ pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
}
impl AsInner<Socket> for TcpListener {
#[inline]
fn as_inner(&self) -> &Socket {
&self.inner
}

View File

@ -112,6 +112,7 @@ fn duplicate(&self) -> io::Result<FileDesc> {
}
impl AsInner<c_int> for FileDesc {
#[inline]
fn as_inner(&self) -> &c_int {
&self.fd
}
@ -462,6 +463,7 @@ pub fn as_raw(&self) -> c_int {
}
impl AsInner<c_int> for Socket {
#[inline]
fn as_inner(&self) -> &c_int {
self.0.as_inner()
}

View File

@ -481,6 +481,7 @@ fn is_read_vectored(&self) -> bool {
}
impl AsInner<OwnedFd> for FileDesc {
#[inline]
fn as_inner(&self) -> &OwnedFd {
&self.0
}
@ -505,6 +506,7 @@ fn as_fd(&self) -> BorrowedFd<'_> {
}
impl AsRawFd for FileDesc {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}

View File

@ -547,6 +547,7 @@ pub fn created(&self) -> io::Result<SystemTime> {
}
impl AsInner<stat64> for FileAttr {
#[inline]
fn as_inner(&self) -> &stat64 {
&self.stat
}
@ -1269,12 +1270,14 @@ pub fn set_mode(&mut self, mode: u32) {
}
impl AsInner<FileDesc> for File {
#[inline]
fn as_inner(&self) -> &FileDesc {
&self.0
}
}
impl AsInnerMut<FileDesc> for File {
#[inline]
fn as_inner_mut(&mut self) -> &mut FileDesc {
&mut self.0
}
@ -1299,6 +1302,7 @@ fn as_fd(&self) -> BorrowedFd<'_> {
}
impl AsRawFd for File {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}

View File

@ -129,6 +129,7 @@ pub fn as_raw(&self) -> RawFd {
}
impl AsInner<FileDesc> for Socket {
#[inline]
fn as_inner(&self) -> &FileDesc {
&self.0
}
@ -153,6 +154,7 @@ fn as_fd(&self) -> BorrowedFd<'_> {
}
impl AsRawFd for Socket {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
@ -183,6 +185,7 @@ pub fn connect_timeout(_: &SocketAddr, _: Duration) -> io::Result<TcpStream> {
unimpl!();
}
#[inline]
pub fn socket(&self) -> &Socket {
&self.inner
}
@ -305,6 +308,7 @@ pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
unimpl!();
}
#[inline]
pub fn socket(&self) -> &Socket {
&self.inner
}
@ -371,6 +375,7 @@ pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
unimpl!();
}
#[inline]
pub fn socket(&self) -> &Socket {
&self.inner
}

View File

@ -490,6 +490,7 @@ pub fn as_raw(&self) -> RawFd {
}
impl AsInner<FileDesc> for Socket {
#[inline]
fn as_inner(&self) -> &FileDesc {
&self.0
}
@ -514,6 +515,7 @@ fn as_fd(&self) -> BorrowedFd<'_> {
}
impl AsRawFd for Socket {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}

View File

@ -89,6 +89,7 @@ fn into_inner(self) -> Vec<u8> {
}
impl AsInner<[u8]> for Buf {
#[inline]
fn as_inner(&self) -> &[u8] {
&self.inner
}

View File

@ -135,6 +135,7 @@ fn read(fd: &FileDesc, dst: &mut Vec<u8>) -> Result<bool, io::Error> {
}
impl AsRawFd for AnonPipe {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}

View File

@ -275,12 +275,14 @@ pub fn sock_shutdown(&self, how: Shutdown) -> io::Result<()> {
}
impl AsInner<OwnedFd> for WasiFd {
#[inline]
fn as_inner(&self) -> &OwnedFd {
&self.fd
}
}
impl AsInnerMut<OwnedFd> for WasiFd {
#[inline]
fn as_inner_mut(&mut self) -> &mut OwnedFd {
&mut self.fd
}
@ -305,6 +307,7 @@ fn as_fd(&self) -> BorrowedFd<'_> {
}
impl AsRawFd for WasiFd {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.fd.as_raw_fd()
}

View File

@ -498,6 +498,7 @@ pub fn read_link(&self, file: &Path) -> io::Result<PathBuf> {
}
impl AsInner<WasiFd> for File {
#[inline]
fn as_inner(&self) -> &WasiFd {
&self.fd
}
@ -522,6 +523,7 @@ fn as_fd(&self) -> BorrowedFd<'_> {
}
impl AsRawFd for File {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.fd.as_raw_fd()
}

View File

@ -17,6 +17,7 @@ pub struct TcpStream {
}
impl AsInner<WasiFd> for Socket {
#[inline]
fn as_inner(&self) -> &WasiFd {
&self.0
}
@ -41,6 +42,7 @@ fn as_fd(&self) -> BorrowedFd<'_> {
}
impl AsRawFd for Socket {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
@ -184,6 +186,7 @@ pub fn set_nonblocking(&self, state: bool) -> io::Result<()> {
}
}
#[inline]
pub fn socket(&self) -> &Socket {
&self.inner
}
@ -274,6 +277,7 @@ pub fn set_nonblocking(&self, state: bool) -> io::Result<()> {
}
}
#[inline]
pub fn socket(&self) -> &Socket {
&self.inner
}
@ -284,6 +288,7 @@ pub fn into_socket(self) -> Socket {
}
impl AsInner<Socket> for TcpListener {
#[inline]
fn as_inner(&self) -> &Socket {
&self.inner
}
@ -436,6 +441,7 @@ pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
unsupported()
}
#[inline]
pub fn socket(&self) -> &Socket {
&self.inner
}
@ -446,6 +452,7 @@ pub fn into_socket(self) -> Socket {
}
impl AsInner<Socket> for UdpSocket {
#[inline]
fn as_inner(&self) -> &Socket {
&self.inner
}

View File

@ -832,6 +832,7 @@ fn open_link_no_reparse(parent: &File, name: &[u16], access: u32) -> io::Result<
}
impl AsInner<Handle> for File {
#[inline]
fn as_inner(&self) -> &Handle {
&self.handle
}

View File

@ -34,6 +34,7 @@ pub fn new_event(manual: bool, init: bool) -> io::Result<Handle> {
}
impl AsInner<OwnedHandle> for Handle {
#[inline]
fn as_inner(&self) -> &OwnedHandle {
&self.0
}

View File

@ -446,6 +446,7 @@ fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
}
impl AsInner<OwnedSocket> for Socket {
#[inline]
fn as_inner(&self) -> &OwnedSocket {
&self.0
}

View File

@ -27,6 +27,7 @@ fn from_inner(inner: Wtf8Buf) -> Self {
}
impl AsInner<Wtf8> for Buf {
#[inline]
fn as_inner(&self) -> &Wtf8 {
&self.inner
}

View File

@ -239,6 +239,7 @@ pub fn connect_timeout(addr: &SocketAddr, timeout: Duration) -> io::Result<TcpSt
Ok(TcpStream { inner: sock })
}
#[inline]
pub fn socket(&self) -> &Socket {
&self.inner
}
@ -352,6 +353,7 @@ pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
}
impl AsInner<Socket> for TcpStream {
#[inline]
fn as_inner(&self) -> &Socket {
&self.inner
}
@ -427,6 +429,7 @@ pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
Ok(TcpListener { inner: sock })
}
#[inline]
pub fn socket(&self) -> &Socket {
&self.inner
}
@ -517,6 +520,7 @@ pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
Ok(UdpSocket { inner: sock })
}
#[inline]
pub fn socket(&self) -> &Socket {
&self.inner
}

View File

@ -501,6 +501,7 @@ pub struct Wtf8 {
}
impl AsInner<[u8]> for Wtf8 {
#[inline]
fn as_inner(&self) -> &[u8] {
&self.bytes
}