Using variable caches

Hello the community.

I'm currently working on a passive states plugins. I have noted some minor slow-downs with it on my old computer when a battler is affected by lots of states which suppose that there is room for some improvement.
I was wondering if this type of optimization was safe (I mean, can it create conflicts with MV's base code?):

var _Game_BattlerBase_prototype_states = Game_BattlerBase.prototype.states;
Game_BattlerBase.prototype.states = function() {
if (this._statesCache !== undefined) return this._statesCache;
this._statesCache =_Game_BattlerBase_prototype_states.call(this);
this.addPassivesStates(this._statesCache);
return this._statesCache;
};

var _Game_BattlerBase_prototype_refresh = Game_BattlerBase.prototype.refresh;
Game_BattlerBase.prototype.refresh = function() {
this._statesCache = undefined;
_Game_BattlerBase_prototype_refresh.call(this);
};
The problem is that adding/removing states calls the battler's refresh function, which will set _statesCache to undefined, meaning it'll only ever contain one state at a time anyway.
Haven't tested it, but I'd guess that states would not get removed instantly when you add appropriate "state resist" traits to the battler (it would take a second refresh to update the cache).

In general, I don't think those arrays created are modified by default, so caching them should be fine. However, those cached arrays will probably get serialized into your savefiles (which inflates their size and may cause some inconsistencies when loading an outdated savestate).

I'm not sure if creating the states array over and over again has that much of an impact, especially since many calls of this function probably occur when checking a battlers traits (which again creates an even bigger array).
If adding multiple states to a battler consistently causes a performance drop, chances are there is a different root cause for this (unless you are really using a lot of states).
The problem is that adding/removing states calls the battler's refresh function, which will set _statesCache to undefined, meaning it'll only ever contain one state at a time anyway.

From my point of view, if a second state is added, then because the cache is undefined, it will be updated properly and will contain two states for future calls. I don't see in which the cache will be blocked to one state. Futher explanations will be appreciated.

Haven't tested it, but I'd guess that states would not get removed instantly when you add appropriate "state resist" traits to the battler (it would take a second refresh to update the cache).

In general, I don't think those arrays created are modified by default, so caching them should be fine. However, those cached arrays will probably get serialized into your savefiles (which inflates their size and may cause some inconsistencies when loading an outdated savestate).

I'm not sure if creating the states array over and over again has that much of an impact, especially since many calls of this function probably occur when checking a battlers traits (which again creates an even bigger array).
If adding multiple states to a battler consistently causes a performance drop, chances are there is a different root cause for this (unless you are really using a lot of states).

The cache is set to undefined before the refresh function call the stateResistSet to vanish newly resisted states. I will test it to be sure.

If a savefile is outdated, the cache is set to undefined and will be updated the first time the states function is called. Do you think to something else?

MV's dev tools suggests the my edited and non optimized Game_BattlerBase.prototype.states function is the first cause of drops. Optimize it has a true impact.
You've aliased the refresh function of Game_BattlerBase to set _statesCache to undefined. The states function returns cache if it exists, otherwise adds the new state to the cache. Adding or removing a state calls the refresh function of the battler, which sets _statesCache to undefined, so the next time the states function runs, it finds an undefined cache and adds the new state to it. Every time you add a new state, it's going to reset the cache.
You've aliased the refresh function of Game_BattlerBase to set _statesCache to undefined. The states function returns cache if it exists, otherwise adds the new state to the cache. Adding or removing a state calls the refresh function of the battler, which sets _statesCache to undefined, so the next time the states function runs, it finds an undefined cache and adds the new state to it. Every time you add a new state, it's going to reset the cache.

The Game_BattlerBase.prototype.states fix the cache to an array containing all states, not just the last one added.
The Game_BattlerBase.prototype.states fix the cache to an array containing all states, not just the last one added.

That would have been something useful to include in the initial post. 😉

I'd still question the efficacy of a states cache, as it's the kind of data that will be changing constantly throughout a battle. Caches are better for things that are more or less static and will change infrequently, like stats.
The cache is set to undefined before the refresh function call the stateResistSet to vanish newly resisted states. I will test it to be sure.
The problem I imagined would be that by calling this.stateResistSet() , your cache would already set up again before the resisted states were removed.

As for 2): No, nothing else then. 😀
As for 3): I haven't played much around with the MV (still sticking with VXAce, that implements those things in a similar way), so if you say it works I can't argue with that.

Btw: Are there any special MV dev tools you are referring to? I haven't done much with MV/Javascript yet except for occasionally looking through the plugins, so this question is completely out of personal interest. 🙂
That would have been something useful to include in the initial post. 😉

I'd still question the efficacy of a states cache, as it's the kind of data that will be changing constantly throughout a battle. Caches are better for things that are more or less static and will change infrequently, like stats.

I have updated the base post to be more clear.
You are right. I will first try to see if I can take care of the causes rather than the consequences.

The problem I imagined would be that by calling this.stateResistSet() , your cache would already set up again before the resisted states were removed.

As for 2): No, nothing else then. 😀
As for 3): I haven't played much around with the MV (still sticking with VXAce, that implements those things in a similar way), so if you say it works I can't argue with that.

Btw: Are there any special MV dev tools you are referring to? I haven't done much with MV/Javascript yet except for occasionally looking through the plugins, so this question is completely out of personal interest. 🙂

1) Well seen. I will take care of that.
Btw: By pressing F8, you get access to the console where errors are displayed, but also several others choices among which Timeline or Profiles who give the option to record the Javascript activity.



Anyway, thanks for sharing your time.

Original thread: https://forums.rpgmakerweb.com/threads/using-variable-caches.76842/