From: Alexander Monakov <amonakov@ispras.ru>
To: musl@lists.openwall.com
Subject: [RFC PATCH] micro-optimize __procfdname
Date: Sun, 21 Feb 2016 14:41:21 +0300 (MSK) [thread overview]
Message-ID: <alpine.LNX.2.20.1602211426390.21162@monopod.intra.ispras.ru> (raw)
Hello,
I've noticed that internal function __procfdname can be slightly cleaned up by
filling the supplied buffer right-to-left and returning the last filled
position. The patch below implements what I have in mind, and changes one
call site to demonstrate (I'll be happy to submit a patch that converts all
calls, if overall this change is desirable).
diff --git a/src/internal/procfdname.c b/src/internal/procfdname.c
index 697e0bd..cfb3f90 100644
--- a/src/internal/procfdname.c
+++ b/src/internal/procfdname.c
@@ -1,13 +1,9 @@
-void __procfdname(char *buf, unsigned fd)
+char *__procfdname_impl(char *buf, unsigned fd)
{
- unsigned i, j;
- for (i=0; (buf[i] = "/proc/self/fd/"[i]); i++);
- if (!fd) {
- buf[i] = '0';
- buf[i+1] = 0;
- return;
- }
- for (j=fd; j; j/=10, i++);
- buf[i] = 0;
- for (; fd; fd/=10) buf[--i] = '0' + fd%10;
+ *buf = 0;
+ do *--buf = '0' + fd % 10;
+ while (fd /= 10);
+ for (int i = 13; i >= 0; i--)
+ *--buf = "/proc/self/fd/"[i];
+ return buf;
}
diff --git a/src/internal/procfdname.h b/src/internal/procfdname.h
index e69de29..6d3c6e2 100644
--- a/src/internal/procfdname.h
+++ b/src/internal/procfdname.h
@@ -0,0 +1,9 @@
+#ifndef PROCFDNAME_H
+#define PROCFDNAME_H
+
+char *__procfdname_impl(char *, unsigned);
+
+#define procfdbufsize sizeof "/proc/self/fd/0123456789" + (3 * (sizeof(int)-4))
+#define procfdname(buf, fd) __procfdname_impl(buf + procfdbufsize - 1, fd)
+
+#endif
diff --git a/src/process/fexecve.c b/src/process/fexecve.c
index 6507b42..88e6b9d 100644
--- a/src/process/fexecve.c
+++ b/src/process/fexecve.c
@@ -1,13 +1,11 @@
#include <unistd.h>
#include <errno.h>
-
-void __procfdname(char *, unsigned);
+#include "procfdname.h"
int fexecve(int fd, char *const argv[], char *const envp[])
{
- char buf[15 + 3*sizeof(int)];
- __procfdname(buf, fd);
- execve(buf, argv, envp);
+ char buf[procfdbufsize];
+ execve(procfdname(buf, fd), argv, envp);
if (errno == ENOENT) errno = EBADF;
return -1;
}
next reply other threads:[~2016-02-21 11:41 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-21 11:41 Alexander Monakov [this message]
2016-03-05 5:24 ` Rich Felker
2016-03-05 5:42 ` Alexander Monakov
2016-03-05 5:56 ` Rich Felker
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=alpine.LNX.2.20.1602211426390.21162@monopod.intra.ispras.ru \
--to=amonakov@ispras.ru \
--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).