commit 991008dc460854b5f2f978a87946f9d90e3e5ee5
parent e3461920b100b12eaaca9664ce161519966b58a9
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Mon,  9 Mar 2020 19:16:52 +0100

sfeed_plain: optimize utf8-decoding and column position calculation

Optimize for the common-case: assuming ASCII.
The input is assumed to be valid UTF-8 input (output of sfeed).

This saves 2 function calls for determining the width of a single ASCII
character, which of course is 1.

Ranges:
< 32 are control-characters and are skipped.
< 127 is typical ASCII and is 1 column wide.
>= 127 is the normal path (control-character and multi-byte UTF-8).

Tested on OpenBSD and Linux with various compilers (clang, gcc, pcc and tcc).
On OpenBSD and Linux glibc much improvement. On Linux musl (almost) no change.

In a common-case upto 40% performance improvement.
In the worst-case negligible performance degration (<1%).

Diffstat:
Mutil.c | 11++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/util.c b/util.c @@ -247,10 +247,15 @@ printutf8pad(FILE *fp, const char *s, size_t len, int pad) slen = strlen(s); for (i = 0; i < slen; i += rl) { - if ((rl = mbtowc(&wc, s + i, slen - i < 4 ? slen - i : 4)) <= 0) - break; - if ((w = wcwidth(wc)) == -1) + rl = w = 1; + if ((unsigned char)s[i] < 32) continue; + if ((unsigned char)s[i] >= 127) { + if ((rl = mbtowc(&wc, s + i, slen - i < 4 ? slen - i : 4)) <= 0) + break; + if ((w = wcwidth(wc)) == -1) + continue; + } if (col + w > len || (col + w == len && s[i + rl])) { fputs("\xe2\x80\xa6", fp); col++;