From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: Max Carrara <m.carrara@proxmox.com>
Cc: pbs-devel@lists.proxmox.com
Subject: Re: [pbs-devel] [PATCH pxar 2/2] decoder: aio: improve performance of async file reads
Date: Fri, 4 Aug 2023 13:27:39 +0200 [thread overview]
Message-ID: <unz5e47wrjdngtuf76u4p7ji34nzzqqzesiu5l4ocellrhq7hm@rht7ad5l5ju3> (raw)
In-Reply-To: <20230720171505.1053912-2-m.carrara@proxmox.com>
On Thu, Jul 20, 2023 at 07:15:05PM +0200, Max Carrara wrote:
> In order to bring `aio::Decoder` on par with its `sync` counterpart
> as well as `sync::Accessor` and `aio::Accessor`, its input is now
> buffered.
>
> As the `tokio` docs mention themselves [0], it can be really
> inefficient to directly work with an (unbuffered) `AsyncRead`
> instance.
Sure, but the question is *where* does it truly make sense to do the
buffering, more below...
(...)
> ---
> src/decoder/aio.rs | 12 +++++++++---
> 1 file changed, 9 insertions(+), 3 deletions(-)
>
> diff --git a/src/decoder/aio.rs b/src/decoder/aio.rs
> index 200dd3d..174551b 100644
> --- a/src/decoder/aio.rs
> +++ b/src/decoder/aio.rs
> @@ -79,14 +79,20 @@ mod tok {
> use std::pin::Pin;
> use std::task::{Context, Poll};
>
> - /// Read adapter for `futures::io::AsyncRead`
> + use tokio::io::AsyncRead;
> +
> + /// Read adapter for `tokio::io::AsyncRead`
> pub struct TokioReader<T> {
^ This is a very generic interface here...
> - inner: T,
> + inner: tokio::io::BufReader<T>,
> }
>
> impl<T: tokio::io::AsyncRead> TokioReader<T> {
> pub fn new(inner: T) -> Self {
Note that `tokio`'s `BufReader` itself also implements `AsyncRead`, and
the user may already have a buffered reader here.
A better choice for us here would be to perform this change with the
`tokio-fs` feature and replace the
impl Decoder<TokioReader<tokio::fs::File>> {
fn open(...) -> io::Result<Self> { ... }
}
(which exists only so that `Decoder::open` can be used by the crate
consumer easily, automatically producing a `Decoder` for "some file
type"...)
with:
impl Decoder<TokioReader<BufReader<tokio::fs::File>>> {
fn open(...) -> io::Result<Self> { ... }
}
Since this is the place where we *actually* should be creating the
buffered reader.
> - Self { inner }
> + // buffer size "sweet spot" - larger sizes don't seem to provide any benefit
> + const BUF_SIZE: usize = 1024 * 16;
And we also wouldn't have to decide on what would be a sane size here
with the assumption that it is the right size for any possible T we
instantiate the decoder with.
There's a bit of a danger with sprinkling `BufReaders` in generic `T:
Read` APIs, as this may lead to multiple of those getting chained
together.
Eg. a consumer of the crate may instantiate a
`Decoder<SomeNetworkFile<TlsStreamThing>>`.
Then reads that buffering for such things can improve performance and
turn that into: `Decoder<SomeNetworkFile<BufReader<TlsStreamThing>>>`.
Little do they know that `Decoder` buffers, the creator of
`SomeNetworkFile` also thought the same thing and buffers as well, and
`TlsStreamThing` might also need buffering for a sane implementation, and
suddenly you're just chaining memcpys across 4 buffers before they end
up at the destination ;-)
> + Self {
> + inner: tokio::io::BufReader::with_capacity(BUF_SIZE, inner),
> + }
> }
> }
>
> --
> 2.39.2
prev parent reply other threads:[~2023-08-04 11:28 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-20 17:15 [pbs-devel] [PATCH pxar 1/2] Add dependency on `tokio/io-util` to `tokio-io` feature Max Carrara
2023-07-20 17:15 ` [pbs-devel] [PATCH pxar 2/2] decoder: aio: improve performance of async file reads Max Carrara
2023-07-27 8:50 ` Fabian Grünbichler
2023-08-04 11:27 ` Wolfgang Bumiller [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=unz5e47wrjdngtuf76u4p7ji34nzzqqzesiu5l4ocellrhq7hm@rht7ad5l5ju3 \
--to=w.bumiller@proxmox.com \
--cc=m.carrara@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal