mirror of
https://github.com/smogon/pokemon-showdown.git
synced 2026-06-09 18:01:19 -05:00
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:
parent
a13da24f29
commit
31c6a32d23
22
sim/dex.js
22
sim/dex.js
|
|
@ -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));
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user