Optimize Dex#getEffect by caching the result.

As outlined by pkmn.cc/optimize, this change should result in a ~30%
increase in Battle performance over repeated runs. Caching the
resultant effect is safe provided nothing mutates it.
This commit is contained in:
Kirk Scheibelhut 2019-02-19 17:05:41 -08:00
parent a13da24f29
commit 31c6a32d23

View File

@ -130,6 +130,8 @@ class ModdedDex {
this.abilityCache = new Map();
/** @type {Map<string, TypeInfo>} */
this.typeCache = new Map();
/** @type {Map<string, PureEffect>} */
this.effectCache = new Map();
if (!isOriginal) {
const original = dexes['base'].mod(mod).includeData();
@ -458,10 +460,8 @@ class ModdedDex {
moveCopy.hit = 0;
return moveCopy;
}
/**
* While this function can technically return any kind of effect at
* all, that's not a feature TypeScript needs to know about.
*
* @param {?string | Effect} [name]
* @return {PureEffect}
*/
@ -473,6 +473,22 @@ class ModdedDex {
// @ts-ignore
return name;
}
let cached = this.effectCache.get(name);
if (!cached) {
cached = this.getEffectInner(name);
this.effectCache.set(name, cached);
}
return cached;
}
/**
* While this function can technically return any kind of effect at
* all, that's not a feature TypeScript needs to know about.
* @param {string} name
* @return {PureEffect}
*/
getEffectInner(name) {
if (name.startsWith('move:')) {
// @ts-ignore
return this.getMove(name.slice(5));