Skip to content

Commit

Permalink
Rollup merge of rust-lang#84541 - KaiJewson:inline-raw, r=m-ou-se
Browse files Browse the repository at this point in the history
Inline most raw socket, fd and handle conversions

Now that file descriptor types on Unix have niches, it is advantageous for user libraries which provide file descriptor wrappers (e.g. `Socket` from socket2) to store a `File` internally instead of a `RawFd`, so that the niche can be taken advantage of. However, doing so will currently result in worse performance as `IntoRawFd`, `FromRawFd` and `AsRawFd` are not inlined. This change adds `#[inline]` to those methods on std types that wrap file descriptors, handles or sockets.
  • Loading branch information
Dylan-DPC authored Apr 25, 2021
2 parents c53aeb2 + fbc2aad commit 684592d
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 0 deletions.
6 changes: 6 additions & 0 deletions library/std/src/sys/sgx/ext/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ pub trait TryIntoRawFd: Sized {
}

impl AsRawFd for net::TcpStream {
#[inline]
fn as_raw_fd(&self) -> RawFd {
*self.as_inner().as_inner().as_inner().as_inner()
}
}

impl AsRawFd for net::TcpListener {
#[inline]
fn as_raw_fd(&self) -> RawFd {
*self.as_inner().as_inner().as_inner().as_inner()
}
Expand All @@ -87,6 +89,7 @@ pub struct TcpStreamMetadata {
impl FromRawFd for net::TcpStream {
type Metadata = TcpStreamMetadata;

#[inline]
unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> net::TcpStream {
let fd = sys::fd::FileDesc::from_inner(fd);
let socket = sys::net::Socket::from_inner((fd, metadata.local_addr));
Expand All @@ -105,6 +108,7 @@ pub struct TcpListenerMetadata {
impl FromRawFd for net::TcpListener {
type Metadata = TcpListenerMetadata;

#[inline]
unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> net::TcpListener {
let fd = sys::fd::FileDesc::from_inner(fd);
let socket = sys::net::Socket::from_inner((fd, metadata.local_addr));
Expand All @@ -113,6 +117,7 @@ impl FromRawFd for net::TcpListener {
}

impl TryIntoRawFd for net::TcpStream {
#[inline]
fn try_into_raw_fd(self) -> Result<RawFd, Self> {
let (socket, peer_addr) = self.into_inner().into_inner();
match socket.try_into_inner() {
Expand All @@ -126,6 +131,7 @@ impl TryIntoRawFd for net::TcpStream {
}

impl TryIntoRawFd for net::TcpListener {
#[inline]
fn try_into_raw_fd(self) -> Result<RawFd, Self> {
match self.into_inner().into_inner().try_into_inner() {
Ok(fd) => Ok(fd.into_inner()),
Expand Down
12 changes: 12 additions & 0 deletions library/std/src/sys/unix/ext/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,79 +104,91 @@ pub trait IntoRawFd {

#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
impl AsRawFd for RawFd {
#[inline]
fn as_raw_fd(&self) -> RawFd {
*self
}
}
#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
impl IntoRawFd for RawFd {
#[inline]
fn into_raw_fd(self) -> RawFd {
self
}
}
#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
impl FromRawFd for RawFd {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> RawFd {
fd
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl AsRawFd for fs::File {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.as_inner().fd().raw()
}
}
#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawFd for fs::File {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
fs::File::from_inner(sys::fs::File::from_inner(fd))
}
}
#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawFd for fs::File {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_fd().into_raw()
}
}

#[stable(feature = "asraw_stdio", since = "1.21.0")]
impl AsRawFd for io::Stdin {
#[inline]
fn as_raw_fd(&self) -> RawFd {
libc::STDIN_FILENO
}
}

#[stable(feature = "asraw_stdio", since = "1.21.0")]
impl AsRawFd for io::Stdout {
#[inline]
fn as_raw_fd(&self) -> RawFd {
libc::STDOUT_FILENO
}
}

#[stable(feature = "asraw_stdio", since = "1.21.0")]
impl AsRawFd for io::Stderr {
#[inline]
fn as_raw_fd(&self) -> RawFd {
libc::STDERR_FILENO
}
}

#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
impl<'a> AsRawFd for io::StdinLock<'a> {
#[inline]
fn as_raw_fd(&self) -> RawFd {
libc::STDIN_FILENO
}
}

#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
impl<'a> AsRawFd for io::StdoutLock<'a> {
#[inline]
fn as_raw_fd(&self) -> RawFd {
libc::STDOUT_FILENO
}
}

#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
impl<'a> AsRawFd for io::StderrLock<'a> {
#[inline]
fn as_raw_fd(&self) -> RawFd {
libc::STDERR_FILENO
}
Expand Down
3 changes: 3 additions & 0 deletions library/std/src/sys/unix/ext/net/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,20 +879,23 @@ impl UnixDatagram {

#[stable(feature = "unix_socket", since = "1.10.0")]
impl AsRawFd for UnixDatagram {
#[inline]
fn as_raw_fd(&self) -> RawFd {
*self.0.as_inner()
}
}

#[stable(feature = "unix_socket", since = "1.10.0")]
impl FromRawFd for UnixDatagram {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram {
UnixDatagram(Socket::from_inner(fd))
}
}

#[stable(feature = "unix_socket", since = "1.10.0")]
impl IntoRawFd for UnixDatagram {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.0.into_inner()
}
Expand Down
3 changes: 3 additions & 0 deletions library/std/src/sys/unix/ext/net/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,20 +240,23 @@ impl UnixListener {

#[stable(feature = "unix_socket", since = "1.10.0")]
impl AsRawFd for UnixListener {
#[inline]
fn as_raw_fd(&self) -> RawFd {
*self.0.as_inner()
}
}

#[stable(feature = "unix_socket", since = "1.10.0")]
impl FromRawFd for UnixListener {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> UnixListener {
UnixListener(Socket::from_inner(fd))
}
}

#[stable(feature = "unix_socket", since = "1.10.0")]
impl IntoRawFd for UnixListener {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.0.into_inner()
}
Expand Down
3 changes: 3 additions & 0 deletions library/std/src/sys/unix/ext/net/raw_fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ macro_rules! impl_as_raw_fd {
($($t:ident)*) => {$(
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRawFd for net::$t {
#[inline]
fn as_raw_fd(&self) -> RawFd {
*self.as_inner().socket().as_inner()
}
Expand All @@ -18,6 +19,7 @@ macro_rules! impl_from_raw_fd {
($($t:ident)*) => {$(
#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawFd for net::$t {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> net::$t {
let socket = sys::net::Socket::from_inner(fd);
net::$t::from_inner(sys_common::net::$t::from_inner(socket))
Expand All @@ -31,6 +33,7 @@ macro_rules! impl_into_raw_fd {
($($t:ident)*) => {$(
#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawFd for net::$t {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_socket().into_inner()
}
Expand Down
3 changes: 3 additions & 0 deletions library/std/src/sys/unix/ext/net/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,20 +654,23 @@ impl<'a> io::Write for &'a UnixStream {

#[stable(feature = "unix_socket", since = "1.10.0")]
impl AsRawFd for UnixStream {
#[inline]
fn as_raw_fd(&self) -> RawFd {
*self.0.as_inner()
}
}

#[stable(feature = "unix_socket", since = "1.10.0")]
impl FromRawFd for UnixStream {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
UnixStream(Socket::from_inner(fd))
}
}

#[stable(feature = "unix_socket", since = "1.10.0")]
impl IntoRawFd for UnixStream {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.0.into_inner()
}
Expand Down
7 changes: 7 additions & 0 deletions library/std/src/sys/unix/ext/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ impl ExitStatusExt for process::ExitStatus {

#[stable(feature = "process_extensions", since = "1.2.0")]
impl FromRawFd for process::Stdio {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> process::Stdio {
let fd = sys::fd::FileDesc::new(fd);
let io = sys::process::Stdio::Fd(fd);
Expand All @@ -283,41 +284,47 @@ impl FromRawFd for process::Stdio {

#[stable(feature = "process_extensions", since = "1.2.0")]
impl AsRawFd for process::ChildStdin {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.as_inner().fd().raw()
}
}

#[stable(feature = "process_extensions", since = "1.2.0")]
impl AsRawFd for process::ChildStdout {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.as_inner().fd().raw()
}
}

#[stable(feature = "process_extensions", since = "1.2.0")]
impl AsRawFd for process::ChildStderr {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.as_inner().fd().raw()
}
}

#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawFd for process::ChildStdin {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_fd().into_raw()
}
}

#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawFd for process::ChildStdout {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_fd().into_raw()
}
}

#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawFd for process::ChildStderr {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_fd().into_raw()
}
Expand Down
Loading

0 comments on commit 684592d

Please sign in to comment.