aboutsummaryrefslogtreecommitdiff
path: root/src/work.c
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-11-13 20:50:11 +0100
committerThomas Voss <mail@thomasvoss.com> 2024-11-13 20:50:11 +0100
commit0cf413c8a6a060fbc532f645202b01cf8ef27fef (patch)
tree05a41abb40ff4462c2d535082afe6bbc0065fbc1 /src/work.c
parentaa8fe6e1094651217771cb8744af73b1418ff9b6 (diff)
Try to speed things up a little
Diffstat (limited to 'src/work.c')
-rw-r--r--src/work.c29
1 files changed, 23 insertions, 6 deletions
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 <sys/mman.h>
#include <sys/stat.h>
+#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdatomic.h>
@@ -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);
+ }
}
}