From 7c7720a3ab314e471499194f114797ca7a40afde Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 22 Aug 2019 17:33:23 +0200 Subject: scrolling: optimize row access by assuming number of rows is a power of 2 With this assumption, we can replace 'a % b' with 'a & (b - 1)'. In terms of instructions, this means a fast 'and' instead of a slow 'div'. Further optimize scrolling by: * not double-initializing empty rows. Previously, grid_row_alloc() called calloc(), which was then followed by a memset() when scrolling. This is of course unnecessary. * Don't loop the entire set of visible rows (this was done to ensure all visible rows had been allocated, and to prefetch the cell contents). This isn't necessary; only newly pulled in rows can be NULL. For now, don't prefetch at all. --- commands.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'commands.c') diff --git a/commands.c b/commands.c index 4fe50a8f..01e73981 100644 --- a/commands.c +++ b/commands.c @@ -19,7 +19,11 @@ cmd_scrollback_up(struct terminal *term, int rows) rows = min(rows, term->rows); assert(term->grid->offset >= 0); - int new_view = (term->grid->view + term->grid->num_rows - rows) % term->grid->num_rows; + int new_view = term->grid->view - rows; + while (new_view < 0) + new_view += term->grid->num_rows; + new_view %= term->grid->num_rows; + assert(new_view >= 0); assert(new_view < term->grid->num_rows); -- cgit v1.3