From 3d993554053de2c6e33c0f648bc6649c031a9995 Mon Sep 17 00:00:00 2001 From: Iron-E Date: Sun, 22 Aug 2021 15:20:58 -0400 Subject: [PATCH] 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` --- lua/highlite.lua | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lua/highlite.lua b/lua/highlite.lua index 4d0f007..bd92cf4 100644 --- a/lua/highlite.lua +++ b/lua/highlite.lua @@ -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]