From 2451699c35f87a134144c2d61490653bf2bf4f5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 14 Mar 2021 20:47:44 +0100 Subject: [PATCH] =?UTF-8?q?term:=20term=5Fprint():=20assume=20we=E2=80=99r?= =?UTF-8?q?e=20*not*=20at=20the=20right=20margin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit That is, assume we can, and should, increment the cursor column. This changes the emitted assembly from: 518fb: 8b 8b f8 05 00 00 mov 0x5f8(%rbx),%ecx 51901: ff c9 dec %ecx 51903: 39 d1 cmp %edx,%ecx 51905: 7e 11 jle 51918 51907: ff c2 inc %edx 51909: 89 50 10 mov %edx,0x10(%rax) 5190c: 5b pop %rbx 5190d: 5d pop %rbp 5190e: 41 5c pop %r12 51910: c3 ret 51911: 0f 1f 80 00 00 00 00 nopl 0x0(%rax) 51918: c6 40 18 01 movb $0x1,0x18(%rax) 5191c: 5b pop %rbx 5191d: 5d pop %rbp 5191e: 41 5c pop %r12 51920: c3 ret To: 5191c: 41 8d 50 01 lea 0x1(%r8),%edx 51920: 89 50 10 mov %edx,0x10(%rax) 51923: 3b 93 f8 05 00 00 cmp 0x5f8(%rbx),%edx 51929: 0f 8d 21 01 00 00 jge 51a50 5192f: 5b pop %rbx 51930: 5d pop %rbp 51931: 41 5c pop %r12 51933: c3 ret ... 51a50: c6 40 18 01 movb $0x1,0x18(%rax) 51a54: 44 89 40 10 mov %r8d,0x10(%rax) 51a58: 5b pop %rbx 51a59: 5d pop %rbp 51a5a: 41 5c pop %r12 51a5c: c3 ret I.e. it cuts the normal path from 10 instructions down to 8. It increases the "bad" path with one extra instruction. --- terminal.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/terminal.c b/terminal.c index 88599094..85f267e1 100644 --- a/terminal.c +++ b/terminal.c @@ -2848,11 +2848,11 @@ term_print(struct terminal *term, wchar_t wc, int width) } /* Advance cursor */ - if (term->grid->cursor.point.col < term->cols - 1) { - term->grid->cursor.point.col++; - xassert(!term->grid->cursor.lcf); - } else + if (unlikely(++term->grid->cursor.point.col >= term->cols)) { term->grid->cursor.lcf = true; + term->grid->cursor.point.col--; + } else + xassert(!term->grid->cursor.lcf); } enum term_surface