From: Denys Vlasenko Date: Sun, 29 Jan 2012 21:38:35 +0000 (+0100) Subject: Simple optimizations X-Git-Tag: v4.7~181 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4f3df078b26899afe0f25d8651b06a5a5b5143e2;p=strace 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 --- diff --git a/file.c b/file.c index b2567c5b..87e07438 100644 --- a/file.c +++ b/file.c @@ -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; } diff --git a/signal.c b/signal.c index aca6345f..4556a5fe 100644 --- a/signal.c +++ b/signal.c @@ -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; diff --git a/strace.c b/strace.c index b739cd62..38c8518a 100644 --- a/strace.c +++ b/strace.c @@ -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 diff --git a/util.c b/util.c index 2a73db78..b6cdf9a5 100644 --- a/util.c +++ b/util.c @@ -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) {