From: Jim Warner <james.warner@comcast.net> Date: Tue, 23 Jan 2018 06:00:00 +0000 (-0600) Subject: top: an efficiency tweak to extra wide character logic X-Git-Tag: v3.3.13rc1~24 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3b53aba319f3cc6e26dd428b898d030b3a1c1a07;p=procps-ng top: an efficiency tweak to extra wide character logic 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> --- diff --git a/top/top.c b/top/top.c index 28f903df..30a750f8 100644 --- 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;