[TUT] Modules and efficient scripting
This tutorial is meant for all the scripters arround amxx, recommended mostly for intermediate scripters.
First of all what is an efficient plugin? An efficient plugin is the one that combines low cpu & memory usage doing what it is suposed to do. Let me give an example: Let's try to think of a method to see wether a player was killed. This method can be done different ways but I will point out 2 of them, a good one and a bad one. The good one is when we hook the DeathMsg event PHP Code:
PHP Code:
Well the problem is that PreThink is called > 30 times per second and that means that we are checking too many times wether the player has been killed. This is a problem becouse it will cause a lot of CPU usage, a problem that small computers can't handle. By a lot of CPU usage means that we will have a lot of lag. Let's give again a better example of inefficency but a more subtile one. Let's try to hook the DeathMsg event and get all the info that we need. This is the best method of getting al the info we need is in the first example. The second one will use too many functions that in this case aren't needed... PHP Code:
PHP Code:
Well the problem is that until all the event_death forwards will be called, the data that we have will be stored in the core, and it will be accessible through read_data() function. So in this case it is unnecesarry to access the engine again to get this info since the core has already prepaired them for us. What is the most efficient module? Well this question depends on the things you would like to do in a plugin. Most people say that FakeMeta is the best module arround but it is not the most efficient! The module uses a lot of cpu becouse of the big database of function that it provides. To this question this is the answer: every module has good things to offer, we must use the special functions provided by every module. That means that when we can do a thing in engine or fun or ham it will be best to use engine/fun/ham instead of fakemeta due to lot of CPU usage. I'll give you an example. If we use fm_is_in_viewcone, I will mark the places where the plugin comunicates with the module: PHP Code:
When we use engine for example: PHP Code:
This is what happens when we use fakemeta: PLUGIN -> FAKEMETA -> METAMOD -> GAME ENGINE -> METAMOD -> FAKEMETA -> PLUGIN Multiply it by 5 times and we will see the execution time of the fm_is_in_viewcone. This is what happens when we use engine module: PLUGIN -> ENGINE -> METAMOD -> GAME ENGINE -> METAMOD -> ENGINE -> PLUGIN Only one time. It also depends on the data we want to receive or set. This is why in some situations we should use fun or engine over fakemeta. They use not as much CPU and have low memory usage in this situation. To confirm what I've said about module comunications I will post here some benchmarks that will compare fakemeta with the cstrike module and engine module natives/stocks. You will see that this makes the difference. Entity_set_origin vs. fm_entity_set_origin [fakemeta vs engine] Code:
type | name | calls | time / min / max Code:
type | name | calls | time / min / max Code:
type | name | calls | time / min / max Code:
type | name | calls | time / min / max What is the difference between the forwards of the modules? Well the main difference of the forwards is the registration method. What module provides the most efficient forwards? The difference when we talk about efficency will be almost negligible. Code:
type | name | calls | time / min / max Code:
type | name | calls | time / min / max Code:
type | name | calls | time / min / max What is the advantage that will appear once we cache them? Well the advantage is that it will use less cpu and it will make a big difference when we talk about repetitive checks (like prethink forwards), the disadvantage is that it uses memory. What does it mean to cache results? For example we want to know wether a player is a bot. Instead of using this function all the time: PHP Code:
PHP Code:
You must remember that doing this will make you plugin harder to make. Here is an example with result cache and tests, you will see that we will have a big difference, so it would be advisable to use cache in order to make a plugin efficient. Here is the main idea of what MeRcyLeZZ wants to do in order to improve his plugin. http://forums.alliedmods.net/showpos...91&postcount=1 Here are the results. http://forums.alliedmods.net/showpos...28&postcount=3 I will quote Hawk552s opition about the Fakemeta High sindrome (fakemeta general usage). Quote:
We must use the module when it has a specific good function that others don't provide. The less module comunications the better!!! And remember this tutorial does not want to say not to use fakemeta! It advises to use it only when it needs to be used! A plugin isn't bad if it uses 10 modules instead of just 2... If you propose to cache some results in a plugin, it will be more efficient but there will be a problem when we talk about easy scripting. Good tutorials that fit in with this one: http://forums.alliedmods.net/showthread.php?t=43049 http://forums.alliedmods.net/showthread.php?t=40340 http://forums.alliedmods.net/showthr...296#post809296 Benchmark tool, for the people who want to do some more tests, if you want feel free to post the results in the efficency tutorial: http://forums.alliedmods.net/showthread.php?t=67752 My comment: Hope that it is ok, if you would like to add some more please tell me. I'm not used on writing such long posts... [Hope that my english was fine ...] I thank all that spend their time reading this tutorial, suggested improvements and contributed to this tutorial! |
Re: [TUT] Modules and efficient scripting
Code:
Conclusion: you right! :| |
Re: [TUT] Modules and efficient scripting
I hated to enable more modules than just FM & Ham, but I guess I have no other choice. Still, what about CStrike's model changing? It was said that it's not fully correct so the more correct way was done in FM. Shall I use FM's or still CStrike's way?
Anyway, good tutorial. |
Re: [TUT] Modules and efficient scripting
Quote:
When we talk about the possible errors like SVC BAD then I fully suggest to use the fakemeta way! In this tutorial we must understand to use the unique functions that the module provides. As an example to what i've said, we can see that fakemeta offers good forwards like AddToFullPack, SetModel, EntityState, CmdStart and others. It would be inadequate to use for example the the cstrike fakemeta converted module(made by Exolent) over the cstrike module, because of the big cpu usage. And remember this tutorial does not want to say not to use fakemeta! It advises to use it only when it needs to be used! |
Re: [TUT] Modules and efficient scripting
thank you for the tut
GJ :) |
Re: [TUT] Modules and efficient scripting
Quote:
|
Re: [TUT] Modules and efficient scripting
Quote:
http://forums.alliedmods.net/showpos...7&postcount=40 http://forums.alliedmods.net/showpos...0&postcount=44 I didn't continue the talk there but, you should remove the tasks. And, if that causes problems put them again. |
Re: [TUT] Modules and efficient scripting
Quote:
|
Re: [TUT] Modules and efficient scripting
Quote:
Basically: It hooks spawns. That's an error. Then it uses tasks to protect problems coming from changing the models in spawn. If hooking instead the join team event you will not need tasks because you won't have 32 players changing team at the same time. I appreciate the fact that he discovered why the SVC errors happened but, hooking spawn is stupid because after blocking the "model's reset" nothing happens on spawn so you don't need to re-set the models on spawn. |
Re: [TUT] Modules and efficient scripting
Quote:
|
All times are GMT -4. The time now is 11:18. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.