AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Question regarding set_task and code optimizations (https://forums.alliedmods.net/showthread.php?t=5512)

Mugwump 09-03-2004 22:59

Question regarding set_task and code optimizations
 
I am looking to optimize some aspects of a plugin I am working on which is now up to 14.5k lines of code, uses tons of variables and many tasks which run throughout a given round in CS. Some reports coming in from some beta server admins are that their servers are using more system resources and causing a higher load average overall, this I expected. I am considering ways to optimize the code and the first thing that comes to mind involves the tasks I have running.

How are tasks handled within amxx, are they spawned off as seperate threads or are they executed within a single thread? Do tasks use a standard pool of memory or do they have their own copies for variables and such? Many tasks I have running are for a single player and I am wondering if it is worth the time coming up with new approaches that have 1 task running to handle all of the players for specific things.

Any insight the amxx devs or other knowledgable people can shed on how tasks are handled will be appreciated! :)

-Mug

Twilight Suzuka 09-03-2004 23:48

well, server_frame that would handle all the things would work better.

BAILOPAN 09-04-2004 00:23

Mugwump, to appropriately answer your question, AMX Mod X is single threaded just as the HL server is.

Tasks work like this:
Every time the server executes a frame, a timer is increased. If the timer has increased by 0.1 seconds, a list of all the current tasks is checked. If a task's interval has been reached, the task is executed.

Therefore, tasks are decently efficient.

The question about allocating variables is an even better one. Each plugin has a heap and a stack of memory which is statically allocated before runtime. This means that although your plugin may do this:
Code:
new var[4096]

..in a task that runs every second, there will be no performance loss because the heap is already allocated - it just shifts an internal pointer by 4096*4 bytes... a push/pop operation.

Plugin memory structure:
[CODE][HEAP-> <- STACK]

As you allocate new variables, you're not using more memory... you're just pointing toward memory that's already been allocated. You can verify this with the amx_Allot() and amx_Release() functions which just shift an index.

If anything, the best way to optimize your plugin is to shift the intensive work to an AMXx 0.20 module. The 0.20 module API gives an excellent and easy way to integrate your module with your plugin[s].

I hope this answers your question ;]

devicenull 09-04-2004 00:36

I found, its best to do very very little in server_frame. Do too much, you can lag the server easily, a set task to a very low amount might be better, as it wont lag the server as much

Mugwump 09-04-2004 00:43

Thanks for the responses guys and thanks Bail for the detailed explanation, I dont feel quite as guilty utilizing a ton of set_tasks now but I am curious about your suggestion to put the intensive code in a 0.20 module. Is there a plugin I can see to use as an example of this, I am not quite familiar with the new module api yet ...

Thanks! :)

-Mug

Twilight Suzuka 09-04-2004 00:46

Quote:

Originally Posted by devicenull
I found, its best to do very very little in server_frame. Do too much, you can lag the server easily, a set task to a very low amount might be better, as it wont lag the server as much

Hmm. so use server_frame only when nessasary? Sounds good.

on a totally unrelated note, will multi-threading ever be ported or created again?

BAILOPAN 09-04-2004 00:56

Quote:

Originally Posted by Mugwump
Is there a plugin I can see to use as an example of this, I am not quite familiar with the new module api yet ...

I may convert CSDM to such a style

Twilight: No, making AMxx thread safe is very difficult

PM 09-04-2004 04:18

Quote:

Originally Posted by BAILOPAN
Code:
new var[4096]

..in a task that runs every second, there will be no performance loss because the heap is already allocated - it just shifts an internal pointer by 4096*4 bytes... a push/pop operation.

Hmm not really. The compiler still generates zerofill instructions so you will get 4096 iterations which set something to 0.
I was talking about this with EKS on IRC recently.. If you want to have the maximal performance from variables (and dont need them to be initialized everytime the execution thread sees them), make them global or static (if small supports static; i forgot :o )

Anpheus 09-04-2004 13:07

It may also be more efficient to put multiple set_tasks together, lets say you have two tasks running at the same interval for a specific player. Just merge them. There's no reason to have them called seperately.


The performance gain may be minute, however.

BAILOPAN 09-04-2004 13:48

Quote:

Originally Posted by PM
Quote:

Originally Posted by BAILOPAN
Code:
new var[4096]

..in a task that runs every second, there will be no performance loss because the heap is already allocated - it just shifts an internal pointer by 4096*4 bytes... a push/pop operation.

Hmm not really. The compiler still generates zerofill instructions so you will get 4096 iterations which set something to 0.

I thought the compiler wasn't generating these fill instructions anymore? At least the web compiler version was orginally known for this. I know in my asm programs I never zerofill :p

Anpheus: That's a very good idea


All times are GMT -4. The time now is 17:23.

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