Skip to content

Commit

Permalink
perf: Optimize contains_call() by adding memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlau10 committed Jan 30, 2024
1 parent 418f489 commit 0661683
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/luacheck/stages/resolve_locals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,26 @@ local function in_scope(var, index)
end

local function contains_call(node)
if node.is_contains_call ~= nil then
-- return cached result
return node.is_contains_call
end

if node.tag == "Call" or node.tag == "Invoke" then
node.is_contains_call = true
return true
end

if node.tag ~= "Function" then
for _, sub_node in ipairs(node) do
if type(sub_node) == 'table' and contains_call(sub_node) then
node.is_contains_call = true
return true
end
end
end

node.is_contains_call = false
return false
end

Expand Down

0 comments on commit 0661683

Please sign in to comment.