Skip to content

Commit a43735a

Browse files
github-actions[bot]LocalIdentityLocalIdentity
authored
[pob2-port] Memoize MatchKeywordFlags function (#9047)
* Apply changes from PathOfBuildingCommunity/PathOfBuilding-PoE2#1507 * Fix merge issue --------- Co-authored-by: LocalIdentity <LocalIdentity@users.noreply.github.com> Co-authored-by: LocalIdentity <localidentity2@gmail.com>
1 parent 6664b20 commit a43735a

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/Data/Global.lua

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,44 @@ KeywordFlag.MatchAll = 0x40000000
165165
-- Helper function to compare KeywordFlags
166166
local band = bit.band
167167
local MatchAllMask = bit.bnot(KeywordFlag.MatchAll)
168+
169+
-- Two-level numeric-key cache to avoid building string keys or allocating tables per call.
170+
local matchKeywordFlagsCache = {}
171+
function ClearMatchKeywordFlagsCache()
172+
-- cheap full reset without reallocating the outer table
173+
for k in pairs(matchKeywordFlagsCache) do
174+
matchKeywordFlagsCache[k] = nil
175+
end
176+
end
177+
168178
---@param keywordFlags number The KeywordFlags to be compared to.
169179
---@param modKeywordFlags number The KeywordFlags stored in the mod.
170180
---@return boolean Whether the KeywordFlags in the mod are satisfied.
171181
function MatchKeywordFlags(keywordFlags, modKeywordFlags)
182+
-- Cache lookup
183+
local row = matchKeywordFlagsCache[keywordFlags]
184+
if row then
185+
local cached = row[modKeywordFlags]
186+
if cached ~= nil then
187+
return cached
188+
end
189+
else
190+
row = {}
191+
matchKeywordFlagsCache[keywordFlags] = row
192+
end
193+
-- Not in cache, compute normally
172194
local matchAll = band(modKeywordFlags, KeywordFlag.MatchAll) ~= 0
173-
modKeywordFlags = band(modKeywordFlags, MatchAllMask)
174-
keywordFlags = band(keywordFlags, MatchAllMask)
195+
local modMasked = band(modKeywordFlags, MatchAllMask)
196+
local keywordMasked = band(keywordFlags, MatchAllMask)
197+
198+
local matches
175199
if matchAll then
176-
return band(keywordFlags, modKeywordFlags) == modKeywordFlags
200+
matches = band(keywordMasked, modMasked) == modMasked
201+
else
202+
matches = (modMasked == 0) or (band(keywordMasked, modMasked) ~= 0)
177203
end
178-
return modKeywordFlags == 0 or band(keywordFlags, modKeywordFlags) ~= 0
204+
row[modKeywordFlags] = matches -- Add to cache
205+
return matches
179206
end
180207

181208
-- Active skill types, used in ActiveSkills.dat and GrantedEffects.dat

src/Modules/CalcSetup.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ end
352352
-- 5. Builds a list of active skills and their supports (calcs.createActiveSkill)
353353
-- 6. Builds modifier lists for all active skills (calcs.buildActiveSkillModList)
354354
function calcs.initEnv(build, mode, override, specEnv)
355+
ClearMatchKeywordFlagsCache()
355356
-- accelerator variables
356357
local cachedPlayerDB = specEnv and specEnv.cachedPlayerDB or nil
357358
local cachedEnemyDB = specEnv and specEnv.cachedEnemyDB or nil

0 commit comments

Comments
 (0)