Skip to content

Commit

Permalink
perf: Optimize contains_call() by adding memoization (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlau10 authored May 24, 2024
1 parent 7897788 commit b39bbd0
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._contains_call ~= nil then
-- return cached result
return node._contains_call
end

if node.tag == "Call" or node.tag == "Invoke" then
node._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._contains_call = true
return true
end
end
end

node._contains_call = false
return false
end

Expand Down

0 comments on commit b39bbd0

Please sign in to comment.