How to handle provider that may run slow
i3d opened this issue · comments
I implemented a provider like this
gl.section.left[6]= {
HgInfo = {
icon = ' ',
provider = function()
local hginfo = vim.fn.system("hg ll -r . | grep 'cl' ") -- this is the slow line
if hginfo == nil or hginfo == '' then return '' end
local t = {}
for w in string.gmatch(hginfo, "%S+") do
table.insert(t, w)
end
local str = t[2]
for i = 3, #t do
if i%2 == 0 then str = str .. '::' .. t[i] end
if i >= 4 then break end
end
return str .. ' '
end,
highlight = {colors.gray, colors.bg_dim},
}
}
Once I enable this provider, my whole vim session becomes very slow, the cursor can't keep up with the keystrokes. I found that the slow line is the one with system call to the shell command, but I also found that this provider seems to get called often. I found that when I added a print line in the galaxyline.lua file like this
print(string.format("run provider %s", component_name))
return exec_provider(icon,provider)
What I observed is that in the vim session, other than the ViMode one seems keep running, this one is also running quite often, as I can see the print message alternating very quickly.
My question is if there is a way to tell galaxyline to just render this section once during this vim session and that's it? Since this provider is pulling the local hg rev out, it almost will never get changed when editing a file under it.
Ok, found a workaround. I pull the expensive shell call line out to a function and then store the result to a package level table. Inside the section, I just check the table, if there is value, use that (super fast), otherwise, call once to the command to populate the table...