Skip to content

Commit

Permalink
Don't initialize read buffer
Browse files Browse the repository at this point in the history
This shouldn't be necessary, but it seems that tokio-codec doesn't
properly track the initialization of its buffer and so the old logic
resulted in a ton of overhead.
  • Loading branch information
sfackler committed Jan 9, 2021
1 parent add3cbb commit 65e5c48
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use openssl::ssl::{self, ErrorCode, ShutdownResult, Ssl, SslRef};
use std::fmt;
use std::io::{self, Read, Write};
use std::pin::Pin;
use std::slice;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};

Expand Down Expand Up @@ -189,12 +190,23 @@ where
ctx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
self.with_context(ctx, |s| match cvt(s.read(buf.initialize_unfilled()))? {
Poll::Ready(nread) => {
buf.advance(nread);
Poll::Ready(Ok(()))
self.with_context(ctx, |s| {
// This isn't really "proper", but rust-openssl doesn't currently expose a suitable interface even though
// OpenSSL itself doesn't require the buffer to be initialized. So this is good enough for now.
let slice = unsafe {
let buf = buf.unfilled_mut();
slice::from_raw_parts_mut(buf.as_mut_ptr().cast::<u8>(), buf.len())
};
match cvt(s.read(slice))? {
Poll::Ready(nread) => {
unsafe {
buf.assume_init(nread);
}
buf.advance(nread);
Poll::Ready(Ok(()))
}
Poll::Pending => Poll::Pending,
}
Poll::Pending => Poll::Pending,
})
}
}
Expand Down

0 comments on commit 65e5c48

Please sign in to comment.