Simple optimizations

text	   data	    bss	    dec	    hex	filename
 239474	    672	  20484	 260630	  3fa16	strace.before
 239234	    668	  19044	 258946	  3f382	strace

* file.c (sprint_open_modes): Reduce static buffer size.
Simplify separator printing.
* signal.c (sprintsigmask): Reduce static buffer size.
Simplify separator printing and printing of almost full masks.
Use stpcpy instead of sprintf and strcpy+strlen.
* strace.c (startup_child): Don't strchr() for ':' twice in a row.
* util.c (sprintflags): Exit loop early if possible.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2012-01-29 22:38:35 +01:00
parent 3521884c91
commit 4f3df078b2
4 changed files with 33 additions and 38 deletions

15
file.c
View File

@ -272,7 +272,6 @@ const struct xlat open_mode_flags[] = {
#ifdef O_CLOEXEC
{ O_CLOEXEC, "O_CLOEXEC" },
#endif
#ifdef FNDELAY
{ FNDELAY, "FNDELAY" },
#endif
@ -349,15 +348,17 @@ print_dirfd(struct tcb *tcp, int fd)
const char *
sprint_open_modes(mode_t flags)
{
static char outstr[1024];
static char outstr[(1 + ARRAY_SIZE(open_mode_flags)) * sizeof("O_LARGEFILE")];
char *p;
char sep = 0;
char sep;
const char *str;
const struct xlat *x;
p = stpcpy(outstr, "flags ");
sep = ' ';
p = stpcpy(outstr, "flags");
str = xlookup(open_access_modes, flags & 3);
if (str) {
*p++ = sep;
p = stpcpy(p, str);
flags &= ~3;
if (!flags)
@ -367,8 +368,7 @@ sprint_open_modes(mode_t flags)
for (x = open_mode_flags; x->str; x++) {
if ((flags & x->val) == x->val) {
if (sep)
*p++ = sep;
*p++ = sep;
p = stpcpy(p, x->str);
flags &= ~x->val;
if (!flags)
@ -377,8 +377,7 @@ sprint_open_modes(mode_t flags)
}
}
/* flags is still nonzero */
if (sep)
*p++ = sep;
*p++ = sep;
sprintf(p, "%#x", flags);
return outstr;
}

View File

@ -319,64 +319,57 @@ sprintsigmask(const char *str, sigset_t *mask, int rt)
* and sig 0 doesn't exist either.
* Therefore max possible no of sigs is 255: 1..255
*/
static char outstr[8 * 255];
static char outstr[8 * (255 * 2 / 3)];
int i, nsigs;
int maxsigs;
const char *format;
int show_members;
char sep;
char *s;
strcpy(outstr, str);
s = outstr + strlen(outstr);
nsigs = 0;
maxsigs = nsignals;
#ifdef __SIGRTMAX
if (rt)
maxsigs = __SIGRTMAX; /* instead */
#endif
s = stpcpy(outstr, str);
nsigs = 0;
for (i = 1; i < maxsigs; i++) {
if (sigismember(mask, i) == 1)
nsigs++;
}
if (nsigs >= nsignals * 2 / 3) {
/* 1: show mask members, 0: show those which are NOT in mask */
show_members = (nsigs < nsignals * 2 / 3);
if (!show_members)
*s++ = '~';
for (i = 1; i < maxsigs; i++) {
switch (sigismember(mask, i)) {
case 1:
sigdelset(mask, i);
break;
case 0:
sigaddset(mask, i);
break;
}
}
}
format = "%s";
*s++ = '[';
sep = '[';
for (i = 1; i < maxsigs; i++) {
if (sigismember(mask, i) == 1) {
if (sigismember(mask, i) == show_members) {
/* real-time signals on solaris don't have
* signalent entries
*/
char tsig[40];
*s++ = sep;
if (i < nsignals) {
sprintf(s, format, signalent[i] + 3);
s = stpcpy(s, signalent[i] + 3);
}
#ifdef SIGRTMIN
else if (i >= __SIGRTMIN && i <= __SIGRTMAX) {
char tsig[40];
sprintf(tsig, "RT_%u", i - __SIGRTMIN);
sprintf(s, format, tsig);
s = stpcpy(s, tsig);
}
#endif /* SIGRTMIN */
else {
char tsig[32];
sprintf(tsig, "%u", i);
sprintf(s, format, tsig);
s = stpcpy(s, tsig);
}
s += strlen(s);
format = " %s";
sep = ' ';
}
}
if (sep == '[')
*s++ = sep;
*s++ = ']';
*s = '\0';
return outstr;

View File

@ -656,8 +656,9 @@ startup_child(char **argv)
int m, n, len;
for (path = getenv("PATH"); path && *path; path += m) {
if (strchr(path, ':')) {
n = strchr(path, ':') - path;
const char *colon = strchr(path, ':');
if (colon) {
n = colon - path;
m = n + 1;
}
else

4
util.c
View File

@ -325,8 +325,10 @@ sprintflags(const char *prefix, const struct xlat *xlat, int flags)
if (found)
*outptr++ = '|';
outptr = stpcpy(outptr, xlat->str);
flags &= ~xlat->val;
found = 1;
flags &= ~xlat->val;
if (!flags)
break;
}
}
if (flags) {