Skip to content

Commit c61e9c3

Browse files
committed
tokio-postgres: buffer sockets to avoid excessive syscalls
The current implementation forwards all read requests to the operating system through the socket causing excessive system calls. The effect is magnified when the underlying Socket is wrapped around a TLS implementation. This commit changes the underlying socket to be read-buffered by default with a buffer size of 16K, following the implementation of the official client. Signed-off-by: Petros Angelatos <petrosagg@gmail.com>
1 parent 52de269 commit c61e9c3

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

tokio-postgres/src/socket.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use std::io;
22
use std::pin::Pin;
33
use std::task::{Context, Poll};
4-
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
4+
use tokio::io::{AsyncRead, AsyncWrite, BufReader, ReadBuf};
55
use tokio::net::TcpStream;
66
#[cfg(unix)]
77
use tokio::net::UnixStream;
88

99
#[derive(Debug)]
1010
enum Inner {
11-
Tcp(TcpStream),
11+
Tcp(BufReader<TcpStream>),
1212
#[cfg(unix)]
13-
Unix(UnixStream),
13+
Unix(BufReader<UnixStream>),
1414
}
1515

1616
/// The standard stream type used by the crate.
@@ -21,12 +21,12 @@ pub struct Socket(Inner);
2121

2222
impl Socket {
2323
pub(crate) fn new_tcp(stream: TcpStream) -> Socket {
24-
Socket(Inner::Tcp(stream))
24+
Socket(Inner::Tcp(BufReader::with_capacity(16 * 1024, stream)))
2525
}
2626

2727
#[cfg(unix)]
2828
pub(crate) fn new_unix(stream: UnixStream) -> Socket {
29-
Socket(Inner::Unix(stream))
29+
Socket(Inner::Unix(BufReader::with_capacity(16 * 1024, stream)))
3030
}
3131
}
3232

0 commit comments

Comments
 (0)