Skip to content

Commit

Permalink
Use a read size based on the latest buffer passed to read, if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonarddeR committed Dec 22, 2022
1 parent aaf712e commit 4ad0aa4
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/sys/windows/named_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,15 +539,15 @@ impl<'a> Read for &'a NamedPipe {
state.read = State::Ok(data, next);
} else {
self.inner.put_buffer(data);
Inner::schedule_read(&self.inner, &mut state, None);
Inner::schedule_read(&self.inner, &mut state, None, buf.len().into());
}
Ok(n)
}

// Looks like an in-flight read hit an error, return that here while
// we schedule a new one.
State::Err(e) => {
Inner::schedule_read(&self.inner, &mut state, None);
Inner::schedule_read(&self.inner, &mut state, None, buf.len().into());
if e.raw_os_error() == Some(ERROR_BROKEN_PIPE as i32) {
Ok(0)
} else {
Expand Down Expand Up @@ -581,7 +581,7 @@ impl<'a> Write for &'a NamedPipe {
}

// Move `buf` onto the heap and fire off the write
let mut owned_buf = self.inner.get_buffer();
let mut owned_buf = self.inner.get_buffer(1024 * 4);
owned_buf.extend(buf);
match Inner::maybe_schedule_write(&self.inner, owned_buf, 0, &mut io)? {
// Some bytes are written immediately
Expand Down Expand Up @@ -693,15 +693,15 @@ impl Inner {
/// is scheduled in the background. If the pipe is no longer connected
/// (ERROR_PIPE_LISTENING) then `false` is returned and no read is
/// scheduled.
fn schedule_read(me: &Arc<Inner>, io: &mut Io, events: Option<&mut Vec<Event>>) -> bool {
fn schedule_read(me: &Arc<Inner>, io: &mut Io, events: Option<&mut Vec<Event>>, read_size: Option<usize>) -> bool {
// Check to see if a read is already scheduled/completed
match io.read {
State::None => {}
_ => return true,
}

// Allocate a buffer and schedule the read.
let mut buf = me.get_buffer();
let mut buf = me.get_buffer(read_size.unwrap_or(1024 * 4));
let e = unsafe {
let overlapped = me.read.as_ptr() as *mut _;
let slice = slice::from_raw_parts_mut(buf.as_mut_ptr(), buf.capacity());
Expand Down Expand Up @@ -795,15 +795,15 @@ impl Inner {
fn post_register(me: &Arc<Inner>, mut events: Option<&mut Vec<Event>>) {
let mut io = me.io.lock().unwrap();
#[allow(clippy::needless_option_as_deref)]
if Inner::schedule_read(me, &mut io, events.as_deref_mut()) {
if Inner::schedule_read(me, &mut io, events.as_deref_mut(), None) {
if let State::None = io.write {
io.notify_writable(events);
}
}
}

fn get_buffer(&self) -> Vec<u8> {
self.pool.lock().unwrap().get(4 * 1024)
fn get_buffer(&self, size: usize) -> Vec<u8> {
self.pool.lock().unwrap().get(size)
}

fn put_buffer(&self, buf: Vec<u8>) {
Expand Down

0 comments on commit 4ad0aa4

Please sign in to comment.