Raised This Month: $ Target: $400
 0% 

Such approach is a performance loss or is it irrelevant? [solved]


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 10-04-2015 , 06:05   Such approach is a performance loss or is it irrelevant? [solved]
Reply With Quote #1

Hi everybody, I just develop a Golden Weapons which "enable" to buy fictitiously 3 Golden Weapons with higher damage.

And there is this 'Ham_TakeDamage' forward and 'CurWeapon' event, which I don't understand well the impact of it at server's performace:
PHP Code:
    RegisterHam(Ham_TakeDamage"player""fw_takedamage"0
PHP Code:
/**
 * Does not affect cs built-in bots. Only players and PODBots.
 */
public fw_takedamagevictiminflictorattackerFloat:damage )
{
    if( 
attacker )
    {
        new 
clip
        
new ammo
        
new currentSpecialWeapon get_user_weapon(attackerclipammo)

        if( 
g_whichSpecialWeapon[attacker] == currentSpecialWeapon )
        {
            
SetHamParamFloat(4damage g_weapon_damage )
            return 
true
        
}
    }
    return 
false


PHP Code:
    register_event("CurWeapon","curWeaponEvent","be","1=1"
PHP Code:
/**
 * This message updates the numerical magazine ammo count and the corresponding ammo 
 *    type icon on the HUD.
 */
public curWeaponEvent(id)
{
    
weapPickupEvent(id)

    return 
PLUGIN_HANDLED

PHP Code:
/**
 * When a player pickup a weapon, its change the model view to the Golden Weapon.
 */
public weapPickupEvent(id)
{
    new 
szWeapID read_data(2)

    if( 
g_whichSpecialWeapon[id] == szWeapID )
    {
        switch( 
szWeapID )
        {
            case 
CSW_AK47
            {
                
set_pev(idpev_viewmodel2AK47_V_MODEL)
                
set_pev(idpev_weaponmodel2AK47_P_MODEL)
            } 
            case 
CSW_M4A1
            {
                
set_pev(idpev_viewmodel2M4A1_V_MODEL)
                
set_pev(idpev_weaponmodel2M4A1_P_MODEL)
            } 
            case 
CSW_MP5NAVY
            {
                
set_pev(idpev_viewmodel2MP5_V_MODEL)
                
set_pev(idpev_weaponmodel2MP5_P_MODEL)
            }
        }
    }
    return 
PLUGIN_HANDLED

Is it a performance waste? How can I do better?

Such thing higher the server client's ping?

How can I measure my server 'ping' before and after active this plugin?

I was coping & rewriting from two others plugins who does the same thing, because: 1) the first one crashes my server and i could not understand why, when reading its code, which one I did not like because uses big stuff to make damage. 2) the second one is unapproved but don't uses the big stuff at the first one.

Silver Weapons (the first one)
Golden Ak (banned author)

High performance is what I am trying to archive. By now I am reading Optimizing Plugins (AMX Mod X Scripting).

Here is attached the plugin. It's fully working, but it does not have its low priority requisites, like customizable server variables.
Attached Files
File Type: sma Get Plugin or Get Source (GoldenWeapons.sma - 671 views - 8.5 KB)
File Type: zip models.zip (3.08 MB, 75 views)
__________________
Plugin: Sublime Text - ITE , Galileo
Multi-Mod: Manager / Plugin / Server

Support me on Patreon, Ko-fi, Liberapay or Open Collective

Last edited by addons_zz; 07-26-2016 at 20:05. Reason: Added weapons models.
addons_zz is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-04-2015 , 13:00   Re: Such approach is a performance loss or is it irrelevant
Reply With Quote #2

Run it and see what happens. The code that you've shown is relatively small compared to many plugins that use these hooks. However, get_user_weapon() will not always get the weapon with which the damage was caused. For example, if when someone is injured by a grenade, the attacker might not be holding a grenade.
__________________
fysiks is offline
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 10-04-2015 , 14:28   Re: Such approach is a performance loss or is it irrelevant
Reply With Quote #3

Quote:
Originally Posted by fysiks View Post
Run it and see what happens. The code that you've shown is relatively small compared to many plugins that use these hooks. However, get_user_weapon() will not always get the weapon with which the damage was caused. For example, if when someone is injured by a grenade, the attacker might not be holding a grenade.
Thank you for you time. And that get_user_weapon() is what I am worried about, but not because the grenade problem, because there will be no Golden Grenades.
  1. It is because all the time that someone get injured they will be:
  2. An "if( attacker > 0 )", and ifs to the processor are bad.
  3. A "new currentSpecialWeapon = get_user_weapon(attacker, clip, ammo)", which I don't know if it is quick as access a local constant value, or if it has a lot of cost and should not be executed every use shot.
  4. Another "if( g_whichSpecialWeapon[attacker] == currentSpecialWeapon )", which access a array position and the currentSpecialWeapon got before at 2).
  5. Finally, the "SetHamParamFloat(4, damage * g_weapon_damage )", which I don't know how much costly is it/are Ham functions, specially this one.

Hence, are these steps light weight or heavy weight?
For now, I am keep follow what you said.
  1. A will keep analyzing my server performance and ping with it and without it.
  2. I will keep studying and getting to know better these functions/ifs performance impact.

Last edited by addons_zz; 07-26-2016 at 20:00. Reason: fixed formatting.
addons_zz is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 10-04-2015 , 14:49   Re: Such approach is a performance loss or is it irrelevant
Reply With Quote #4

We can't answer, a plugin alone won't usually cause any clear difference(of course, if not using, for example, per frame forwards badly with crap code).

What you can do is to remove CurWeapon event and also weapPickupEvent, and use Ham_Item_Deploy. Even with CurWeapon, you don't need the weapPickupEvent, but Item_Deploy is better than CurWeapon, it's called only at the moment when you need to show the model again.

Ham_TakeDamage is using game default function(for players it should be CBasePlayer::TakeDamage), and it's okay to use it too. Generally, if it's ok for the game it should be ok for you too.

For example, instead of:
PHP Code:
 give_item(id,"ammo_9mm");
    
give_item(id,"ammo_9mm");
    
give_item(id,"ammo_9mm");
    
give_item(id,"ammo_9mm"); 
You could use cs_set_user_bpammo to save some native calls, even if kinda trivial. give_item firstly creates an entity then force touch between the ent and the player, cs_set_user_bpammo is a more direct way, it alters directly the m_rgAmmo offset.

Edit: About your last post, it's bullshit, don't try to micro optimize, why you are afraid of few checks ?
__________________

Last edited by HamletEagle; 10-04-2015 at 14:52.
HamletEagle is offline
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 10-04-2015 , 17:52   Re: Such approach is a performance loss or is it irrelevant
Reply With Quote #5

Quote:
Originally Posted by HamletEagle View Post
We can't answer, a plugin alone won't usually cause any clear difference(of course, if not using, for example, per frame forwards badly with crap code).

What you can do is to remove CurWeapon event and also weapPickupEvent, and use Ham_Item_Deploy. Even with CurWeapon, you don't need the weapPickupEvent, but Item_Deploy is better than CurWeapon, it's called only at the moment when you need to show the model again.

Ham_TakeDamage is using game default function(for players it should be CBasePlayer::TakeDamage), and it's okay to use it too. Generally, if it's ok for the game it should be ok for you too.

For example, instead of:
PHP Code:
 give_item(id,"ammo_9mm");
    
give_item(id,"ammo_9mm");
    
give_item(id,"ammo_9mm");
    
give_item(id,"ammo_9mm"); 
You could use cs_set_user_bpammo to save some native calls, even if kinda trivial. give_item firstly creates an entity then force touch between the ent and the player, cs_set_user_bpammo is a more direct way, it alters directly the m_rgAmmo offset.
Thank you for your time. I am going to adjust the code.

Quote:
Originally Posted by HamletEagle View Post
Edit: About your last post, it's bullshit, don't try to micro optimize, why you are afraid of few checks ?
Hey, hahhahaha. Because I want fun stuff at my server, but if this fun stuff causes lag, then I prefer don't have them at all. So, if I can make 'micro optimize' to get rid of lag and keep the good stuff, would it be a good trade, don't you think?

For example, if there is something that is executed at server side depending about the 'ping', and it could be implemented at another way where it is computed at client side, hence don't increasing the client't ping, would be a good trade if its improvement where good.

Like the Ham_TakeDamage, which is using the game default function. I am using it, instead of the original's SilverWeapon stranger techniques, which of course is more powerful because affects the cs built-in bots, but it is of course, a sacrifice that is a good trade for server's/client's performance.

Last edited by addons_zz; 10-04-2015 at 17:59.
addons_zz is offline
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 10-04-2015 , 20:41   Re: Such approach is a performance loss or is it irrelevant
Reply With Quote #6

Quote:
Originally Posted by HamletEagle View Post
Edit: About your last post, it's bullshit, don't try to micro optimize, why you are afraid of few checks ?
PS: I just what to optimize a operation that happens every-time, like when you get shot. Not for those who just happens one time here. Not others like the 'cs_set_user_bpammo', to save some native calls which rarely happens.

If it was a operation that happens all the time, every second I would became worried about that, if it is not efficiently micro optimized. Unless it uses CPU time a lot, which I have. But it it used network bandwidth a lot, which I not have, I would became worried.

Last edited by addons_zz; 10-04-2015 at 20:46.
addons_zz is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 10-05-2015 , 04:55   Re: Such approach is a performance loss or is it irrelevant?
Reply With Quote #7

You did not understand me, micro optimizations give you nothing, you don't get rid of lag with them or whatever you are thinking.

About the Damage thing, I think you are confused a bit. It's not more powerfull or whatever. Look at it's description:
Quote:
This message is sent when a player takes damage, to display the red locational indicators. The last three arguments are the origin of the damage inflicter or victim origin if inflicter isn't found. DamageType is a bitwise value which usually consists of a single bit.
Ham only hooks virtual functions, that are implemented differently in every class. So, for another entity class you must register the hook with this classname(if you register for "player", then only entities with player will be afected). I am not sure what bots are you talking about, but there are for sure ways to hook damage for them too. Also, as you can see for the Damage event, you can't modify the damage on the fly, you must create another entity that does damage, while with Ham you alter the damage directly. I would say Ham is more powerfull considering this.

Quote:
I just what to optimize a operation that happens every-time, like when you get shot. Not for those who just happens one time here. Not others like the 'cs_set_user_bpammo', to save some native calls which rarely happens.
Lol, what you understand from "every time" ? You are not working per frame, this happens once at 2/3 seconds or so, it's nothing for the server.

PHP Code:
efficiently micro optimized 
Again, micro optimizations does not give you anything.

Your title ask if it is irrelevant or not, you got your answer, but you still act like you know already. If so, why asking ?

Now, more about your code:
1.In a Ham forward you should use HAM_ return constants + this function is not mean to return booleans, but integers.
2.Do not hardcode a string array size, you have charsmax() for retrieving it. It helps on readability.
3.default: return PLUGIN_HANDLED you can remove this, but not like it matters much.
4.Something you should care about is the use of FM_CmdStart, which is called per frame, and if you have alternatives you should avoid it. I think that now you can hook WeaponIdle with ham, disable it right after hooking, enable it on deploy and disable on holster. It should work, test and see.
5.For Fakemeta forwards you should return FMRES_* constants, not PLUGIN_.
__________________

Last edited by HamletEagle; 10-05-2015 at 05:03.
HamletEagle is offline
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 10-05-2015 , 21:59   Re: Such approach is a performance loss or is it irrelevant?
Reply With Quote #8

Thank you again for your patience.

Quote:
Originally Posted by HamletEagle View Post
You did not understand me, micro optimizations give you nothing, you don't get rid of lag with them or whatever you are thinking.
Sorry. I was think about things that could let the server and the client's needing to exchange some extra packages before the actually existents ones, because of bad code implementation.

Quote:
Originally Posted by HamletEagle View Post
About the Damage thing, I think you are confused a bit. It's not more powerfull or whatever. Look at it's description:

Ham only hooks virtual functions, that are implemented differently in every class. So, for another entity class you must register the hook with this classname(if you register for "player", then only entities with player will be afected). I am not sure what bots are you talking about, but there are for sure ways to hook damage for them too. Also, as you can see for the Damage event, you can't modify the damage on the fly, you must create another entity that does damage, while with Ham you alter the damage directly. I would say Ham is more powerfull considering this.
PHP Code:
efficiently micro optimized 
Again, micro optimizations does not give you anything.

Your title ask if it is irrelevant or not, you got your answer, but you still act like you know already. If so, why asking ?
Sorry, this was my irresponsibility, instead of going here bother everybody, I should go throw the API I am using, and learn throw they documentation, and if its was not good enough, throw implementation. And only after that, came here and ask about yours experience, but even this will be done when you analyse and approve the plugin at 'New Plugin Submissions' sub-forum. I will try to behave myself better to avoid forum harassing.

Quote:
Originally Posted by HamletEagle View Post
Lol, what you understand from "every time" ? You are not working per frame, this happens once at 2/3 seconds or so, it's nothing for the server.
I was thinking about, a server at a small map, with gun-game for example, everybody is shooting every time. And a bad implementation could let to exchange some extra packages before normal actual exchange.

Quote:
Originally Posted by HamletEagle View Post
Now, more about your code:
1.In a Ham forward you should use HAM_ return constants + this function is not mean to return booleans, but integers.
2.Do not hardcode a string array size, you have charsmax() for retrieving it. It helps on readability.
3.default: return PLUGIN_HANDLED you can remove this, but not like it matters much.
4.Something you should care about is the use of FM_CmdStart, which is called per frame, and if you have alternatives you should avoid it. I think that now you can hook WeaponIdle with ham, disable it right after hooking, enable it on deploy and disable on holster. It should work, test and see.
5.For Fakemeta forwards you should return FMRES_* constants, not PLUGIN_.
Thank you for you improvements. As soon as I can I will apply them to work.

Last edited by addons_zz; 10-05-2015 at 22:00.
addons_zz is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 10-06-2015 , 05:07   Re: Such approach is a performance loss or is it irrelevant?
Reply With Quote #9

If you want to optimize your plugin, before trying micro-compiler optimization and such, you should first learn to use the proper functions/forwards for your needs. Learning what implies the use of such natives/forwards. You need more behind-in-scene knowledge about AMXX/Game/Engine. It's about understanding the flow of events to insert your code at the right place. It requires you read HLDSK, AMXX code, and such. You need to care first about the logic of your code.

For example, changing weapon model, you're using WeapPickup, which is an horrible and unreliable way. When you think about it, just because you pick up a weapon doesn't mean you will currently holding it right away, it depends of various factors (weapon weight, client cvar, CanDeploy/CanHolster virtual function, etc). This is not guarantee you will get the weapon nor that you will switch to it. Also this message happens before you actually gets the weapon. This doesn't seem a right place to set model. This is game knowledge.
You would want an event which is called when you deploy your weapon. If you look at HLSDK and how weapon is made, you see that model is set when Deploy() is called. Incidentally it happens Ham module can hooks virtual functions. This is AMXX knowledge. Then you should do the same for your plugin. You will now set model at the right time and when it's necessary.
__________________
Arkshine is offline
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 10-06-2015 , 07:07   Re: Such approach is a performance loss or is it irrelevant?
Reply With Quote #10

Thank you, hence that plugin is not finished yet. I still need understand it flow throw AMXX and HL and use the proper functions right instead of this bad codding senseless. You already give me a big start where must I go to.

Haha, You said that Pawn is a simplified language, and I agree but, to program good code in Pawn instead of senseless code, you need much more then Pawn, you need know well the scenario behind it, to not end up bad codding like me, that obliviously need to study more not about Pawn essentially but its used environment AMXX and GoldSrc. Not just because things compiles and works (apparently), does not mean that is everything alright. May be one bad codded plugin working can be ok, but can you imagine 50 bad plugins running at the same time? May be still ok, but may be not. May be neither one bad coded is ok.

Last edited by addons_zz; 10-06-2015 at 07:23.
addons_zz is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 22:16.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode