From cf71534768b01e2a94a8f8f13faca2fb1920e93e Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 10 Jul 2019 18:45:12 +0200 Subject: terminal: remove 'blink' attribute from cell struct This makes the other attribute bits fit in a single uint8_t, which makes each cell smaller which improves cache usage. --- csi.c | 61 +++++++++++++++++++++++++++++++------------------------------ terminal.h | 2 +- 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/csi.c b/csi.c index 3311b7b3..51f7705d 100644 --- a/csi.c +++ b/csi.c @@ -79,6 +79,33 @@ sgr_reset(struct terminal *term) term->vt.attrs.background = term->background; } +static const char * +csi_as_string(struct terminal *term, uint8_t final) +{ + static char msg[1024]; + int c = snprintf(msg, sizeof(msg), "CSI: "); + + if (term->vt.private != 0) + c += snprintf(&msg[c], sizeof(msg) - c, "%c", term->vt.private); + + for (size_t i = 0; i < term->vt.params.idx; i++){ + c += snprintf(&msg[c], sizeof(msg) - c, "%d", + term->vt.params.v[i].value); + + for (size_t j = 0; i < term->vt.params.v[i].sub.idx; j++) { + c += snprintf(&msg[c], sizeof(msg) - c, ":%d", + term->vt.params.v[i].sub.value[j]); + } + + c += snprintf(&msg[c], sizeof(msg) - c, "%s", + i == term->vt.params.idx - 1 ? "" : ";"); + } + + c += snprintf(&msg[c], sizeof(msg) - c, "%c (%zu parameters)", + final, term->vt.params.idx); + return msg; +} + static void csi_sgr(struct terminal *term) { @@ -99,8 +126,8 @@ csi_sgr(struct terminal *term) case 2: term->vt.dim = true; break; case 3: term->vt.attrs.italic = true; break; case 4: term->vt.attrs.underline = true; break; - case 5: term->vt.attrs.blink = true; break; - case 6: term->vt.attrs.blink = true; break; + case 5: LOG_WARN("unimplemented: %s (blink)", csi_as_string(term, 'm')); /*term->vt.attrs.blink = true; */break; + case 6: break; /* rapid blink, ignored */ case 7: term->vt.attrs.reverse = true; break; case 8: term->vt.attrs.conceal = true; break; case 9: term->vt.attrs.strikethrough = true; break; @@ -108,7 +135,8 @@ csi_sgr(struct terminal *term) case 22: term->vt.attrs.bold = term->vt.dim = false; break; case 23: term->vt.attrs.italic = false; break; case 24: term->vt.attrs.underline = false; break; - case 25: term->vt.attrs.blink = false; break; + case 25: /*term->vt.attrs.blink = false; */break; + case 26: break; /* rapid blink, ignored */ case 27: term->vt.attrs.reverse = false; break; case 28: term->vt.attrs.conceal = false; break; case 29: term->vt.attrs.strikethrough = false; break; @@ -239,33 +267,6 @@ csi_sgr(struct terminal *term) } } -static const char * -csi_as_string(struct terminal *term, uint8_t final) -{ - static char msg[1024]; - int c = snprintf(msg, sizeof(msg), "CSI: "); - - if (term->vt.private != 0) - c += snprintf(&msg[c], sizeof(msg) - c, "%c", term->vt.private); - - for (size_t i = 0; i < term->vt.params.idx; i++){ - c += snprintf(&msg[c], sizeof(msg) - c, "%d", - term->vt.params.v[i].value); - - for (size_t j = 0; i < term->vt.params.v[i].sub.idx; j++) { - c += snprintf(&msg[c], sizeof(msg) - c, ":%d", - term->vt.params.v[i].sub.value[j]); - } - - c += snprintf(&msg[c], sizeof(msg) - c, "%s", - i == term->vt.params.idx - 1 ? "" : ";"); - } - - c += snprintf(&msg[c], sizeof(msg) - c, "%c (%zu parameters)", - final, term->vt.params.idx); - return msg; -} - void csi_dispatch(struct terminal *term, uint8_t final) { diff --git a/terminal.h b/terminal.h index 4ddf017e..66cfebe0 100644 --- a/terminal.h +++ b/terminal.h @@ -56,7 +56,7 @@ struct attributes { uint8_t italic:1; uint8_t underline:1; uint8_t strikethrough:1; - uint8_t blink:1; + //uint8_t blink:1; /* Not supported yet, and removing it means all other attributes fit in a single uint8_t */ uint8_t conceal:1; uint8_t reverse:1; uint8_t have_foreground:1; -- cgit v1.3 From b1f8dd75d684b3dd00f147a63ef687a6b566fc7f Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 10 Jul 2019 18:47:32 +0200 Subject: cell: we only need 4 bytes for the longest utf8 sequence We don't *have* to NULL-terminate the utf8 string. So don't. This makes the cell glyph 4 bytes exactly, which is better for alignment and cache usage. --- render.c | 2 +- terminal.h | 2 +- vt.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/render.c b/render.c index 65e2ab96..894bc939 100644 --- a/render.c +++ b/render.c @@ -101,7 +101,7 @@ render_cell(struct terminal *term, struct buffer *buf, const struct cell *cell, cairo_status_t status = cairo_scaled_font_text_to_glyphs( attrs_to_font(term, &cell->attrs), x, y + term->fextents.ascent, - cell->c, strlen(cell->c), &gseq.g, &new_glyphs, + cell->c, strnlen(cell->c, 4), &gseq.g, &new_glyphs, NULL, NULL, NULL); if (status != CAIRO_STATUS_SUCCESS) diff --git a/terminal.h b/terminal.h index 66cfebe0..16e53f22 100644 --- a/terminal.h +++ b/terminal.h @@ -68,7 +68,7 @@ struct attributes { struct cell { struct attributes attrs; - char c[5]; + char c[4]; } __attribute__((packed)); struct scroll_region { diff --git a/vt.c b/vt.c index 8314c060..5d4127c5 100644 --- a/vt.c +++ b/vt.c @@ -757,7 +757,7 @@ action_print(struct terminal *term, uint8_t c) if (unlikely(term->charset[term->selected_charset] == CHARSET_GRAPHIC) && c >= 0x41 && c <= 0x7e) { - strcpy(cell->c, vt100_0[c - 0x41]); + strncpy(cell->c, vt100_0[c - 0x41], sizeof(cell->c)); } else { //LOG_DBG("print: ASCII: %c", c); cell->c[0] = c; -- cgit v1.3 From 74ab12010185735c6a1afa1029be3824fa56fc92 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 10 Jul 2019 18:48:46 +0200 Subject: terminal: use floats instead of double for colors We don't need the full resolution a double gives us. Using floats reduces the size of cells greatly, which improves cache usage. --- terminal.h | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/terminal.h b/terminal.h index 16e53f22..fdb86a81 100644 --- a/terminal.h +++ b/terminal.h @@ -38,20 +38,13 @@ struct wayland { bool have_argb8888; }; -struct rgb { double r, g, b; } __attribute__((packed)); +struct rgb { float r, g, b; } __attribute__((packed)); +/* + * Note: we want the cells to be as small as possible. Larger cells + * means fewer scrollback lines (or performance drops due to cache + * misses) */ struct attributes { -#if 0 - bool bold; - bool italic; - bool underline; - bool strikethrough; - bool blink; - bool conceal; - bool reverse; - bool have_foreground; - bool have_background; -#else uint8_t bold:1; uint8_t italic:1; uint8_t underline:1; @@ -61,7 +54,7 @@ struct attributes { uint8_t reverse:1; uint8_t have_foreground:1; uint8_t have_background:1; -#endif + struct rgb foreground; /* Only valid when have_foreground == true */ struct rgb background; /* Only valid when have_background == true */ } __attribute__((packed)); -- cgit v1.3 From 4ffd93ca4cb8abc54aacb3e554d0b91225d9376d Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 10 Jul 2019 18:49:34 +0200 Subject: render: reduce scrollback line count More than 1000 lines and we start exhibiting bad cache behavior --- render.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/render.c b/render.c index 894bc939..a97e53cb 100644 --- a/render.c +++ b/render.c @@ -379,7 +379,7 @@ render_resize(struct terminal *term, int width, int height) term->width = width; term->height = height; - const int scrollback_lines = 10000; + const int scrollback_lines = 1000; const int old_cols = term->cols; const int old_rows = term->rows; -- cgit v1.3 From 379f7cf646c9543868ae1262fd3cf9e79f537c9a Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 10 Jul 2019 19:17:53 +0200 Subject: grid: swap_row: drop requirement that row indices be positive --- grid.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/grid.c b/grid.c index 2ffae9a3..a3f77dba 100644 --- a/grid.c +++ b/grid.c @@ -12,12 +12,13 @@ grid_swap_row(struct grid *grid, int row_a, int row_b) { assert(grid->offset >= 0); assert(row_a != row_b); - assert(row_a >= 0); - assert(row_b >= 0); int real_a = (grid->offset + row_a + grid->num_rows) % grid->num_rows; int real_b = (grid->offset + row_b + grid->num_rows) % grid->num_rows; + assert(real_a >= 0); + assert(real_b >= 0); + struct row *tmp = grid->rows[real_a]; grid->rows[real_a] = grid->rows[real_b]; grid->rows[real_b] = tmp; -- cgit v1.3 From 1d7bf3fbca011a8f3e09ef72398494c8ea55b16f Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 10 Jul 2019 19:18:36 +0200 Subject: terminal: scrolling: better debug log messages --- terminal.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/terminal.c b/terminal.c index 02795797..7a054692 100644 --- a/terminal.c +++ b/terminal.c @@ -146,7 +146,8 @@ term_cursor_down(struct terminal *term, int count) void term_scroll_partial(struct terminal *term, struct scroll_region region, int rows) { - LOG_DBG("scroll: %d rows", rows); + LOG_DBG("scroll: rows=%d, region.start=%d, region.end=%d", + rows, region.start, region.end); assert(rows < term->rows && "unimplemented"); @@ -185,6 +186,9 @@ void term_scroll_reverse_partial(struct terminal *term, struct scroll_region region, int rows) { + LOG_DBG("scroll reverse: rows=%d, region.start=%d, region.end=%d", + rows, region.start, region.end); + assert(rows < term->rows && "unimplemented"); bool view_follows = term->grid->view == term->grid->offset; -- cgit v1.3 From e4a631f7f097426e70d24e3a2ec50f7626ae860f Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 10 Jul 2019 19:18:53 +0200 Subject: terminal: scrolling: prefetch cells of scrolled in lines Since newly scrolled in lines will be erased, we want them in the cache. So, allocate and prefetch new lines, then repair non-scrolling regions (to give the prefetch time to actually fetch the data). Finally, erase the newly scrolled in lines. This also fixes a bug where the fixup for the non-scrolling regions could crash on an unallocated row (we were accessing rows that would be, but hadn't yet been, scrolled in). --- terminal.c | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/terminal.c b/terminal.c index 7a054692..f0fa3cfc 100644 --- a/terminal.c +++ b/terminal.c @@ -151,15 +151,6 @@ term_scroll_partial(struct terminal *term, struct scroll_region region, int rows assert(rows < term->rows && "unimplemented"); - /* Top non-scrolling region */ - for (int i = region.start - 1; i >= 0; i--) - grid_swap_row(term->grid, i, i + rows); - - /* Bottom non-scrolling region */ - for (int i = term->rows - 1; i >= region.end; i--) - grid_swap_row(term->grid, i, i + rows); - - /* Offset grid origin */ bool view_follows = term->grid->view == term->grid->offset; term->grid->offset += rows; term->grid->offset %= term->grid->num_rows; @@ -167,11 +158,28 @@ term_scroll_partial(struct terminal *term, struct scroll_region region, int rows if (view_follows) term->grid->view = term->grid->offset; - for (int r = max(region.end - rows, 0); r < region.end; r++) { - struct row *row = grid_row(term->grid, r); - erase_line(term, row); + /* + * This loop serves two purposes: + * 1) ensure all visible lines are *allocated* + * 2) prefetch the cells - this makes life easier for erase_line() below + */ + for (int r = max(region.end - rows, 0); r < term->rows; r++) { + struct row *row = grid_row(term->grid, r); + __builtin_prefetch(row->cells, 1, 3); } + /* Top non-scrolling region. */ + for (int i = region.start - 1; i >= 0; i--) + grid_swap_row(term->grid, i - rows, i); + + /* Bottom non-scrolling region */ + for (int i = term->rows - 1; i >= region.end; i--) + grid_swap_row(term->grid, i - rows, i); + + /* Erase scrolled in lines */ + for (int r = max(region.end - rows, 0); r < region.end; r++) + erase_line(term, grid_row(term->grid, r)); + term_damage_scroll(term, DAMAGE_SCROLL, region, rows); term->grid->cur_row = grid_row(term->grid, term->cursor.row); } @@ -192,13 +200,17 @@ term_scroll_reverse_partial(struct terminal *term, assert(rows < term->rows && "unimplemented"); bool view_follows = term->grid->view == term->grid->offset; - term->grid->offset += term->grid->num_rows - rows; term->grid->offset %= term->grid->num_rows; if (view_follows) term->grid->view = term->grid->offset; + for (int r = 0; r < min(region.start + rows, region.end); r++) { + struct row *row = grid_row(term->grid, r); + __builtin_prefetch(row->cells, 1, 3); + } + /* Bottom non-scrolling region */ for (int i = region.end + rows; i < term->rows + rows; i++) grid_swap_row(term->grid, i, i - rows); @@ -207,10 +219,9 @@ term_scroll_reverse_partial(struct terminal *term, for (int i = 0 + rows; i < region.start + rows; i++) grid_swap_row(term->grid, i, i - rows); - term_erase( - term, - &(struct coord){0, region.start}, - &(struct coord){term->cols - 1, min(region.start + rows, region.end) - 1}); + /* Erase scrolled in lines */ + for (int r = region.start; r < min(region.start + rows, region.end); r++) + erase_line(term, grid_row(term->grid, r)); term_damage_scroll(term, DAMAGE_SCROLL_REVERSE, region, rows); term->grid->cur_row = grid_row(term->grid, term->cursor.row); -- cgit v1.3