From 0cf413c8a6a060fbc532f645202b01cf8ef27fef Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Wed, 13 Nov 2024 20:50:11 +0100 Subject: Try to speed things up a little --- src/work.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'src/work.c') diff --git a/src/work.c b/src/work.c index 38dfe07..b6433b8 100644 --- a/src/work.c +++ b/src/work.c @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -26,6 +27,10 @@ #include "util.h" #include "work.h" +#ifndef __GNUC__ + #define __builtin_expect(x, y) (x) +#endif + #define DEFINE_OPERATOR(fn) \ void operator_##fn(ptrdiff_t opi, u8view_t sv, u8view_t **hl) #define array_extend_sv(xs, sv) \ @@ -457,12 +462,24 @@ compute_pos(const char8_t *p, pos_state_t *ps) { u8view_t g; while (ps->buf.p < p) { - ucsgnext(&g, &ps->buf); - if (islbrk(g)) { - ps->row++; - ps->col = 0; - } else - ps->col = ucswdth(g, ps->col, grab_tabsize); + unsigned char c = *ps->buf.p; + if (__builtin_expect(isascii(c), 1)) { /* ASCII fast path */ + ps->buf.p++; + if (c == '\r' || c == '\n' || c == '\v' || c == '\f') { + ps->row++; + ps->col = 0; + } else if (c == '\t') + ps->col += grab_tabsize - ps->col % grab_tabsize; + else if (isprint(c)) + ps->col++; + } else { + ucsgnext(&g, &ps->buf); + if (islbrk(g)) { + ps->row++; + ps->col = 0; + } else + ps->col = ucswdth(g, ps->col, grab_tabsize); + } } } -- cgit v1.2.3