logd: optimize code hotspots

Discovered that we had a few libc hotspots. Adjust code to generally
reduce or nullify the number of calls to malloc, free, strlen,
strcmp, strncmp, memcmp & strncasecmp. Total gain looks to be about
3% of logd's processing time. malloc still contributes to 3%, but all
others are now total 0.5%.

Bug: 23685592
Change-Id: Ife721121667969260cdb8b055524ae90f5911278
diff --git a/logd/LogKlog.cpp b/logd/LogKlog.cpp
index 242d7a0..d28161e 100644
--- a/logd/LogKlog.cpp
+++ b/logd/LogKlog.cpp
@@ -306,7 +306,7 @@
                 }
                 --len;
             } while (*s++ != c);
-        } while (memcmp(s, needle, needleLen) != 0);
+        } while (fast<memcmp>(s, needle, needleLen));
         s--;
     }
     return s;
@@ -588,7 +588,7 @@
         const char *bt, *et, *cp;
 
         bt = p;
-        if (!strncmp(p, "[INFO]", 6)) {
+        if (!fast<strncmp>(p, "[INFO]", 6)) {
             // <PRI>[<TIME>] "[INFO]"<tag> ":" message
             bt = p + 6;
             taglen -= 6;
@@ -612,10 +612,10 @@
             p = cp + 1;
         } else if (taglen) {
             size = et - bt;
-            if (strncmp(bt, cp, size)) {
+            if ((*bt == *cp) && fast<strncmp>(bt + 1, cp + 1, size - 1)) {
                 // <PRI>[<TIME>] <tag>_host '<tag>.<num>' : message
-                if (!strncmp(bt + size - 5, "_host", 5)
-                        && !strncmp(bt, cp, size - 5)) {
+                if (!fast<strncmp>(bt + size - 5, "_host", 5)
+                        && !fast<strncmp>(bt + 1, cp + 1, size - 6)) {
                     const char *b = cp;
                     cp += size - 5;
                     taglen -= size - 5;
@@ -694,10 +694,10 @@
             // register names like x18 but not driver names like en0
                 || ((size == 3) && (isdigit(tag[1]) && isdigit(tag[2])))
             // blacklist
-                || ((size == 3) && !strncmp(tag, "CPU", 3))
-                || ((size == 7) && !strncasecmp(tag, "WARNING", 7))
-                || ((size == 5) && !strncasecmp(tag, "ERROR", 5))
-                || ((size == 4) && !strncasecmp(tag, "INFO", 4))) {
+                || ((size == 3) && !fast<strncmp>(tag, "CPU", 3))
+                || ((size == 7) && !fast<strncasecmp>(tag, "WARNING", 7))
+                || ((size == 5) && !fast<strncasecmp>(tag, "ERROR", 5))
+                || ((size == 4) && !fast<strncasecmp>(tag, "INFO", 4))) {
             p = start;
             etag = tag = "";
         }
@@ -709,7 +709,7 @@
     const char *mp = strnrchr(tag, ']', taglen);
     if (mp && (++mp < etag)) {
         size_t s = etag - mp;
-        if (((s + s) < taglen) && !memcmp(mp, mp - 1 - s, s)) {
+        if (((s + s) < taglen) && !fast<memcmp>(mp, mp - 1 - s, s)) {
             taglen = mp - tag;
         }
     }
@@ -727,6 +727,9 @@
         p = " ";
         b = 1;
     }
+    if (b > LOGGER_ENTRY_MAX_PAYLOAD) {
+        b = LOGGER_ENTRY_MAX_PAYLOAD;
+    }
     size_t n = 1 + taglen + 1 + b + 1;
     int rc = n;
     if ((taglen > n) || (b > n)) { // Can not happen ...
@@ -734,12 +737,7 @@
         return rc;
     }
 
-    // Allocate a buffer to hold the interpreted log message
-    char *newstr = reinterpret_cast<char *>(malloc(n));
-    if (!newstr) {
-        rc = -ENOMEM;
-        return rc;
-    }
+    char newstr[n];
     char *np = newstr;
 
     // Convert priority into single-byte Android logger priority
@@ -759,7 +757,6 @@
     // Log message
     rc = logbuf->log(LOG_ID_KERNEL, now, uid, pid, tid, newstr,
                      (n <= USHRT_MAX) ? (unsigned short) n : USHRT_MAX);
-    free(newstr);
 
     // notify readers
     if (!rc) {