From e428858ad60faced9dda452641fcc18d97f72c21 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 12 Dec 2020 17:43:33 +0530 Subject: [PATCH] Micro-optimization --- kitty/history.c | 5 +++-- kitty/ringbuf.c | 12 ++++++++++++ kitty/ringbuf.h | 5 +++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/kitty/history.c b/kitty/history.c index 64510f857..0a6df0f02 100644 --- a/kitty/history.c +++ b/kitty/history.c @@ -326,8 +326,9 @@ static inline char_type pagerhist_remove_char(PagerHistoryBuf *ph, unsigned *count, uint8_t record[8]) { uint32_t codep, state = UTF8_ACCEPT; *count = 0; - while (ringbuf_bytes_used(ph->ringbuf)) { - ringbuf_memmove_from(&record[*count], ph->ringbuf, 1); + size_t num = ringbuf_bytes_used(ph->ringbuf); + while (num--) { + record[*count] = ringbuf_move_char(ph->ringbuf); decode_utf8(&state, &codep, record[*count]); *count += 1; if (state == UTF8_REJECT) { codep = 0; break; } diff --git a/kitty/ringbuf.c b/kitty/ringbuf.c index aac107211..1588adef5 100644 --- a/kitty/ringbuf.c +++ b/kitty/ringbuf.c @@ -291,6 +291,18 @@ ringbuf_memmove_from(void *dst, ringbuf_t src, size_t count) return src->tail; } +unsigned char +ringbuf_move_char(ringbuf_t src) { + assert(!ringbuf_is_empty(src)); + const uint8_t *bufend = ringbuf_end(src); + assert(bufend > src->tail); + uint8_t ans = *src->tail; + src->tail += 1; + if (src->tail == bufend) + src->tail = src->buf; + return ans; +} + size_t ringbuf_memcpy_from(void *dst, const ringbuf_t src, size_t count) { diff --git a/kitty/ringbuf.h b/kitty/ringbuf.h index 5dc31806f..04be0d0f3 100644 --- a/kitty/ringbuf.h +++ b/kitty/ringbuf.h @@ -189,6 +189,11 @@ ringbuf_read(int fd, ringbuf_t rb, size_t count); void * ringbuf_memmove_from(void *dst, ringbuf_t src, size_t count); +/* ringbuf_memmove_from() optimized for a single character. + * Must only be called if the ringbuf is not empty */ +unsigned char +ringbuf_move_char(ringbuf_t src); + /* * Same as ringbuf_memmove_from() except that it does not change the ringbuffer * and returns the actual number of bytes copied, which is the minimum of ringbuf_bytes_used