]> granicus.if.org Git - procps-ng/commitdiff
library: improve performance for one 'escape' function
authorJim Warner <james.warner@comcast.net>
Tue, 12 Jan 2021 06:00:00 +0000 (00:00 -0600)
committerCraig Small <csmall@dropbear.xyz>
Thu, 21 Jan 2021 06:30:24 +0000 (17:30 +1100)
While this patch has some cosmetic whitespace changes,
more importantly it makes that 'esc_all' function more
efficient. By abandoning the indexed loop approach for
a direct pointer manipulation, we will save 9 iterated
machine instructions, for a total of 33 bytes of code.

Signed-off-by: Jim Warner <james.warner@comcast.net>
proc/escape.c

index 0b82d31ca3c5963d2957387695f50cf7a98509a2..76be810b5c38d5b44e43a3ef7053a435638a3b1f 100644 (file)
 #include "escape.h"
 #include "readproc.h"
 
-
 #define SECURE_ESCAPE_ARGS(dst, bytes) do { \
   if ((bytes) <= 0) return 0; \
   *(dst) = '\0'; \
   if ((bytes) >= INT_MAX) return 0; \
 } while (0)
 
-static char UTF_tab[] = {
+static const char UTF_tab[] = {
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x00 - 0x0F
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x10 - 0x1F
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x20 - 0x2F
@@ -46,34 +45,34 @@ static char UTF_tab[] = {
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0x90 - 0x9F
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0xA0 - 0xAF
    -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0xB0 - 0xBF
-   -1,-1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xC0 - 0xCF, 0xC2 = begins 2
+   -1,-1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xC0 - 0xCF
     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xD0 - 0xDF
-    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xE0 - 0xEF, 0xE0 = begins 3
-    4, 4, 4, 4, 4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0xF0 - 0xFF, 0xF0 = begins 4
-};                                                  //            ( 0xF5 & beyond invalid )
+    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xE0 - 0xEF
+    4, 4, 4, 4, 4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, // 0xF0 - 0xFF
+};
 
 static const unsigned char ESC_tab[] = {
-   "@..............................."   // 0x00 - 0x1F
-   "||||||||||||||||||||||||||||||||"   // 0x20 - 0x3F
-   "||||||||||||||||||||||||||||||||"   // 0x40 - 0x5f
-   "|||||||||||||||||||||||||||||||."   // 0x60 - 0x7F
-   "????????????????????????????????"   // 0x80 - 0x9F
-   "????????????????????????????????"   // 0xA0 - 0xBF
-   "????????????????????????????????"   // 0xC0 - 0xDF
-   "????????????????????????????????"   // 0xE0 - 0xFF
+   "@..............................." // 0x00 - 0x1F
+   "||||||||||||||||||||||||||||||||" // 0x20 - 0x3F
+   "||||||||||||||||||||||||||||||||" // 0x40 - 0x5f
+   "|||||||||||||||||||||||||||||||." // 0x60 - 0x7F
+   "????????????????????????????????" // 0x80 - 0x9F
+   "????????????????????????????????" // 0xA0 - 0xBF
+   "????????????????????????????????" // 0xC0 - 0xDF
+   "????????????????????????????????" // 0xE0 - 0xFF
 };
 
 static inline void esc_all (unsigned char *str) {
    unsigned char c;
-   int i;
 
    // if bad locale/corrupt str, replace non-printing stuff
-   for (i = 0; str[i] != '\0'; i++)
-      if ((c = ESC_tab[str[i]]) != '|')
-         str[i] = c;
+   while (*str) {
+      if ((c = ESC_tab[*str]) != '|')
+         *str = c;
+      ++str;
+   }
 }
 
-
 static inline void esc_ctl (unsigned char *str, int len) {
    int i, n;
 
@@ -90,7 +89,6 @@ static inline void esc_ctl (unsigned char *str, int len) {
    }
 }
 
-
 int escape_str (unsigned char *dst, const unsigned char *src, int bufsize) {
    static int utf_sw = 0;
    int n;
@@ -113,7 +111,6 @@ int escape_str (unsigned char *dst, const unsigned char *src, int bufsize) {
    return n;
 }
 
-
 int escape_command (unsigned char *outbuf, const proc_t *pp, int bytes, unsigned flags) {
    int overhead = 0;
    int end = 0;