mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: [RFC PATCH] micro-optimize __procfdname
Date: Sat, 5 Mar 2016 00:56:05 -0500	[thread overview]
Message-ID: <20160305055605.GF9349@brightrain.aerifal.cx> (raw)
In-Reply-To: <alpine.LNX.2.20.1603050830540.31711@monopod.intra.ispras.ru>

On Sat, Mar 05, 2016 at 08:42:16AM +0300, Alexander Monakov wrote:
> On Sat, 5 Mar 2016, Rich Felker wrote:
> > I really doubt this makes any major improvement, but it might help
> > size a bit and it might be cleaner/more readable, so it's interesting.
> 
> Yeah, this precedes a syscall so speed-wise it doesn't matter; I just
> noticed two div-10 loops and saw a chance to improve size.

*nod*

> > > +char *__procfdname_impl(char *, unsigned);
> > > +
> > > +#define procfdbufsize sizeof "/proc/self/fd/0123456789" + (3 * (sizeof(int)-4))
> > 
> > What is the motivation behind changing the size expression to use the
> > "012...9" part? It's nonobvious to me.
> 
> It just makes it obvious that there are 10 decimal places, which is how much a
> 32-bit unsigned int can occupy at most. I don't mind using any other style.

I tend to like 3*sizeof(int) just because it's an idiom I know
(pessimistic bound as if each byte could hold 0...999 range rather
than just 0...255) but your version is slightly sharper.

> > > +#define procfdname(buf, fd) __procfdname_impl(buf + procfdbufsize - 1, fd)
> > 
> > I suppose the idea of putting the offset to the end in a macro in the
> > header rather than in the callee is both optimization and allowing the
> > compiler to detect out-of-bounds pointer arithmetic?
> 
> Hm, the latter is rather theoretical given the uses, right? I just made it to

I meant I thought the compiler might be able to catch if a callee
accidentally used the wrong buffer size. Shouldn't happen anyway, but
it'd be nice to have an extra layer of verification.

> make it really obvious that __procfdname_impl fills in reverse; it might be a
> very minor size optimization. I don't mind dropping this add adjusting buf
> with '+= procfdbufsize - 1' in the callee.

Yes, making it obvious what's going on is nice too.

Actually it would be even nicer if we could use a compound literal
inside the macro as the buffer, but that would pessimize with
unnecessary initialization and eliminate a lot of the code-size
benefit, I think.

> > Here using the return value directly is nice but at some other call
> > points might we need to introduce a pointer variable to store the
> > pointer returned? I haven't checked yet.
> 
> Yes, I went through the call sites and they are all easy to adjust; I think a
> couple needed a pointer, like you said.

OK.

Rich


  reply	other threads:[~2016-03-05  5:56 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-21 11:41 Alexander Monakov
2016-03-05  5:24 ` Rich Felker
2016-03-05  5:42   ` Alexander Monakov
2016-03-05  5:56     ` Rich Felker [this message]
2016-03-05  6:14       ` Alexander Monakov
2016-03-05  6:20         ` Rich Felker
2016-03-05  6:35           ` Alexander Monakov
2016-03-05 15:13           ` Alexander Monakov
2016-03-05 17:30             ` Alexander Monakov

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=20160305055605.GF9349@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --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).