Skip to content

Commit e428858

Browse files
committed
Micro-optimization
1 parent 3e73f86 commit e428858

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

kitty/history.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,9 @@ static inline char_type
326326
pagerhist_remove_char(PagerHistoryBuf *ph, unsigned *count, uint8_t record[8]) {
327327
uint32_t codep, state = UTF8_ACCEPT;
328328
*count = 0;
329-
while (ringbuf_bytes_used(ph->ringbuf)) {
330-
ringbuf_memmove_from(&record[*count], ph->ringbuf, 1);
329+
size_t num = ringbuf_bytes_used(ph->ringbuf);
330+
while (num--) {
331+
record[*count] = ringbuf_move_char(ph->ringbuf);
331332
decode_utf8(&state, &codep, record[*count]);
332333
*count += 1;
333334
if (state == UTF8_REJECT) { codep = 0; break; }

kitty/ringbuf.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,18 @@ ringbuf_memmove_from(void *dst, ringbuf_t src, size_t count)
291291
return src->tail;
292292
}
293293

294+
unsigned char
295+
ringbuf_move_char(ringbuf_t src) {
296+
assert(!ringbuf_is_empty(src));
297+
const uint8_t *bufend = ringbuf_end(src);
298+
assert(bufend > src->tail);
299+
uint8_t ans = *src->tail;
300+
src->tail += 1;
301+
if (src->tail == bufend)
302+
src->tail = src->buf;
303+
return ans;
304+
}
305+
294306
size_t
295307
ringbuf_memcpy_from(void *dst, const ringbuf_t src, size_t count)
296308
{

kitty/ringbuf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ ringbuf_read(int fd, ringbuf_t rb, size_t count);
189189
void *
190190
ringbuf_memmove_from(void *dst, ringbuf_t src, size_t count);
191191

192+
/* ringbuf_memmove_from() optimized for a single character.
193+
* Must only be called if the ringbuf is not empty */
194+
unsigned char
195+
ringbuf_move_char(ringbuf_t src);
196+
192197
/*
193198
* Same as ringbuf_memmove_from() except that it does not change the ringbuffer
194199
* and returns the actual number of bytes copied, which is the minimum of ringbuf_bytes_used

0 commit comments

Comments
 (0)