From 999bb983f0f44b16126b79ba105f079ac1df21ff Mon Sep 17 00:00:00 2001 From: luukvbaal <31730729+luukvbaal@users.noreply.github.com> Date: Fri, 20 Jan 2023 00:47:02 +0100 Subject: [PATCH] perf(statuscolumn): only fill click defs array once per redraw (#21884) Problem: 'statuscolumn' click definitions are cleared, evaluated, allocated and filled each redraw for every row in a window. This despite the fact that we only store a single click definition array for the entire column as opposed to one for each row. Solution: Only fill the 'statuscolumn' click definition array once per window per redraw. Resolve https://github.com/neovim/neovim/issues/21767. --- runtime/doc/options.txt | 5 +++++ src/nvim/statusline.c | 21 +++++++++++++-------- test/functional/ui/statuscolumn_spec.lua | 2 +- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt index 169ec95b03..0c078b7bba 100644 --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -6027,6 +6027,11 @@ A jump table for the options with a short description can be found at |Q_op|. when drawing the actual buffer line, and positive when drawing the wrapped part of a buffer line. + NOTE: The %@ click execute function item is supported as well but the + specified function will be the same for each row in the same column. + It cannot be switched out through a dynamic 'statuscolumn' format, the + handler should be written with this in mind. + Examples: >vim " Relative number with bar separator and click handlers: :set statuscolumn=%@SignCb@%s%=%T%@NumCb@%r│%T diff --git a/src/nvim/statusline.c b/src/nvim/statusline.c index 29ce8dab71..6ad1f31143 100644 --- a/src/nvim/statusline.c +++ b/src/nvim/statusline.c @@ -882,8 +882,8 @@ void draw_tabline(void) redraw_tabline = false; } -/// Build the 'statuscolumn' string for line "lnum". If "setnum" is true, -/// update the "lnum" and "relnum" vim-variables for a new line. +/// Build the 'statuscolumn' string for line "lnum". When "relnum" == -1, +/// the v:lnum and v:relnum variables don't have to be updated. /// /// @param hlrec HL attributes (can be NULL) /// @param stcp Status column attributes (can be NULL) @@ -891,6 +891,8 @@ void draw_tabline(void) int build_statuscol_str(win_T *wp, linenr_T lnum, long relnum, int maxwidth, int fillchar, char *buf, stl_hlrec_t **hlrec, statuscol_T *stcp) { + bool fillclick = relnum >= 0 && lnum == wp->w_topline; + if (relnum >= 0) { set_vim_var_nr(VV_LNUM, lnum); set_vim_var_nr(VV_RELNUM, relnum); @@ -898,14 +900,17 @@ int build_statuscol_str(win_T *wp, linenr_T lnum, long relnum, int maxwidth, int StlClickRecord *clickrec; char *stc = xstrdup(wp->w_p_stc); - int width = build_stl_str_hl(wp, buf, MAXPATHL, stc, "statuscolumn", OPT_LOCAL, - fillchar, maxwidth, hlrec, &clickrec, stcp); + int width = build_stl_str_hl(wp, buf, MAXPATHL, stc, "statuscolumn", OPT_LOCAL, fillchar, + maxwidth, hlrec, fillclick ? &clickrec : NULL, stcp); xfree(stc); - stl_clear_click_defs(wp->w_statuscol_click_defs, wp->w_statuscol_click_defs_size); - wp->w_statuscol_click_defs = stl_alloc_click_defs(wp->w_statuscol_click_defs, width, - &wp->w_statuscol_click_defs_size); - stl_fill_click_defs(wp->w_statuscol_click_defs, clickrec, buf, width, false); + // Only update click definitions once per window per redraw + if (fillclick) { + stl_clear_click_defs(wp->w_statuscol_click_defs, wp->w_statuscol_click_defs_size); + wp->w_statuscol_click_defs = stl_alloc_click_defs(wp->w_statuscol_click_defs, width, + &wp->w_statuscol_click_defs_size); + stl_fill_click_defs(wp->w_statuscol_click_defs, clickrec, buf, width, false); + } return width; } diff --git a/test/functional/ui/statuscolumn_spec.lua b/test/functional/ui/statuscolumn_spec.lua index 3da630ddcc..ae3b95fb0f 100644 --- a/test/functional/ui/statuscolumn_spec.lua +++ b/test/functional/ui/statuscolumn_spec.lua @@ -61,7 +61,7 @@ describe('statuscolumn', function() ]]) end) - it("works with 'statuscolumn'", function() + it("works with 'number' and 'relativenumber'", function() command([[set stc=%{&nu?v:lnum:''}%=%{&rnu?'\ '.v:relnum:''}│]]) screen:expect([[ 4 │aaaaa |