Skip to content

Commit a3ac9d2

Browse files
committed
perf: reduce call api nvim_win_get_width 💪
I just realize we usually call nvim_win_get_width function on component so i call it from render and send to component by params benchmark - evil_line reduce from 0.13 to 0.125 - airline reduce from 0.23 to 0.21
1 parent 2ec95c1 commit a3ac9d2

File tree

5 files changed

+28
-34
lines changed

5 files changed

+28
-34
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ basic.lsp_diagnos = {
196196
yellow = { 'yellow', 'black' },
197197
blue = { 'blue', 'black' },
198198
},
199-
text = function(bufnr, winnr)
199+
text = function(bufnr, winnr, width)
200200
if lsp_comps.check_lsp() then
201201
return {
202202
-- `red` is define in hl_colors or a hightlight group name

lua/windline/component.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,16 @@ function Comp:setup_hl(colors)
8484
end
8585

8686

87-
function Comp:render(bufnr, winnr)
87+
function Comp:render(bufnr, winnr, width)
8888
self.bufnr = bufnr
8989
local hl_data = self.hl_data or {}
90-
local childs = self.text(self.bufnr, winnr)
90+
local childs = self.text(bufnr, winnr, width)
9191
if type(childs) == 'table'then
9292
local result = ''
9393
for _,child in pairs(childs) do
9494
local text,hl = child[1],child[2]
9595
if type(text) == 'function' then
96-
text = child[1](bufnr, winnr)
96+
text = child[1](bufnr, winnr, width)
9797
end
9898
if type(hl) == 'string' then
9999
hl = hl_data[hl] or hl

lua/windline/init.lua

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,15 @@ local mode = utils.mode
1919

2020
M.statusline_ft = {}
2121

22-
local is_width_valid = function (width, winnr)
23-
if width == nil then return true end
24-
return api.nvim_win_is_valid(winnr)
25-
and width < api.nvim_win_get_width(winnr)
26-
end
27-
2822
local render = function(bufnr, winnr, items, cache)
2923
M.state.comp = {} --reset component data
3024
M.state.mode = mode()
3125
Comp.reset()
3226
local status = ''
27+
local win_width = api.nvim_win_is_valid(winnr) and api.nvim_win_get_width(winnr)
3328
for _, comp in pairs(items) do
34-
if is_width_valid(comp.width, winnr) then
35-
status = status .. comp:render(bufnr, winnr)
29+
if win_width and (comp.width == nil or comp.width < win_width) then
30+
status = status .. comp:render(bufnr, winnr, win_width)
3631
end
3732
end
3833
if cache then
@@ -268,8 +263,8 @@ M.benchmark = function()
268263
local bench = require('plenary.profile').benchmark
269264
local statusline = ''
270265
local time = bench(num, function()
271-
vim.g.statusline_winid = vim.api.nvim_get_current_win()
272-
statusline = WindLine.show(vim.api.nvim_get_current_buf(), vim.g.statusline_winid)
266+
vim.g.statusline_winid = api.nvim_get_current_win()
267+
statusline = WindLine.show(api.nvim_get_current_buf(), vim.g.statusline_winid)
273268
end)
274269
local popup = require('plenary.popup')
275270
local result = {}
@@ -279,18 +274,19 @@ M.benchmark = function()
279274
table.insert(result, string.format('render %s time : *%s*', num, time))
280275
table.insert(result, 'Comp:')
281276
local line = M.get_statusline_ft(vim.bo.filetype) or M.default_line
282-
local bufnr = vim.api.nvim_get_current_buf()
283-
local winnr = vim.api.nvim_get_current_win()
277+
local bufnr = api.nvim_get_current_buf()
278+
local winnr = api.nvim_get_current_win()
279+
local width = api.nvim_win_get_width(0)
284280
table.insert(result, string.format('%s %12s %12s %s %s', ' ', 'time', 'name', 'num ', 'text'))
285281
for index, comp in ipairs(line.active) do
286282
local item=''
287283
time = bench(num, function()
288-
item = comp:render(bufnr, winnr)
284+
item = comp:render(bufnr, winnr, width)
289285
end)
290286
table.insert(result, string.format('%02d *%10s* %12s %s - %s', index, time, comp.name or ' ', num, item))
291287
end
292-
local width = math.floor(vim.o.columns / 1.5)
293-
local col = math.floor((vim.o.columns - width) / 2)
288+
local vim_width = math.floor(vim.o.columns / 1.5)
289+
local col = math.floor((vim.o.columns - vim_width) / 2)
294290
popup.create(result, {
295291
border = {},
296292
minheight = 30,

lua/wlsample/airline.lua

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,11 @@ airline_colors.c = {
6262
basic.divider = { b_components.divider, hl_list.Normal }
6363

6464
local width_breakpoint = 100
65-
local check_width = function()
66-
return vim.fn.winwidth(0) > width_breakpoint
67-
end
6865

6966
basic.section_a = {
7067
hl_colors = airline_colors.a,
71-
text = function()
72-
if check_width() then
68+
text = function(_,_,width)
69+
if width > width_breakpoint then
7370
return {
7471
{ ' ' .. state.mode[1] .. ' ', state.mode[2] },
7572
{ sep.right_filled, state.mode[2] .. 'Sep' },
@@ -86,9 +83,9 @@ local get_git_branch = git_comps.git_branch()
8683

8784
basic.section_b = {
8885
hl_colors = airline_colors.b,
89-
text = function()
86+
text = function(_,_, width)
9087
local branch_name = get_git_branch()
91-
if check_width() and #branch_name > 1 then
88+
if width > width_breakpoint and #branch_name > 1 then
9289
return {
9390
{ branch_name , state.mode[2] },
9491
{ ' ', '' },
@@ -128,8 +125,8 @@ basic.section_x = {
128125

129126
basic.section_y = {
130127
hl_colors = airline_colors.b,
131-
text = function()
132-
if check_width() then
128+
text = function(_,_,width)
129+
if width > width_breakpoint then
133130
return {
134131
{ sep.left_filled, state.mode[2] .. 'Sep' },
135132
{ b_components.file_type({ icon = true }), state.mode[2] },
@@ -142,8 +139,8 @@ basic.section_y = {
142139

143140
basic.section_z = {
144141
hl_colors = airline_colors.a,
145-
text = function()
146-
if check_width() then
142+
text = function(_,_,width)
143+
if width > width_breakpoint then
147144
return {
148145
{ sep.left_filled, state.mode[2] .. 'Sep' },
149146
{ '', state.mode[2] },
@@ -154,6 +151,7 @@ basic.section_z = {
154151
end
155152
return {
156153
{ sep.left_filled, state.mode[2] .. 'Sep' },
154+
{ '', state.mode[2] },
157155
{ b_components.line_col, state.mode[2] },
158156
}
159157
end,

lua/wlsample/evil_line.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ basic.file = {
6767
white = { 'white', 'black' },
6868
magenta = { 'magenta', 'black' },
6969
},
70-
text = function(_, winnr)
71-
if vim.api.nvim_win_get_width(winnr) > breakpoint_width then
70+
text = function(_, _, width)
71+
if width > breakpoint_width then
7272
return {
7373
{ b_components.cache_file_size(), 'default' },
7474
{ ' ', '' },
@@ -95,8 +95,8 @@ basic.file_right = {
9595
white = { 'white', 'black' },
9696
magenta = { 'magenta', 'black' },
9797
},
98-
text = function(_, winnr)
99-
if vim.api.nvim_win_get_width(winnr) < breakpoint_width then
98+
text = function(_, _, width)
99+
if width < breakpoint_width then
100100
return {
101101
{ b_components.line_col, 'white' },
102102
{ b_components.progress, '' },

0 commit comments

Comments
 (0)