Skip to content

Commit

Permalink
Rollup merge of #111057 - xfix:tcpstream-as-raw-fd-inline, r=m-ou-se
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
matthiaskrgr authored May 1, 2023
2 parents 0a2562b + 500a8e1 commit 0213461
Show file tree
Hide file tree
Showing 26 changed files with 69 additions and 0 deletions.
8 changes: 8 additions & 0 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ impl File {
// `AsRawHandle`/`IntoRawHandle`/`FromRawHandle` on Windows.

impl AsInner<fs_imp::File> for File {
#[inline]
fn as_inner(&self) -> &fs_imp::File {
&self.inner
}
Expand Down Expand Up @@ -1087,12 +1088,14 @@ impl OpenOptions {
}

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
}
Expand Down Expand Up @@ -1352,6 +1355,7 @@ impl fmt::Debug for Metadata {
}

impl AsInner<fs_imp::FileAttr> for Metadata {
#[inline]
fn as_inner(&self) -> &fs_imp::FileAttr {
&self.0
}
Expand Down Expand Up @@ -1604,6 +1608,7 @@ impl FileType {
}

impl AsInner<fs_imp::FileType> for FileType {
#[inline]
fn as_inner(&self) -> &fs_imp::FileType {
&self.0
}
Expand All @@ -1616,6 +1621,7 @@ impl FromInner<fs_imp::FilePermissions> for Permissions {
}

impl AsInner<fs_imp::FilePermissions> for Permissions {
#[inline]
fn as_inner(&self) -> &fs_imp::FilePermissions {
&self.0
}
Expand Down Expand Up @@ -1770,6 +1776,7 @@ impl fmt::Debug for DirEntry {
}

impl AsInner<fs_imp::DirEntry> for DirEntry {
#[inline]
fn as_inner(&self) -> &fs_imp::DirEntry {
&self.0
}
Expand Down Expand Up @@ -2510,6 +2517,7 @@ impl DirBuilder {
}

impl AsInnerMut<fs_imp::DirBuilder> for DirBuilder {
#[inline]
fn as_inner_mut(&mut self) -> &mut fs_imp::DirBuilder {
&mut self.inner
}
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ impl Write for &TcpStream {
}

impl AsInner<net_imp::TcpStream> for TcpStream {
#[inline]
fn as_inner(&self) -> &net_imp::TcpStream {
&self.0
}
Expand Down Expand Up @@ -1033,6 +1034,7 @@ impl Iterator for IntoIncoming {
impl FusedIterator for IntoIncoming {}

impl AsInner<net_imp::TcpListener> for TcpListener {
#[inline]
fn as_inner(&self) -> &net_imp::TcpListener {
&self.0
}
Expand Down
1 change: 1 addition & 0 deletions library/std/src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ impl UdpSocket {
// `AsRawSocket`/`IntoRawSocket`/`FromRawSocket` on Windows.

impl AsInner<net_imp::UdpSocket> for UdpSocket {
#[inline]
fn as_inner(&self) -> &net_imp::UdpSocket {
&self.0
}
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/os/linux/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub struct PidFd {
}

impl AsInner<FileDesc> for PidFd {
#[inline]
fn as_inner(&self) -> &FileDesc {
&self.inner
}
Expand All @@ -70,6 +71,7 @@ impl IntoInner<FileDesc> for PidFd {
}

impl AsRawFd for PidFd {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.as_inner().as_raw_fd()
}
Expand Down
8 changes: 8 additions & 0 deletions library/std/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -304,6 +305,7 @@ impl Write for &ChildStdin {
}

impl AsInner<AnonPipe> for ChildStdin {
#[inline]
fn as_inner(&self) -> &AnonPipe {
&self.inner
}
Expand Down Expand Up @@ -373,6 +375,7 @@ impl Read for ChildStdout {
}

impl AsInner<AnonPipe> for ChildStdout {
#[inline]
fn as_inner(&self) -> &AnonPipe {
&self.inner
}
Expand Down Expand Up @@ -438,6 +441,7 @@ impl Read for ChildStderr {
}

impl AsInner<AnonPipe> for ChildStderr {
#[inline]
fn as_inner(&self) -> &AnonPipe {
&self.inner
}
Expand Down Expand Up @@ -1107,12 +1111,14 @@ impl fmt::Debug for Command {
}

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
}
Expand Down Expand Up @@ -1605,6 +1611,7 @@ impl ExitStatus {
}

impl AsInner<imp::ExitStatus> for ExitStatus {
#[inline]
fn as_inner(&self) -> &imp::ExitStatus {
&self.0
}
Expand Down Expand Up @@ -1884,6 +1891,7 @@ impl From<u8> for ExitCode {
}

impl AsInner<imp::ExitCode> for ExitCode {
#[inline]
fn as_inner(&self) -> &imp::ExitCode {
&self.0
}
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/hermit/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl FromRawFd for FileDesc {
}

impl AsInner<OwnedFd> for FileDesc {
#[inline]
fn as_inner(&self) -> &OwnedFd {
&self.fd
}
Expand Down
3 changes: 3 additions & 0 deletions library/std/src/sys/hermit/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,14 @@ impl DirBuilder {
}

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
}
Expand All @@ -397,6 +399,7 @@ impl AsFd for File {
}

impl AsRawFd for File {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/sys/hermit/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ impl Socket {
}

impl AsInner<FileDesc> for Socket {
#[inline]
fn as_inner(&self) -> &FileDesc {
&self.0
}
Expand All @@ -364,6 +365,7 @@ impl AsFd for Socket {
}

impl AsRawFd for Socket {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/sgx/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ impl FileDesc {
}

impl AsInner<Fd> for FileDesc {
#[inline]
fn as_inner(&self) -> &Fd {
&self.fd
}
Expand Down
3 changes: 3 additions & 0 deletions library/std/src/sys/sgx/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl Socket {
}

impl AsInner<FileDesc> for Socket {
#[inline]
fn as_inner(&self) -> &FileDesc {
&self.inner
}
Expand Down Expand Up @@ -220,6 +221,7 @@ impl TcpStream {
}

impl AsInner<Socket> for TcpStream {
#[inline]
fn as_inner(&self) -> &Socket {
&self.inner
}
Expand Down Expand Up @@ -304,6 +306,7 @@ impl TcpListener {
}

impl AsInner<Socket> for TcpListener {
#[inline]
fn as_inner(&self) -> &Socket {
&self.inner
}
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/sys/solid/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl FileDesc {
}

impl AsInner<c_int> for FileDesc {
#[inline]
fn as_inner(&self) -> &c_int {
&self.fd
}
Expand Down Expand Up @@ -462,6 +463,7 @@ impl Socket {
}

impl AsInner<c_int> for Socket {
#[inline]
fn as_inner(&self) -> &c_int {
self.0.as_inner()
}
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/sys/unix/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ impl<'a> Read for &'a FileDesc {
}

impl AsInner<OwnedFd> for FileDesc {
#[inline]
fn as_inner(&self) -> &OwnedFd {
&self.0
}
Expand All @@ -505,6 +506,7 @@ impl AsFd for FileDesc {
}

impl AsRawFd for FileDesc {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
Expand Down
4 changes: 4 additions & 0 deletions library/std/src/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ impl FileAttr {
}

impl AsInner<stat64> for FileAttr {
#[inline]
fn as_inner(&self) -> &stat64 {
&self.stat
}
Expand Down Expand Up @@ -1269,12 +1270,14 @@ impl DirBuilder {
}

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
}
Expand All @@ -1299,6 +1302,7 @@ impl AsFd for File {
}

impl AsRawFd for File {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
Expand Down
5 changes: 5 additions & 0 deletions library/std/src/sys/unix/l4re.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ pub mod net {
}

impl AsInner<FileDesc> for Socket {
#[inline]
fn as_inner(&self) -> &FileDesc {
&self.0
}
Expand All @@ -153,6 +154,7 @@ pub mod net {
}

impl AsRawFd for Socket {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
Expand Down Expand Up @@ -183,6 +185,7 @@ pub mod net {
unimpl!();
}

#[inline]
pub fn socket(&self) -> &Socket {
&self.inner
}
Expand Down Expand Up @@ -305,6 +308,7 @@ pub mod net {
unimpl!();
}

#[inline]
pub fn socket(&self) -> &Socket {
&self.inner
}
Expand Down Expand Up @@ -371,6 +375,7 @@ pub mod net {
unimpl!();
}

#[inline]
pub fn socket(&self) -> &Socket {
&self.inner
}
Expand Down
2 changes: 2 additions & 0 deletions library/std/src/sys/unix/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ impl Socket {
}

impl AsInner<FileDesc> for Socket {
#[inline]
fn as_inner(&self) -> &FileDesc {
&self.0
}
Expand All @@ -514,6 +515,7 @@ impl AsFd for Socket {
}

impl AsRawFd for Socket {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/unix/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl IntoInner<Vec<u8>> for Buf {
}

impl AsInner<[u8]> for Buf {
#[inline]
fn as_inner(&self) -> &[u8] {
&self.inner
}
Expand Down
1 change: 1 addition & 0 deletions library/std/src/sys/unix/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ pub fn read2(p1: AnonPipe, v1: &mut Vec<u8>, p2: AnonPipe, v2: &mut Vec<u8>) ->
}

impl AsRawFd for AnonPipe {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
Expand Down
Loading

0 comments on commit 0213461

Please sign in to comment.