0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

Refactor eager_{read,write,accept}_tcp into separate functions

This commit is contained in:
Bert Belder 2018-10-24 01:32:55 +02:00
parent 988ec88dd0
commit 58f0547e09
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461

View file

@ -206,25 +206,18 @@ where
Either::A(tokio_io::io::read(resource, buf)).into()
}
// This is an optimization that Tokio should do.
// Attempt to call read() on the main thread.
#[cfg(not(windows))]
pub fn eager_read<T>(resource: Resource, mut buf: T) -> EagerRead<Resource, T>
where
T: AsMut<[u8]>,
{
let mut table = RESOURCE_TABLE.lock().unwrap();
let maybe_repr = table.get_mut(&resource.rid);
match maybe_repr {
None => panic!("bad rid"),
Some(repr) => match repr {
Repr::TcpStream(ref mut tcp_stream) => {
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(not(windows))]
fn eager_read_tcp<T: AsMut<[u8]>>(
tcp_stream: &TcpStream,
resource: Resource,
mut buf: T,
) -> EagerRead<Resource, T> {
// Unforunately we can't just call read() on tokio::net::TcpStream
use std::os::unix::io::AsRawFd;
use std::os::unix::io::FromRawFd;
use std::os::unix::io::IntoRawFd;
let mut std_tcp_stream =
unsafe { std::net::TcpStream::from_raw_fd(tcp_stream.as_raw_fd()) };
let fd = (*tcp_stream).as_raw_fd();
let mut std_tcp_stream = unsafe { std::net::TcpStream::from_raw_fd(fd) };
let read_result = std_tcp_stream.read(buf.as_mut());
// std_tcp_stream will close when it gets dropped. Thus...
let _ = std_tcp_stream.into_raw_fd();
@ -238,6 +231,22 @@ where
}
}
}
}
// This is an optimization that Tokio should do.
// Attempt to call read() on the main thread.
#[cfg(not(windows))]
pub fn eager_read<T>(resource: Resource, buf: T) -> EagerRead<Resource, T>
where
T: AsMut<[u8]>,
{
let mut table = RESOURCE_TABLE.lock().unwrap();
let maybe_repr = table.get_mut(&resource.rid);
match maybe_repr {
None => panic!("bad rid"),
Some(repr) => match repr {
Repr::TcpStream(ref mut tcp_stream) => {
eager_read_tcp(tcp_stream, resource, buf)
}
_ => Either::A(tokio_io::io::read(resource, buf)),
},
@ -255,25 +264,14 @@ where
Either::A(tokio_write::write(resource, buf)).into()
}
// This is an optimization that Tokio should do.
// Attempt to call write() on the main thread.
#[cfg(not(windows))]
pub fn eager_write<T>(resource: Resource, buf: T) -> EagerWrite<Resource, T>
where
T: AsRef<[u8]>,
{
let mut table = RESOURCE_TABLE.lock().unwrap();
let maybe_repr = table.get_mut(&resource.rid);
match maybe_repr {
None => panic!("bad rid"),
Some(repr) => match repr {
Repr::TcpStream(ref mut tcp_stream) => {
// Unforunately we can't just call write() on tokio::net::TcpStream
use std::os::unix::io::AsRawFd;
use std::os::unix::io::FromRawFd;
use std::os::unix::io::IntoRawFd;
let mut std_tcp_stream =
unsafe { std::net::TcpStream::from_raw_fd(tcp_stream.as_raw_fd()) };
fn eager_write_tcp<T: AsRef<[u8]>>(
tcp_stream: &TcpStream,
resource: Resource,
buf: T,
) -> EagerWrite<Resource, T> {
let fd = (*tcp_stream).as_raw_fd();
let mut std_tcp_stream = unsafe { std::net::TcpStream::from_raw_fd(fd) };
let write_result = std_tcp_stream.write(buf.as_ref());
// std_tcp_stream will close when it gets dropped. Thus...
let _ = std_tcp_stream.into_raw_fd();
@ -287,6 +285,22 @@ where
}
}
}
}
// This is an optimization that Tokio should do.
// Attempt to call write() on the main thread.
#[cfg(not(windows))]
pub fn eager_write<T>(resource: Resource, buf: T) -> EagerWrite<Resource, T>
where
T: AsRef<[u8]>,
{
let mut table = RESOURCE_TABLE.lock().unwrap();
let maybe_repr = table.get_mut(&resource.rid);
match maybe_repr {
None => panic!("bad rid"),
Some(repr) => match repr {
Repr::TcpStream(ref mut tcp_stream) => {
eager_write_tcp(tcp_stream, resource, buf)
}
_ => Either::A(tokio_write::write(resource, buf)),
},
@ -303,22 +317,13 @@ pub fn eager_accept(resource: Resource) -> EagerAccept {
Either::A(tokio_util::accept(resource)).into()
}
// This is an optimization that Tokio should do.
// Attempt to call write() on the main thread.
#[cfg(not(windows))]
pub fn eager_accept(resource: Resource) -> EagerAccept {
let mut table = RESOURCE_TABLE.lock().unwrap();
let maybe_repr = table.get_mut(&resource.rid);
match maybe_repr {
None => panic!("bad rid"),
Some(repr) => match repr {
Repr::TcpListener(ref mut listener) => {
// Unforunately we can't just call write() on tokio::net::TcpStream
use std::os::unix::io::AsRawFd;
use std::os::unix::io::FromRawFd;
use std::os::unix::io::IntoRawFd;
let mut std_listener =
unsafe { std::net::TcpListener::from_raw_fd(listener.as_raw_fd()) };
fn eager_accept_tcp(
tcp_listener: &tokio::net::TcpListener,
resource: Resource,
) -> EagerAccept {
let fd = (*tcp_listener).as_raw_fd();
let std_listener = unsafe { std::net::TcpListener::from_raw_fd(fd) };
let result = std_listener.accept();
// std_listener will close when it gets dropped. Thus...
let _ = std_listener.into_raw_fd();
@ -339,6 +344,19 @@ pub fn eager_accept(resource: Resource) -> EagerAccept {
}
}
}
}
// This is an optimization that Tokio should do.
// Attempt to call write() on the main thread.
#[cfg(not(windows))]
pub fn eager_accept(resource: Resource) -> EagerAccept {
let mut table = RESOURCE_TABLE.lock().unwrap();
let maybe_repr = table.get_mut(&resource.rid);
match maybe_repr {
None => panic!("bad rid"),
Some(repr) => match repr {
Repr::TcpListener(ref mut tcp_listener) => {
eager_accept_tcp(tcp_listener, resource)
}
_ => Either::A(tokio_util::accept(resource)),
},