Skip to content

Conversation

justinmk
Copy link
Member

@justinmk justinmk commented Dec 1, 2017

For some reason, enabling focus reporting during terminal setup, causes
slow rendering during Nvim startup on tmux 2.3 with the tmux
focus-events option enabled.

To workaround that issue, this commit defers the request.

closes #7649

Using script to record the terminal session (and vterm-dump to
post-process the result):

BEFORE this commit:

./build/bin/nvim -u NONE{CR}{LF}
{DECSM 1049}{DECSM 1}{ESC =}
{CUP *}{ED 2}{DECSM 2004}{DECSM 1004}{CSI 8,44,156 t}{CSI * r}
{CUP 1,1}
{CUP *}{ED 2}{DECSM 25}
{DECRM 25}{CSI 2   q}{CSI 2   q}
{CUP *}{ED 2}{LF}
{ESC (B}{SGR *}{SGR 94}~                                                                                                                                                           {CR}{LF}
~                                                                                                                                                           {CR}{LF}

AFTER this commit:

./build/bin/nvim -u NONE{CR}{LF}
{DECSM 1049}{DECSM 1}{ESC =}
{CUP *}{ED 2}{CSI 8,44,156 t}{CSI * r}
{CUP 1,1}
{CUP *}{ED 2}{DECSM 2004}{DECSM 1004}{DECSM 25}
{DECRM 25}{CSI 2   q}{CSI 2   q}
{CUP *}{ED 2}{LF}
{ESC (B}{SGR *}{SGR 94}~                                                                                                                                                           {CR}{LF}
~                                                                                                                                                           {CR}{LF}
...

For some reason, enabling focus reporting during terminal setup, causes
slow rendering during Nvim startup on tmux 2.3 with the tmux
`focus-events` option enabled.

To workaround that issue, this commit defers the request.

closes neovim#7649

init.vim:
    call plug#begin('~/.config/nvim/plugged')
    Plug 'morhetz/gruvbox'
    call plug#end()
    set background=light " background light just to see the effect more quickly
    colorscheme gruvbox
.tmux.conf:
    set -g focus-events on
    set-option -ga terminal-overrides ",xterm-256color:Tc"
    set-option -g default-terminal "screen-256color"

Using `script` to record the terminal session (and `vterm-dump` to
post-process the result):

BEFORE this commit:
    ./build/bin/nvim -u NONE{CR}{LF}
    {DECSM 1049}{DECSM 1}{ESC =}
    {CUP *}{ED 2}{DECSM 2004}{DECSM 1004}{CSI 8,44,156 t}{CSI * r}
    {CUP 1,1}
    {CUP *}{ED 2}{DECSM 25}
    {DECRM 25}{CSI 2   q}{CSI 2   q}
    {CUP *}{ED 2}{LF}
    {ESC (B}{SGR *}{SGR 94}~                                                                                                                                                           {CR}{LF}
    ~                                                                                                                                                           {CR}{LF}

AFTER this commit:
    ./build/bin/nvim -u NONE{CR}{LF}
    {DECSM 1049}{DECSM 1}{ESC =}
    {CUP *}{ED 2}{CSI 8,44,156 t}{CSI * r}
    {CUP 1,1}
    {CUP *}{ED 2}{DECSM 2004}{DECSM 1004}{DECSM 25}
    {DECRM 25}{CSI 2   q}{CSI 2   q}
    {CUP *}{ED 2}{LF}
    {ESC (B}{SGR *}{SGR 94}~                                                                                                                                                           {CR}{LF}
    ~                                                                                                                                                           {CR}{LF}
    ...
@justinmk justinmk merged commit 27f9b1c into neovim:master Dec 1, 2017
@justinmk justinmk deleted the tui-deferred-startup branch December 1, 2017 03:18
justinmk added a commit to justinmk/neovim that referenced this pull request Dec 2, 2017
ref neovim#7649
ref neovim#7664

27f9b1c caused a regression: it uses loop_schedule_deferred() to
defer emitting the "enable focus reporting" termcode. tui_main() never
processes `tui_loop.events` (which loop_schedule_deferred() depends on),
so the event was never actually processed.

But fixing that (by processing `tui_loop.events`) would bring back the
problem 27f9b1c tried to fix: it still emits the event too soon.

Instead, do a little dance: schedule the event on `main_loop` and then
forward it to `tui_loop`.

NOTE: after this commit, in tmux 2.3 with `focus-events` enabled,
FocusGained is fired on startup and when resuming from suspend.

Using `script` to record the terminal session (and `vterm-dump` to
post-process the result):

BEFORE:
    {DECSM 1049}{DECSM 1}{ESC =}
    {CUP *}{ED *}{DECSM 2004}{DECSM 1004}{CSI 1,43 r}
    {CUP 1,1}
    {CUP *}{ED *}{SM 34}{DECSM 25}
    {DECRM 25}{CSI 2   q}{CSI 2   q}
    {CUP *}{ED *}{LF}
    {SGR *}{LS1}{SGR 94}~
    ...

AFTER:
    {CUP *}{ED *}{CSI 1,43 r}
    {CUP 1,1}
    {CUP *}{ED *}{SM 34}{DECSM 25}
    {DECRM 25}{CSI 2   q}{CSI 2   q}
    {CUP *}{ED *}{DECSM 2004}{DECSM 1004}{LF}
    {SGR *}{LS1}{SGR 94}~
    ...
justinmk added a commit to justinmk/neovim that referenced this pull request Dec 11, 2017
Try another approach to defer the termcodes. Seems less janky, but still
not perfect.

ref neovim#7664
ref neovim#7649
ref neovim#7664
ref 27f9b1c
justinmk added a commit to justinmk/neovim that referenced this pull request Dec 12, 2017
With this implementation there is no "jank" during startup.

Using the main_loop in any fashion is janky. Using only the TUI loop
emits the termcodes too soon, or requires bad hacks like counting
tui_flush invocations (9 seems to work).

ref neovim#7664
ref neovim#7649
ref neovim#7664
ref 27f9b1c
justinmk added a commit to justinmk/neovim that referenced this pull request Dec 13, 2017
Try another approach to defer the termcodes. Seems less janky, but still
not perfect.

ref neovim#7664
ref neovim#7649
ref neovim#7664
ref 27f9b1c
justinmk added a commit to justinmk/neovim that referenced this pull request Dec 13, 2017
With this implementation there is no "jank" during startup.

Using the main_loop in any fashion is janky. Using only the TUI loop
emits the termcodes too soon, or requires bad hacks like counting
tui_flush invocations (9 seems to work).

ref neovim#7664
ref neovim#7649
ref neovim#7664
ref 27f9b1c
justinmk added a commit to justinmk/neovim that referenced this pull request Dec 13, 2017
Try another approach to defer the termcodes. Seems less janky, but still
not perfect.

ref neovim#7664
ref neovim#7649
ref neovim#7664
ref 27f9b1c
justinmk added a commit to justinmk/neovim that referenced this pull request Dec 13, 2017
With this implementation there is no "jank" during startup.

Using the main_loop in any fashion is janky. Using only the TUI loop
emits the termcodes too soon, or requires bad hacks like counting
tui_flush invocations (9 seems to work).

ref neovim#7664
ref neovim#7649
ref neovim#7664
ref 27f9b1c
justinmk added a commit to justinmk/neovim that referenced this pull request Dec 15, 2017
- Revert timer-based approach.
- Instead, call loop_poll_events() with a timeout in an "active" loop,
  to infer that "TUI startup activity has mostly finished", but also to
  enforce a mininum time (100 ms) before emitting "enable focus
  reporting" termcode. (If TUI startup takes longer than that minimum
  time, it's probably a slow environment anyways.)
- Tickle `main_loop` by sending a dummy event.  Without this, the
  initial "focus-gained" response from the terminal may not get
  processed until the user hits a key.

ref neovim#7220
ref neovim#7664
ref neovim#7649
ref neovim#7664
ref 27f9b1c
justinmk added a commit to justinmk/neovim that referenced this pull request Dec 16, 2017
- Revert timer-based approach.
- Instead, call loop_poll_events() with a timeout in an "active" loop,
  to infer that "TUI startup activity has mostly finished", but also to
  enforce a mininum time (100 ms) before emitting "enable focus
  reporting" termcode. (If TUI startup takes longer than that minimum
  time, it's probably a slow environment anyways.)
- Tickle `main_loop` by sending a dummy event.  Without this, the
  initial "focus-gained" response from the terminal may not get
  processed until the user hits a key.

ref neovim#7720
ref neovim#7664
ref neovim#7649
ref neovim#7664
ref 27f9b1c
justinmk added a commit to justinmk/neovim that referenced this pull request Dec 16, 2017
- Revert timer-based approach.
- Instead, call loop_poll_events() with a timeout in an "active" loop,
  to infer that "TUI startup activity has mostly finished", but also to
  enforce a mininum time (100 ms) before emitting "enable focus
  reporting" termcode. (If TUI startup takes longer than that minimum
  time, it's probably a slow environment anyways.)
- Tickle `main_loop` by sending a dummy event.  Without this, the
  initial "focus-gained" response from the terminal may not get
  processed until the user hits a key.

ref neovim#7720
ref neovim#7664
ref neovim#7649
ref neovim#7664
ref 27f9b1c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tui: tmux with focus-events, slow "scan line" effect
1 participant