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>
This commit is contained in:
Jim Warner 2018-01-23 00:00:00 -06:00 committed by Craig Small
parent 2167dcbccb
commit 3b53aba319

View File

@ -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;