@@ -165,17 +165,44 @@ KeywordFlag.MatchAll = 0x40000000
165165-- Helper function to compare KeywordFlags
166166local band = bit .band
167167local 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.
171181function 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
179206end
180207
181208-- Active skill types, used in ActiveSkills.dat and GrantedEffects.dat
0 commit comments