commit 94ec38e85dbc4ec718934b3bcc765398e2b8ccd4 from: Omar Polo date: Tue Jun 29 18:44:37 2021 UTC optimisation: cache the top_line vline instead of using the line_off + vline_nth, cache the current top line. commit - 60e7ebd308ed9ece004eb91f81feb9343e589334 commit + 94ec38e85dbc4ec718934b3bcc765398e2b8ccd4 blob - 4d64069ea7d59c6276e1dbe0ff26a8dbbb8c533a blob + b5cff45710f8bacfe9d23f92805d6e7fe19748f0 --- cmd.c +++ cmd.c @@ -141,6 +141,7 @@ cmd_scroll_line_up(struct buffer *buffer) return; buffer->line_off--; + buffer->top_line = TAILQ_PREV(buffer->top_line, vhead, vlines); buffer->current_line = TAILQ_PREV(buffer->current_line, vhead, vlines); restore_cursor(buffer); } @@ -157,7 +158,10 @@ cmd_scroll_line_down(struct buffer *buffer) return; buffer->current_line = vl; + + buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines); buffer->line_off++; + restore_cursor(buffer); } @@ -187,6 +191,7 @@ void cmd_beginning_of_buffer(struct buffer *buffer) { buffer->current_line = TAILQ_FIRST(&buffer->head); + buffer->top_line = buffer->current_line; buffer->line_off = 0; buffer->curs_y = 0; buffer->cpoff = 0; @@ -200,6 +205,15 @@ cmd_end_of_buffer(struct buffer *buffer) off = buffer->line_max - body_lines; off = MAX(0, off); + + while (buffer->line_off > (size_t)off) { + buffer->line_off--; + buffer->top_line = TAILQ_PREV(buffer->top_line, vhead, vlines); + } + while (buffer->line_off < (size_t)off) { + buffer->line_off++; + buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines); + } buffer->line_off = off; buffer->curs_y = MIN((size_t)body_lines-1, buffer->line_max-1); blob - d1627087376d67b890105acc9638195bb0c13202 blob + 8ba69fbf498995de86497b1f3d19c5b144c6abff --- telescope.h +++ telescope.h @@ -200,6 +200,7 @@ struct buffer { int curs_y; size_t line_off; size_t line_max; + struct vline *top_line; struct vline *current_line; size_t cpoff; TAILQ_HEAD(vhead, vline) head; blob - 919c538e2658fe1a8347a999c4faea6e64c819ba blob + 2b0cc30aa67a7e932f2f09f29e4896693dd2c3dc --- ui.c +++ ui.c @@ -568,9 +568,9 @@ wrap_page(struct buffer *buffer, int width) int pre_width; const char *prfx; - orig = buffer->current_line == NULL - ? NULL - : buffer->current_line->parent; + orig = buffer->current_line == NULL ? NULL : buffer->current_line->parent; + + buffer->top_line = NULL; buffer->current_line = NULL; buffer->force_redraw = 1; @@ -619,6 +619,8 @@ wrap_page(struct buffer *buffer, int width) if (buffer->current_line == NULL) buffer->current_line = TAILQ_FIRST(&buffer->head); + buffer->top_line = buffer->current_line; + return 1; } @@ -792,8 +794,7 @@ redraw_window(WINDOW *win, int height, int width, stru goto end; l = 0; - vl = nth_line(buffer, buffer->line_off); - for (; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) { + for (vl = buffer->top_line; vl != NULL; vl = TAILQ_NEXT(vl, vlines)) { wmove(win, l, 0); print_vline(x_offset, width, win, vl); l++;