]> granicus.if.org Git - procps-ng/commitdiff
top: an efficiency tweak to extra wide character logic
authorJim Warner <james.warner@comcast.net>
Tue, 23 Jan 2018 06:00:00 +0000 (00:00 -0600)
committerCraig Small <csmall@enc.com.au>
Mon, 19 Feb 2018 09:37:24 +0000 (20:37 +1100)
When I recently added extra wide character support for
locales like zh_CN, I didn't worry about some overhead
associated with the new calls to 'mbtowc' & 'wcwidth'.
That's because such overhead was usually incurred with
user interactions, not a normal iterative top display.

There was, however, one area where this overhead would
impact the normal iterative top mode - that's with the
Summary display. So I peeked at the glibc source code.

As it turns out, the costs of executing those 'mbtowc'
and 'wcwidth' functions were not at all insignificant.
So, this patch will avoid them in the vast majority of
instances, while still enabling extra wide characters.

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

index 28f903df7007d590d56e64101232237d378961bf..30a750f80525949a397a4e04d0daa17a5407e7b3 100644 (file)
--- a/top/top.c
+++ b/top/top.c
@@ -687,11 +687,12 @@ static char UTF8_tab[] = {
 static inline int utf8_cols (const unsigned char *p, int n) {
 #ifndef OFF_XTRAWIDE
    wchar_t wc;
-   int wlen;
 
-   (void)mbtowc(&wc, (const char *)p, n);
-   if ((wlen = wcwidth(wc)) < 1) wlen = 1;
-   return wlen;
+   if (n > 1) {
+      (void)mbtowc(&wc, (const char *)p, n);
+      if ((n = wcwidth(wc)) < 1) n = 1;
+   }
+   return n;
 #else
    (void)p; (void)n;
    return 1;