mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: Alexander Monakov <amonakov@ispras.ru>
Cc: musl@lists.openwall.com, Dominic Chen <d.c.ddcc@gmail.com>
Subject: Re: [musl] Issue with fread() and unaligned readv()
Date: Tue, 16 Mar 2021 19:54:55 -0400	[thread overview]
Message-ID: <20210316235454.GJ32655@brightrain.aerifal.cx> (raw)
In-Reply-To: <alpine.LNX.2.20.13.2103161121210.17743@monopod.intra.ispras.ru>

On Tue, Mar 16, 2021 at 12:30:11PM +0300, Alexander Monakov wrote:
> On Mon, 15 Mar 2021, Rich Felker wrote:
> 
> > > Thanks. Can musl reduce the first iov tuple by, say, 8 bytes rather than
> > > 1 byte, to avoid forcing the kernel to perform a misaligned copy?
> > 
> > Well then you have to do more copy in userspace afterwards, and reduce
> > the effective buffer size by a bit, going back to kernel slightly more
> > often or spending extra memory to compensate.
> 
> Of course, but shouldn't you consider how it balances against the
> cost to perform a 1K (BUFSIZ) misaligned copy on each read?

Do we have an idea what this cost actually is on popular platforms?
That would help determine if this direction is useful to consider.

> > There's also no strong
> > reason to believe one will be aligned and the other won't, except at
> > beginning of file. The alignment mod 8 depends on file position and
> > access history, and neither the caller's buffer nor the FILE buffer
> > have any inherent alignment.
> 
> The alignment of caller's buffer is another matter, I was talking about
> misaligned copy into internal FILE buffer (and even then, at least when
> user buffer was malloc'ed it will be sufficiently aligned).
> 
> The buffer in FILE obtained from fopen will be aligned to _Alignof(_IO_FILE)
> in musl thanks to UNGET being 8.

Yes, it's just stdin/out/err that have no inherent alignment. The
buffers from fopen will be aligned to at least 4 and perhaps 8
depending on arch ABI and members of FILE.

> If the file has been repositioned, yes, bets are off, but I think with stdio
> it is quite common to read a file without seeks (could be non-seekable
> in the first place).

Well it's plenty common to work with network, terminal, pipe, etc.
containing non-aligned-record content. But these are probably less
interesting for performance.

Looking at the source, we could probably, instead of using
-!!f->buf_size, setup a negative offset based on len and buf_size, up
to 8, that's computed and saved in the first line of the function. If
the offset is equal to len, the first iov can be skipped entirely,
which tends to make the syscall considerably faster -- optimizing this
out was already desirable. Then, the final if (f->buf_size) buf[len-1]
= *f->rpos++; could be replaced with a loop up to this offset that
collapses out if it's zero. This would avoid introducing significant
amounts of new code and might improve things in other cases too.

Rich

      reply	other threads:[~2021-03-16 23:55 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-15 21:39 Dominic Chen
2021-03-15 21:51 ` Rich Felker
2021-03-15 22:09   ` Alexander Monakov
2021-03-15 22:29     ` Rich Felker
2021-03-15 22:42       ` Alexander Monakov
2021-03-16  2:13         ` Rich Felker
2021-03-16  9:30           ` Alexander Monakov
2021-03-16 23:54             ` Rich Felker [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=20210316235454.GJ32655@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=amonakov@ispras.ru \
    --cc=d.c.ddcc@gmail.com \
    --cc=musl@lists.openwall.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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).