Skip to content

Commit

Permalink
perf: various performance improvements
Browse files Browse the repository at this point in the history
- use locals
- use string.format
  • Loading branch information
rebelot committed Sep 3, 2022
1 parent aa21485 commit 5b72a6a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 63 deletions.
36 changes: 22 additions & 14 deletions lua/heirline/highlights.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
local M = {}
local str_format = string.format
local tbl_insert = table.insert
local tbl_concat = table.concat

local defined_highlights = {}

Expand Down Expand Up @@ -32,34 +35,39 @@ end

local function get_hl_style(hl)
local style = {}
local valid_styles = { "bold", "standout", "underline", "undercurl", "underdouble", "underdotted", "underdashed", "strikethrough", "italic", "reverse" }
local valid_styles = {
"bold",
"standout",
"underline",
"undercurl",
"underdouble",
"underdotted",
"underdashed",
"strikethrough",
"italic",
"reverse",
}
for _, v in ipairs(valid_styles) do
if hl[v] then
table.insert(style, v)
tbl_insert(style, v)
end
end
return table.concat(style, "")

return tbl_concat(style, "")
end

local function name_rgb_hl(hl)
return "Stl"
.. (hl.fg and hl.fg:gsub("#", "") or "")
.. "_"
.. (hl.bg and hl.bg:gsub("#", "") or "")
.. "_"
.. get_hl_style(hl)
.. "_"
.. (hl.sp and hl.sp:gsub("#", "") or "")
local hl_name, _ =
str_format("Stl%s_%s_%s_%s", hl.fg or "", hl.bg or "", get_hl_style(hl), hl.sp or ""):gsub("#", "")
return hl_name
end

local function name_cterm_hl(hl)
return "Stl" .. (hl.ctermfg or "") .. "_" .. (hl.ctermbg or "") .. "_" .. get_hl_style(hl.cterm or hl)
return str_format("Stl%s_%s_%s", (hl.ctermfg or ""), (hl.ctermbg or ""), get_hl_style(hl.cterm or hl))
end

local function hex(val)
if type(val) == "number" then
return string.format("#%06x", val)
return str_format("#%06x", val)
else
return val
end
Expand Down
83 changes: 49 additions & 34 deletions lua/heirline/statusline.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
local utils = require("heirline.utils")
local hi = require("heirline.highlights")
local eval_hl = hi.eval_hl
local tbl_insert = table.insert
local tbl_concat = table.concat
local str_format = string.format

local default_restrict = {
init = true,
Expand Down Expand Up @@ -223,7 +227,7 @@ local function register_update_autocmd(component)
else
events = {}
for i, e in ipairs(component.update) do
table.insert(events, e)
tbl_insert(events, e)
end
callback = component.update.callback
pattern = component.update.pattern
Expand Down Expand Up @@ -255,13 +259,16 @@ function StatusLine:_eval()
self:clear_tree()
end

local tree = self._tree

if self.condition and not self:condition() then
return false
end

if self.update then
if type(self.update) == "function" then
if self:update() then
local update = self.update
if update then
if type(update) == "function" then
if update(self) then
self._win_cahe = nil
end
else
Expand All @@ -272,7 +279,7 @@ function StatusLine:_eval()

local win_cache = self:get_win_attr("_win_cache")
if win_cache then
self._tree[1] = win_cache
tree[1] = win_cache
return true
end
end
Expand All @@ -281,7 +288,8 @@ function StatusLine:_eval()
self:init()
end

local hl = type(self.hl) == "function" and (self:hl() or {}) or self.hl -- self raw hl
local hl = self.hl
hl = type(hl) == "function" and (hl(self) or {}) or hl -- self raw hl

if type(hl) == "string" then
hl = utils.get_highlight(hl)
Expand All @@ -295,71 +303,74 @@ function StatusLine:_eval()
self.merged_hl = vim.tbl_extend("force", parent_hl, hl)
end

if self.on_click then
local on_click = self.on_click
if on_click then
local func_name = register_global_function(self)
local minwid = type(self.on_click.minwid) == "function" and self.on_click.minwid(self)
or self.on_click.minwid
or ""
table.insert(self._tree, "%" .. minwid .. "@" .. func_name .. "@")
local minwid = on_click.minwid or ""
minwid = type(minwid) == "function" and minwid(self) or minwid
tbl_insert(tree, str_format("%%%s@%s@", minwid, func_name))
end

if self.provider then
local provider_str = type(self.provider) == "function" and (self:provider() or "") or (self.provider or "")
local hl_str_start, hl_str_end = hi.eval_hl(self.merged_hl)
table.insert(self._tree, hl_str_start .. provider_str .. hl_str_end)
local provider = self.provider
if provider then
local provider_str = type(provider) == "function" and (provider(self) or "") or (provider or "")
local hl_str_start, hl_str_end = eval_hl(self.merged_hl)
tbl_insert(tree, hl_str_start .. provider_str .. hl_str_end)
end

local children_i
if self.pick_child then
children_i = self.pick_child
else
children_i = {}
for i, _ in ipairs(self) do
table.insert(children_i, i)
local pick_child = self.pick_child
local picked_children
if pick_child then
picked_children = {}
for _, i in ipairs(pick_child) do
tbl_insert(picked_children, self[i])
end
end

for _, i in ipairs(children_i) do
local child = self[i]
for _, child in ipairs(picked_children or self) do
child._tree = {}
table.insert(self._tree, child._tree)
tbl_insert(tree, child._tree)
local ret = child:_eval()
if not ret then
table.remove(tree)
end
if ret and not self.fallthrough then
break
end
end

if self.on_click then
table.insert(self._tree, "%X")
if on_click then
tbl_insert(tree, "%X")
end

if self.after then
self:after()
end

if self.update then
self:set_win_attr("_win_cache", self._tree)
table.insert(self._updatable_components, self)
if update then
self:set_win_attr("_win_cache", tree)
tbl_insert(self._updatable_components, self)
end
return true
end

function StatusLine:traverse(tree, stl)
stl = stl or {}
tree = tree or self._tree
local traverse = self.traverse

if not tree then
return ""
end

for _, node in ipairs(tree) do
if type(node) ~= "table" then
table.insert(stl, node)
tbl_insert(stl, node)
else
self:traverse(node, stl)
traverse(self, node, stl)
end
end
return table.concat(stl, "")
return tbl_concat(stl, "")
end

function StatusLine:clear_tree()
Expand All @@ -368,7 +379,7 @@ function StatusLine:clear_tree()
return
end
for i, _ in ipairs(tree) do
self._tree[i] = nil
tree[i] = nil
end
end

Expand All @@ -388,4 +399,8 @@ function StatusLine:eval()
return self:traverse()
end

function StatusLine:is_empty()
return self:traverse() == ""
end

return StatusLine
34 changes: 19 additions & 15 deletions lua/heirline/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,17 @@ function M.make_tablist(tab_component)
local tabpages = vim.api.nvim_list_tabpages()
for i, tabpage in ipairs(tabpages) do
local tabnr = vim.api.nvim_tabpage_get_number(tabpage)
if not (self[i] and tabnr == self[i].tabnr) then
local child = self[i]
if not (child and child.tabnr == tabnr) then
self[i] = self:new(tab_component, i)
self[i].tabnr = tabnr
child = self[i]
child.tabnr = tabnr
end
if tabpage == vim.api.nvim_get_current_tabpage() then
self[i].is_active = true
child.is_active = true
self.active_child = i
else
self[i].is_active = false
child.is_active = false
end
end
if #self > #tabpages then
Expand Down Expand Up @@ -408,22 +410,24 @@ function M.make_buflist(buffer_component, left_trunc, right_trunc, buf_func)
local bufs = buf_func()
local visible_buffers = bufs_in_tab()
for i, bufnr in ipairs(bufs) do
if not (self[i] and bufnr == self[i].bufnr) then
local child = self[i]
if not (child and child.bufnr == bufnr) then
self[i] = self:new(buffer_component, i)
self[i].bufnr = bufnr
child = self[i]
child.bufnr = bufnr
end

if bufnr == tonumber(vim.g.actual_curbuf) then
self[i].is_active = true
child.is_active = true
self.active_child = i
else
self[i].is_active = false
child.is_active = false
end

if visible_buffers[bufnr] then
self[i].is_visible = true
child.is_visible = true
else
self[i].is_visible = false
child.is_visible = false
end
end
if #self > #bufs then
Expand All @@ -443,7 +447,7 @@ function M.page_buflist(buflist, maxwidth)
return
end

local tbl = {}
local bfl = {}
maxwidth = maxwidth - 2 -- leave some space for {right,left}_trunc

local pages = {{}}
Expand Down Expand Up @@ -489,20 +493,20 @@ function M.page_buflist(buflist, maxwidth)
end

if page_index > 1 then
table.insert(tbl, buflist.left_trunc:eval())
table.insert(bfl, buflist.left_trunc:eval())
end

for _, child in ipairs(page) do
table.insert(tbl, child:traverse())
table.insert(bfl, child:traverse())
end

-- table.insert(tbl, "%=")

if page_index < #pages then
table.insert(tbl, buflist.right_trunc:eval())
table.insert(bfl, buflist.right_trunc:eval())
end
buflist:clear_tree()
buflist._tree[1] = table.concat(tbl, "")
buflist._tree[1] = table.concat(bfl, "")
end

---ColorScheme callback useful to reset highlights
Expand Down

0 comments on commit 5b72a6a

Please sign in to comment.