[pob2-port] Memoize MatchKeywordFlags function (#9047)
* Apply changes from https://github.com/PathOfBuildingCommunity/PathOfBuilding-PoE2/pull/1507 * Fix merge issue --------- Co-authored-by: LocalIdentity <LocalIdentity@users.noreply.github.com> Co-authored-by: LocalIdentity <localidentity2@gmail.com>
This commit is contained in:
committed by
GitHub
parent
6664b2044c
commit
a43735af1e
@@ -165,17 +165,44 @@ KeywordFlag.MatchAll = 0x40000000
|
||||
-- Helper function to compare KeywordFlags
|
||||
local band = bit.band
|
||||
local MatchAllMask = bit.bnot(KeywordFlag.MatchAll)
|
||||
|
||||
-- Two-level numeric-key cache to avoid building string keys or allocating tables per call.
|
||||
local matchKeywordFlagsCache = {}
|
||||
function ClearMatchKeywordFlagsCache()
|
||||
-- cheap full reset without reallocating the outer table
|
||||
for k in pairs(matchKeywordFlagsCache) do
|
||||
matchKeywordFlagsCache[k] = nil
|
||||
end
|
||||
end
|
||||
|
||||
---@param keywordFlags number The KeywordFlags to be compared to.
|
||||
---@param modKeywordFlags number The KeywordFlags stored in the mod.
|
||||
---@return boolean Whether the KeywordFlags in the mod are satisfied.
|
||||
function MatchKeywordFlags(keywordFlags, modKeywordFlags)
|
||||
local matchAll = band(modKeywordFlags, KeywordFlag.MatchAll) ~= 0
|
||||
modKeywordFlags = band(modKeywordFlags, MatchAllMask)
|
||||
keywordFlags = band(keywordFlags, MatchAllMask)
|
||||
if matchAll then
|
||||
return band(keywordFlags, modKeywordFlags) == modKeywordFlags
|
||||
-- Cache lookup
|
||||
local row = matchKeywordFlagsCache[keywordFlags]
|
||||
if row then
|
||||
local cached = row[modKeywordFlags]
|
||||
if cached ~= nil then
|
||||
return cached
|
||||
end
|
||||
else
|
||||
row = {}
|
||||
matchKeywordFlagsCache[keywordFlags] = row
|
||||
end
|
||||
return modKeywordFlags == 0 or band(keywordFlags, modKeywordFlags) ~= 0
|
||||
-- Not in cache, compute normally
|
||||
local matchAll = band(modKeywordFlags, KeywordFlag.MatchAll) ~= 0
|
||||
local modMasked = band(modKeywordFlags, MatchAllMask)
|
||||
local keywordMasked = band(keywordFlags, MatchAllMask)
|
||||
|
||||
local matches
|
||||
if matchAll then
|
||||
matches = band(keywordMasked, modMasked) == modMasked
|
||||
else
|
||||
matches = (modMasked == 0) or (band(keywordMasked, modMasked) ~= 0)
|
||||
end
|
||||
row[modKeywordFlags] = matches -- Add to cache
|
||||
return matches
|
||||
end
|
||||
|
||||
-- Active skill types, used in ActiveSkills.dat and GrantedEffects.dat
|
||||
|
||||
@@ -352,6 +352,7 @@ end
|
||||
-- 5. Builds a list of active skills and their supports (calcs.createActiveSkill)
|
||||
-- 6. Builds modifier lists for all active skills (calcs.buildActiveSkillModList)
|
||||
function calcs.initEnv(build, mode, override, specEnv)
|
||||
ClearMatchKeywordFlagsCache()
|
||||
-- accelerator variables
|
||||
local cachedPlayerDB = specEnv and specEnv.cachedPlayerDB or nil
|
||||
local cachedEnemyDB = specEnv and specEnv.cachedEnemyDB or nil
|
||||
|
||||
Reference in New Issue
Block a user