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:
Iron-E 2021-08-22 15:20:58 -04:00
parent dd827f0915
commit 3d99355405
No known key found for this signature in database
GPG Key ID: 19B71B7B7B021D22
1 changed files with 3 additions and 4 deletions

View File

@ -170,13 +170,12 @@ return setmetatable(highlite, {['__call'] = function(self, normal, highlights, t
local function resolve(tbl, key, resolve_links)
local value = tbl[key]
local value_type = type(value)
if value_type == 'function' then
-- lazily cache the result; next time, if it isn't a function this step will be skipped
if value_type == 'function' then -- call and cache the result; next time, if it isn't a function this step will be skipped
tbl[key] = value(setmetatable({}, {
['__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
return resolve(tbl, tbl[key], resolve_links)
elseif resolve_links and value_type == _TYPE_STRING and not string.find(value, '^#') then
return resolve(tbl, value, resolve_links)
end
return tbl[key]