From: Denys Vlasenko Date: Mon, 27 Feb 2012 13:37:48 +0000 (+0100) Subject: Assorted trivial optimizations X-Git-Tag: v4.7~147 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1945ccc3fbd5b56008c4a6b0cdd4611616201675;p=strace Assorted trivial optimizations text data bss dec hex filename 236448 672 19044 256164 3e8a4 strace.before 236360 672 19044 256076 3e84c strace * file.c (sprintmode): Use smaller static buffer, eliminate strlen call. (sprinttime): Use smaller static buffer. (printstat_sparc64): Coalesce two printing calls into one. (printstat_powerpc32): Likewise. (printcompat_statfs6): Likewise. (sys_utime): Do not fetch personality_wordsize[current_personality] repeatedly - cache it in local variable instead. * process.c (printargv): Likewise. * resource.c (sprintrlim): Return const char*, not char*. This allows to eliminate sprintf(buf, "RLIM_INFINITY"). Use smaller static buffer. (sprintrlim64): Likewise. * strace.c (strerror): Use smaller static buffer. (strsignal): Likewise. Signed-off-by: Denys Vlasenko --- diff --git a/file.c b/file.c index 69b3c84b..be1b1a73 100644 --- a/file.c +++ b/file.c @@ -666,7 +666,9 @@ static const struct xlat modetypes[] = { static const char * sprintmode(int mode) { - static char buf[64]; + static char buf[sizeof("S_IFSOCK|S_ISUID|S_ISGID|S_ISVTX|%o") + + sizeof(int)*3 + + /*paranoia:*/ 8]; const char *s; if ((mode & S_IFMT) == 0) @@ -675,13 +677,13 @@ sprintmode(int mode) sprintf(buf, "%#o", mode); return buf; } - sprintf(buf, "%s%s%s%s", s, + s = buf + sprintf(buf, "%s%s%s%s", s, (mode & S_ISUID) ? "|S_ISUID" : "", (mode & S_ISGID) ? "|S_ISGID" : "", (mode & S_ISVTX) ? "|S_ISVTX" : ""); mode &= ~(S_IFMT|S_ISUID|S_ISGID|S_ISVTX); if (mode) - sprintf(buf + strlen(buf), "|%#o", mode); + sprintf((char*)s, "|%#o", mode); s = (*buf == '|') ? buf + 1 : buf; return *s ? s : "0"; } @@ -690,7 +692,7 @@ static char * sprinttime(time_t t) { struct tm *tmp; - static char buf[32]; + static char buf[sizeof("yyyy/mm/dd-hh:mm:ss")]; if (t == 0) { strcpy(buf, "0"); @@ -818,8 +820,7 @@ printstat_sparc64(struct tcb *tcp, long addr) if (!abbrev(tcp)) { tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime)); tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime)); - tprintf("st_ctime=%s", sprinttime(statbuf.st_ctime)); - tprints("}"); + tprintf("st_ctime=%s}", sprinttime(statbuf.st_ctime)); } else tprints("...}"); @@ -884,8 +885,7 @@ printstat_powerpc32(struct tcb *tcp, long addr) if (!abbrev(tcp)) { tprintf("st_atime=%s, ", sprinttime(statbuf.st_atime)); tprintf("st_mtime=%s, ", sprinttime(statbuf.st_mtime)); - tprintf("st_ctime=%s", sprinttime(statbuf.st_ctime)); - tprints("}"); + tprintf("st_ctime=%s}", sprinttime(statbuf.st_ctime)); } else tprints("...}"); @@ -1742,8 +1742,7 @@ printcompat_statfs64(struct tcb *tcp, long addr) statbuf.f_fsid.__val[0], statbuf.f_fsid.__val[1]); tprintf(", f_namelen=%lu", (unsigned long)statbuf.f_namelen); tprintf(", f_frsize=%lu", (unsigned long)statbuf.f_frsize); - tprintf(", f_flags=%lu", (unsigned long)statbuf.f_frsize); - tprints("}"); + tprintf(", f_flags=%lu}", (unsigned long)statbuf.f_frsize); } int @@ -2088,6 +2087,7 @@ sys_utime(struct tcb *tcp) long utl[2]; int uti[2]; } u; + unsigned wordsize = personality_wordsize[current_personality]; if (entering(tcp)) { printpath(tcp, tcp->u_arg[0]); @@ -2096,17 +2096,13 @@ sys_utime(struct tcb *tcp) tprints("NULL"); else if (!verbose(tcp)) tprintf("%#lx", tcp->u_arg[1]); - else if (umoven(tcp, tcp->u_arg[1], - 2 * personality_wordsize[current_personality], - (char *) &u) < 0) + else if (umoven(tcp, tcp->u_arg[1], 2 * wordsize, (char *) &u) < 0) tprints("[?, ?]"); - else if (personality_wordsize[current_personality] - == sizeof u.utl[0]) { + else if (wordsize == sizeof u.utl[0]) { tprintf("[%s,", sprinttime(u.utl[0])); tprintf(" %s]", sprinttime(u.utl[1])); } - else if (personality_wordsize[current_personality] - == sizeof u.uti[0]) { + else if (wordsize == sizeof u.uti[0]) { tprintf("[%s,", sprinttime(u.uti[0])); tprintf(" %s]", sprinttime(u.uti[1])); } diff --git a/process.c b/process.c index 621cad8e..a6705ec9 100644 --- a/process.c +++ b/process.c @@ -1007,21 +1007,21 @@ printargv(struct tcb *tcp, long addr) } cp; const char *sep; int n = 0; + unsigned wordsize = personality_wordsize[current_personality]; cp.p64 = 1; for (sep = ""; !abbrev(tcp) || n < max_strlen / 2; sep = ", ", ++n) { - if (umoven(tcp, addr, personality_wordsize[current_personality], - cp.data) < 0) { + if (umoven(tcp, addr, wordsize, cp.data) < 0) { tprintf("%#lx", addr); return; } - if (personality_wordsize[current_personality] == 4) + if (wordsize == 4) cp.p64 = cp.p32; if (cp.p64 == 0) break; tprints(sep); printstr(tcp, cp.p64, -1); - addr += personality_wordsize[current_personality]; + addr += wordsize; } if (cp.p64) tprintf("%s...", sep); diff --git a/resource.c b/resource.c index 09744d74..f5cec215 100644 --- a/resource.c +++ b/resource.c @@ -104,14 +104,15 @@ static const struct xlat resources[] = { }; #if !HAVE_LONG_LONG_RLIM_T -static char * +static const char * sprintrlim(long lim) { - static char buf[32]; + static char buf[sizeof(long)*3 + sizeof("%ld*1024")]; if (lim == RLIM_INFINITY) - sprintf(buf, "RLIM_INFINITY"); - else if (lim > 1024 && lim%1024 == 0) + return "RLIM_INFINITY"; + + if (lim > 1024 && lim%1024 == 0) sprintf(buf, "%ld*1024", lim/1024); else sprintf(buf, "%ld", lim); @@ -192,14 +193,15 @@ sys_setrlimit(struct tcb *tcp) #endif /* !HAVE_LONG_LONG_RLIM_T */ #if _LFS64_LARGEFILE || HAVE_LONG_LONG_RLIM_T -static char * +static const char * sprintrlim64(rlim64_t lim) { - static char buf[64]; + static char buf[sizeof(long long)*3 + sizeof("%lld*1024")]; if (lim == RLIM64_INFINITY) - sprintf(buf, "RLIM64_INFINITY"); - else if (lim > 1024 && lim%1024 == 0) + return "RLIM64_INFINITY"; + + if (lim > 1024 && lim%1024 == 0) sprintf(buf, "%lld*1024", (long long) lim/1024); else sprintf(buf, "%lld", (long long) lim); diff --git a/strace.c b/strace.c index 977c9292..9b026e32 100644 --- a/strace.c +++ b/strace.c @@ -1561,7 +1561,7 @@ extern char *sys_errlist[]; const char * strerror(int err_no) { - static char buf[64]; + static char buf[sizeof("Unknown error %d") + sizeof(int)*3]; if (err_no < 1 || err_no >= sys_nerr) { sprintf(buf, "Unknown error %d", err_no); @@ -1584,7 +1584,7 @@ extern char *_sys_siglist[]; const char * strsignal(int sig) { - static char buf[64]; + static char buf[sizeof("Unknown signal %d") + sizeof(int)*3]; if (sig < 1 || sig >= NSIG) { sprintf(buf, "Unknown signal %d", sig);