View Single Post
Author Message
MeRcyLeZZ
Veteran Member
Join Date: Dec 2007
Old 03-18-2009 , 21:01   Optimizing Plugins: Cache Results on a Larger Scale?
Reply With Quote #1

Surely many of you have read the article on the Wiki about how saving results helps optimize your plugins. Now I'm wanting to know what you guys think about applying such method on a larger scale (sort of).

Let's say I have the following code:
Code:
public fw_PlayerPreThink( id ) {     if ( get_pcvar_num( cvar_affectBots ) && is_user_bot( id ) && is_user_alive( id ) )     {         // Do stuff...     } }
So if the forward gets called 100 times a second, that means the plugin will be checking the CVar value, whether the player is alive, and whether it's a bot 100 times per second per player (this assuming they all pass the checks). However there is no need for that, since stuff like that doesn't change that often.

So the idea would be to:

1. Cache the CVar on new round events (since there is no forward for detecting the exact moment it's changed). Then we can just add to the plugin's documentation: "CVar changes will take effect after a new round". Usually, end-users won't be upset by this restriction IMO.

2. Cache whether the user is a bot on client connect forwards (since the "bot" property will only change whenever a new player joins the server).

3. Cache whether the player is alive on client spawn, death, and disconnect events (since... well you get the point).

So this is what the final code would look like:
Code:
#include <amxmodx> #include <cstrike> #include <fakemeta> #include <hamsandwich> new cvar_affectBots new g_cachedAffectBots, g_isBot[33], g_isAlive[33] public plugin_init() {     register_plugin( "Cache Results", "0.0", "Example" )         register_event("HLTV", "event_round_start", "a", "1=0", "2=0")         RegisterHam( Ham_Spawn, "player", "fw_PlayerSpawn", 1 )     RegisterHam( Ham_Killed, "player", "fw_PlayerKilled", 1 )         register_forward( FM_PlayerPreThink, "fw_PlayerPreThink" )         cvar_affectBots = register_cvar( "amx_affect_bots", "1" ) } public plugin_cfg() {     // Cache CVARs after configs are loaded     set_task(0.5, "event_round_start") } public event_round_start() {     g_cachedAffectBots = get_pcvar_num( cvar_affectBots ) } public client_putinserver( id ) {     if ( is_user_bot( id ) )         g_isBot[id] = true } public client_disconnect( id ) {     g_isAlive[id] = false     g_isBot[id] = false } public fw_PlayerSpawn( id ) {     // Filter out false Ham_Spawn triggers (please ignore the fact I didn't     // add CZ bots support for this, that's beyond the scope of this thread)     if ( !is_user_alive( id ) || !cs_get_user_team( id ) )         return;         g_isAlive[id] = true } public fw_PlayerKilled( id /*, attacker, shouldgib*/ ) {     g_isAlive[id] = false } public fw_PlayerPreThink( id ) {     if ( g_cachedAffectBots && g_isBot[id] && g_isAlive[id] )     {         // Do stuff...     } }
Yeah, it looks more complex, I know. But it should actually be more efficient. And I've seen some plugins already doing this.

So what do you think? Is this really an optimization? If so, is it worth it the work? And when?
__________________

Last edited by MeRcyLeZZ; 03-21-2009 at 17:31. Reason: fixed link
MeRcyLeZZ is offline