Skip to content

Commit

Permalink
Implement CheckScript caching to (maybe?) accelerate processing of lo…
Browse files Browse the repository at this point in the history
…ng priority lists.
  • Loading branch information
Hekili committed Apr 21, 2020
1 parent 1bf7e85 commit bc04738
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 30 deletions.
87 changes: 57 additions & 30 deletions Scripts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ local safeMax = ns.safeMax

local trim = string.trim

local twipe = table.wipe


-- Forgive the name, but this should properly replace ! characters with not, accounting for appropriate bracketing.
-- Why so complex? Because "! 0 > 1" converted to Lua is "not 0 > 1" which evaluates to "false > 1" -- not the goal.
Expand Down Expand Up @@ -1126,52 +1128,77 @@ end
scripts.ConvertScript = ConvertScript


function scripts:CheckScript( scriptID, action, elem )
local prev_action = state.this_action
if action then state.this_action = action end
do
local cacheTime = 0
local cache = {}

local script = self.DB[ scriptID ]
function scripts:CheckScript( scriptID, action, elem )
local moment = state.now + state.offset

if not script then
state.this_action = prev_action
return false
end
if moment ~= cacheTime then
if Hekili.ActiveDebug then Hekili:Debug( "Resetting CheckScript cache as time changed from %.2f to %.2f ( %.2f ).", cacheTime, moment, moment - cacheTime ) end
cacheTime = moment
twipe( cache )
end

if not elem then
if script.Error then
state.this_action = prev_action
return false, script.Error
local uniqueID = scriptID .. "-" .. state.query_time
if cache[ uniqueID ] ~= nil then return cache[ uniqueID ] end

local prev_action = state.this_action
if action then state.this_action = action end

elseif not script.Conditions then
local script = self.DB[ scriptID ]

if not script then
state.this_action = prev_action
return true
cache[ uniqueID ] = false
return false
end

else
local success, value = pcall( script.Conditions )
if not elem then
if script.Error then
state.this_action = prev_action
cache[ uniqueID ] = false
return false, script.Error

if success then
elseif not script.Conditions then
state.this_action = prev_action
return value
end
end
cache[ uniqueID ] = true
return true

else
if not script.Modifiers[ elem ] then
state.this_action = prev_action
return nil, elem .. " not set."
else
local success, value = pcall( script.Conditions )

else
local success, value = pcall( script.Modifiers[ elem ] )
if success then
state.this_action = prev_action
cache[ uniqueID ] = value
return value
end
end

if success then
else
if not script.Modifiers[ elem ] then
state.this_action = prev_action
return value
return nil, elem .. " not set."

else
local success, value = pcall( script.Modifiers[ elem ] )

if success then
state.this_action = prev_action
return value
end
end
end

state.this_action = prev_action
cache[ uniqueID ] = false
return false
end

state.this_action = prev_action
return false
function scripts:ResetCache()
twipe( cache )
end
end


Expand Down
2 changes: 2 additions & 0 deletions State.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4730,6 +4730,8 @@ do

end

scripts:ResetCache()

state.this_action = curr_action
state:RemoveEvent( e )
end
Expand Down

0 comments on commit bc04738

Please sign in to comment.