perf(highlite): check `resolve_links` before `string.find`
`string.find` is not an expensive operation _per se_, but it doesn't need to be done at all if `reolsve_links` is false. Checking `resolve_links` is a cheaper operation than `string.find`
This commit is contained in:
parent
dd827f0915
commit
3d99355405
|
@ -170,13 +170,12 @@ return setmetatable(highlite, {['__call'] = function(self, normal, highlights, t
|
||||||
local function resolve(tbl, key, resolve_links)
|
local function resolve(tbl, key, resolve_links)
|
||||||
local value = tbl[key]
|
local value = tbl[key]
|
||||||
local value_type = type(value)
|
local value_type = type(value)
|
||||||
if value_type == 'function' then
|
if value_type == 'function' then -- call and cache the result; next time, if it isn't a function this step will be skipped
|
||||||
-- lazily cache the result; next time, if it isn't a function this step will be skipped
|
|
||||||
tbl[key] = value(setmetatable({}, {
|
tbl[key] = value(setmetatable({}, {
|
||||||
['__index'] = function(_, inner_key) return resolve(tbl, inner_key, true) end
|
['__index'] = function(_, inner_key) return resolve(tbl, inner_key, true) end
|
||||||
}))
|
}))
|
||||||
elseif value_type == _TYPE_STRING and not string.find(value, '^#') and resolve_links then
|
elseif resolve_links and value_type == _TYPE_STRING and not string.find(value, '^#') then
|
||||||
return resolve(tbl, tbl[key], resolve_links)
|
return resolve(tbl, value, resolve_links)
|
||||||
end
|
end
|
||||||
|
|
||||||
return tbl[key]
|
return tbl[key]
|
||||||
|
|
Loading…
Reference in New Issue