Poor implementation of plugin aliasing?

I've been basing my plugin design on the default ones from kadokawa, but just realized there's a bit of an inefficiency with the way it works. This is for those plugins that have plugin commands called from the event.

They usually start off like this:

var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand; Game_Interpreter.prototype.pluginCommand = function(command, args) { _Game_Interpreter_pluginCommand.call(this, command, args); if (command === 'keyword') {This means if every plugin aliases pluginCommand, and the first command in the function is to call the aliased function, every single time a plugin command is processed, it's going to go through every single plugin that aliases this function, even if the very first one encountered is the one that needs to run.Let's say the plugin is my Smart Pathfinding plugin, and it's the last one in the user's plugin list of 100 plugins, all that allow plugin commands within the event.

var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand; Game_Interpreter.prototype.pluginCommand = function(command, args) { _Game_Interpreter_pluginCommand.call(this, command, args); if (command.toUpperCase() === 'SMARTPATH') { // do stuff here } };The dev adds a plugin command
Code:
SmartPath 0 5 3
The function in my plugin is encountered, which calls the aliased function in the previous plugin, which calls the aliased function in the previous plugin, and so on all the way back to the first of the 100 plugins. When it reaches the end and there are no more aliased functions to call, then it starts working its way back through them in the opposite direction, checking for the command that's actually being run. Finally it bubbles up through the list and ends up back at my plugin. None of the other 99 plugins did anything, and now that mine is back in control I finally do something with it.This is highly inefficient, especially if there are some plugins that have a larger number of commands that they're looking for. I should be doing my checks first to see if MY plugin wants to handle the command, and only if it doesn't, should the aliased function be called. In the example above, if mine is last in the list, it would recognize the command straight away, and there would be no calls to aliased functions at all.

I imagine the time to process is probably quite small, but we have people here who have a hundred or more scripts, and if they do the same in their MV projects with plugins, and likely use plugin commands more often as a result, this could make an impact on performance.

The comparison is like this:

The current way, every time a plugin command is called, it checks conditions in 100 different plugins.

If we only call the aliased function if the current plugin doesn't know about the command, it would average to checking conditions in 50 different plugins, if we make the assumption that each plugin is called with the same frequency.

The dev can then decide, if order is not dictated by plugin dependencies, to put the most commonly used ones towards the bottom of the list, to reduce the potential for performance issues due to the number of plugins being used.

Thoughts?

Or do all of you know this and already call the aliased function as the exception, and I'm just late to the party?
I think this this is a great post Shaz. This is one of the downsides to aliasing, although I'll be the first to agree in the case of Game_Interpreter its pretty much unavoidable if you're writing as a plugin (I only put in the 'pretty much' because I know absolutes are almost always at least theoretically wrong).

You've given a good  way to start optimizing this inefficiency. Maybe someone else will  look at it and suggest another tweak (like how to figure out script order, keeping in mind dependencies).

For the record, from what I've seen of my testing on my mobile device, I'll welcome every optimization I can get my grubby little mitts on. And now that you've written this, that includes the interpreter... (I know the compiler caches the last few choices, but beyond that?)
If you were going to go with an approach like this, you would have to be VERY careful that the aliased function IS called if your plugin isn't going to do anything with the command. Otherwise you risk breaking everyone else's plugins because you haven't passed control back correctly.


... maybe an even better way would be to not use the pluginCommand method at all, to create your own function names, then build a "hash" of plugin commands (keywords) and the function name in your plugin. Then when a pluginCommand is encountered, it can look at the first command/keyword, find the exact function that it needs to call, and call it. No aliasing at all. No looping back through all the plugins to see which one is going to handle this command.


That is quite a detour from the current way it's done though. I think it could still work, even if only some scripters adopted the idea - it would require one plugin to override the Game_Interpreter's command356 function to have a quick look through the list to see if the command is in that group - if so, call it, if not, go ahead and run the pluginCommand functions.
this why people shouldn't "abuse" of plugin command...I mean they also have some command you may not need to use a plugin command honestly...this why here Script call could be more efficient than plugincommand 

where it's permit to call directly the content and without the inconvenient of the alias.

OR you can reproduce a your own plugin command...because plugin command is simply a function who get parameters.

OR you can just...create your command in the game_interpreter because honestly that's would do the same's as the plugin command.

just the way of calling it will be smally different.

//plugin call acommand value,value//script callacommand(value,value);so...honestly yeah we should not overabuse of plugin command :/
We can't tell game developers how many plugins they should/shouldn't use. All we can do is try and make the plugins we create work as well as they can. This is a part of that.


You can't just create your own version of the plugin command, because it must, by design, allow other plugins to run as well. You can do whatever you want in your own plugins, but you mustn't stop other plugins being called by what you do.


I mean, yes, you could create your own function in Game_Interpreter and make your plugin use that. But that doesn't get around the issue I spoke about above - it doesn't stop it looking at the other 99 plugins first to see if they are the ones that need to process the command. And we can't ask users to call your Game_Interpreter functions directly - that's what the Script command does, and plugins are designed to be easier to use.


We are looking for the most efficient method for the engine to run the right plugin without having to go through every single one of them to determine which one is "the right one".
That's one of the reasons why I don't like plugin command (that, and it splits on spaces which makes a lot of things quite a headache), but I've seen people ask "why are you not using a plugin command?" in other topics so it seems like most people expect to use plugin commands over script calls.


Personally I would just advocate the use of script calls like before. However, that has its own set of performance implications, which may or may not be worse than just calling 100 aliased methods.
I also had people asking why I was not using the plugin command.

Would it be better if the Interpreter plugin command just executed a function that is stored in a "Plugin" object?

A rough example of what I mean:

Game_Interpreter.prototype.pluginCommand = function(command, args) { $gamePlugins[command](args); };Then plugin developers could just add their function to the $gamePlugins object which would be shared by allExample:

Code:
$gamePlugins.commandName = function(arguments) {  // Creating the plugin's function};
Yes, that's what I was hinting to in my second post.


This would only cause a problem then, if two scripters used the same command name. But that's going to happen with the current system anyway.
Ah, sorry - well I think that idea sounds good. Perhaps as it's early days, something like that could be implemented in the default scripts?


It could check if the function exists and if not continue to run and check for older 'aliased' code.


Then scripters would only need to specify "Make sure you have the latest version of RPG Maker MV for this to work" if they use the new way of doing things or something?
I can ask, but I can't see that it's something they will change in the default implementation of the engine, at this stage. Simply because it works as-is (if it ain't broke ...) and that there are already several hundred plugins that do it the current way.


I suspect if such a method is to be used, it would be up to use to do it, which means scripters can choose whether they want to go that way or not.


I would prefer it to remain part of Game_Interpreter, so we have access to the calling event.
This is honestly was has my hyped about using this now. Still figuring stuff out.

I'll admit questions like interpreter optimization are further down in my bucket list than things like "Why does forcing GPU rendering worsen performance in some areas while helping others?" and "What _exactly_ is causing the screen tears on this map on this device this afternoon?"
Plugin Commands are awesome, but the concern is valid. I think if you just call the aliased method when you don't run anything, you're good to go.

The custom command solution proposed would break the way I'm using the plugin commands, though:

I'm not creating command words, but sentences. Some examples:

Copy event 15 from map 7 to position 17 20This is a valid plugin command from my custom events plugin.On my smartpath plugin, I have a plugin command like this:

Code:
Move event 17 to position 15 30
And on the time system:
Code:
Run common event 2 in 5 minutes
I'm really happy with the plugin commands feature because they allow this that is much easier for the users to understand. I wish more people would use it this way.
That means that nobody else would be able to use Run, Copy or Move as their plugin commands. If they did, and a dev used both plugins, whichever one looks at the conditions first could break, if the command were intended for the other plugin.
That means that nobody else would be able to use Run, Copy or Move as their plugin commands. If they did, and a dev used both plugins, whichever one looks at the conditions first could break, if the command were intended for the other plugin.
Yes, if I were to check only the command name, that would happen.
If someone else had Run or Move or Copy as the command name and only checked that, their plugins would not work properly if the command entered was for your plugin.


imo 'run', 'move' and 'copy' are bad commands to use on their own anyway, so if anyone's plugins break because they're using just those words as the command, they deserve it 😛 I at least try to make mine a little more representative of the plugin. Like your example, but there wouldn't be a space - it would be MoveEvent, CopyEvent, RunCommonEvent.
If someone else had Run or Move or Copy as the command name and only checked that, their plugins would not work properly if the command entered was for your plugin.


imo 'run', 'move' and 'copy' are bad commands to use on their own anyway, so if anyone's plugins break because they're using just those words as the command, they deserve it 😛 I at least try to make mine a little more representative of the plugin. Like your example, but there wouldn't be a space - it would be MoveEvent, CopyEvent, RunCommonEvent.
I would probably use MoveEvent, CopyEvent, and RunCommonEvent as well...


Well, I would say CallCommonEvent or ReserveCommonEvent, but I guess my choice of naming is also pretty verbose.


Some have suggested tacking on a prefix:


Shaz.MoveEvent


TH.MoveEvent


But personally I would like to just say


MoveEvent
this is a problem involving recursion. as such, we / they / plugin writers should enact early breaks and simple optimizations like tail end recursion.

for ex.

Code:
var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;Game_Interpreter.prototype.pluginCommand = function(command, args) {	if (command.toUpperCase() === 'SMARTPATH') {		// do stuff here		return true; // done with our command, no need to check other plugin commands	}	else {		return _Game_Interpreter_pluginCommand.call(this, command, args); // tail end recursion	}};
which is pretty much what I said in the opening post? 😉
kadokawa wrap his code inside:

(function() {...})();which mean his alias is Private variable...

so it won't be like you said... since that variable name is only valid inside those function.

BUT... there's a bigger problem... the private variable cannot be accessed by other plugin. (i try it and i cannot access it)

thus the aliased method (a.k.a original method before the scripter edit it) is lost. (at least i don't know a way to access it)

it would be troublesome if somehow you NEED to use the aliased method (a.k.a original method) instead edited method.

also there's other scripter that use this method:

(function($) { $.myalias = Game_Event.prototype.initialize;})(XXX);i like this approach better than previous function wrap.

if using that... we can still access the alias by:

Code:
XXX.myalias.call(this,'parameter if exist');
if the plugin writer doesn't want you accessing the original function, then probably you shouldn't be accessing it. even if he does open up access to the alias, you can't guarantee that it's not tainted. like if 10 people modify the same function, which is the 'correct' one? but if you still want the true original version, make a separate plugin that has priority over theirs and save the alias to the untainted function.

Original thread: https://forums.rpgmakerweb.com/threads/poor-implementation-of-plugin-aliasing.50237/