From 3b53aba319f3cc6e26dd428b898d030b3a1c1a07 Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Tue, 23 Jan 2018 00:00:00 -0600 Subject: [PATCH] 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 --- top/top.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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;