]> granicus.if.org Git - strace/commitdiff
Simple optimizations
authorDenys Vlasenko <vda.linux@googlemail.com>
Sun, 29 Jan 2012 21:38:35 +0000 (22:38 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sun, 29 Jan 2012 21:38:35 +0000 (22:38 +0100)
   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>
file.c
signal.c
strace.c
util.c

diff --git a/file.c b/file.c
index b2567c5b63906957df4702e5c8ce0a7886efe2e2..87e07438d78e1b5b8b1365dac804a0beec0554e7 100644 (file)
--- 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;
 }
index aca6345f6e191507a2ae4c3a15fa1bddbda34ee6..4556a5fecc57aaea5cabbc8baf4408a62b0b2fa9 100644 (file)
--- 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;
index b739cd62182ce6fc2860e337880f69a1213e02e7..38c8518aa1c70e5a9d7f4adad1062d3c4e69766e 100644 (file)
--- 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 2a73db78808e0c3d074e0387816dc48c1e31eae2..b6cdf9a5d211965ace2b9a7772b0c368d050fd05 100644 (file)
--- 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) {