From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/9469 Path: news.gmane.org!not-for-mail From: Alexander Monakov Newsgroups: gmane.linux.lib.musl.general Subject: Re: [RFC PATCH] micro-optimize __procfdname Date: Sat, 5 Mar 2016 08:42:16 +0300 (MSK) Message-ID: References: <20160305052459.GD9349@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Trace: ger.gmane.org 1457156551 29460 80.91.229.3 (5 Mar 2016 05:42:31 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 5 Mar 2016 05:42:31 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-9482-gllmg-musl=m.gmane.org@lists.openwall.com Sat Mar 05 06:42:31 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1ac4z0-0001On-3h for gllmg-musl@m.gmane.org; Sat, 05 Mar 2016 06:42:30 +0100 Original-Received: (qmail 30410 invoked by uid 550); 5 Mar 2016 05:42:28 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 30392 invoked from network); 5 Mar 2016 05:42:27 -0000 In-Reply-To: <20160305052459.GD9349@brightrain.aerifal.cx> User-Agent: Alpine 2.20 (LNX 67 2015-01-07) Xref: news.gmane.org gmane.linux.lib.musl.general:9469 Archived-At: 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. > > +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. > > +#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 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. > 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. Alexander