Raised This Month: $ Target: $400
 0% 

set_task with B flag - replace with entity think API?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 09-28-2012 , 06:45   set_task with B flag - replace with entity think API?
Reply With Quote #1

Has anyone thought about doing this before? It sounds like a general optimization.

Create one plugin that creates an entity with think set to .1 seconds, then other plugins would use the API to add their set_tasks with "b" flag to the entity's think. Check for tasks registered in it and if it's time to call one, execute a function in the plugin that registered the task.

What do you think? If it's not a bad idea and can be at least a bit useful, I could try to come with something. Not sure how to execute a function in other plugin though and callfunc_x looks really slow.

Last edited by Backstabnoob; 09-28-2012 at 06:51.
Backstabnoob is offline
micapat
Veteran Member
Join Date: Feb 2010
Location: Nyuu, nyuu (France).
Old 09-28-2012 , 06:52   Re: set_task with B flag - replace with entity think API?
Reply With Quote #2

Something like that :

- Main Plugin -

PHP Code:
public plugin_init( )
{
    new 
entity create_entity"info_target" );
    
entity_set_classnameentityEV_SZ_classname"func_task" );
    
register_think"func_task""Task__Think" );
    
entity_set_floatentityEV_FL_nextthinkget_gametime( ) + 0.1 );
}

public 
Task__Thinkentity )
{
    
entity_set_floatentityEV_FL_nextthinkget_gametime( ) + 0.1 );

- Others -

PHP Code:
public plugin_init( )
{
    
register_think"func_task""Task__Think" );
}

public 
Task__Think( )
{
    
// ...

__________________
micapat is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 09-28-2012 , 06:55   Re: set_task with B flag - replace with entity think API?
Reply With Quote #3

It's a very bad idea. You want to replace a system coded and optimized in C with a system code in Pawn ? It will be way slower and inefficient. Also, thinking each 0.1 seconds it's what does basically the code for set_task. Using set_task with "b" flag is fine.
__________________
Arkshine is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 09-28-2012 , 06:57   Re: set_task with B flag - replace with entity think API?
Reply With Quote #4

Well yeah but it will only be done once, if you use 5 plugins with set_task and "b" flag for .1 seconds it's going to create 5 tasks, where as using that will still use only 1 think.
Backstabnoob is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 09-28-2012 , 07:18   Re: set_task with B flag - replace with entity think API?
Reply With Quote #5

It creates 5 tasks and what ? It changes nothing really.

The code for set_task works like that : AMXX hooks ServerFrame, does a check so the set_task code is executed each 0.1 second, then this code loops on all registered tasks.

5, 10, 15 tasks, you won't see any significant changes. All checks and such are done in the module, it's optimized and fast.

Your way to use a global think, could be useful only if you need to use a think for 0.1 second _only_ in several plugins, so you would save entities. That's only the advantage I can see.
__________________

Last edited by Arkshine; 09-28-2012 at 07:30.
Arkshine is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 09-28-2012 , 07:26   Re: set_task with B flag - replace with entity think API?
Reply With Quote #6

Okay, thanks for the explanation. Now that I think of it, calling the function directly from the main plugin could be actually even slower.
Backstabnoob is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 09-28-2012 , 07:30   Re: set_task with B flag - replace with entity think API?
Reply With Quote #7

I would say that, unless you need something to think < 0.1 or you need an entity to something specific, you should avoid creating entity when it's not necessary, because of all the code involving behind the creating/handling of an entity in the engine VS a simple loop with some check, which is not worth.
__________________

Last edited by Arkshine; 09-28-2012 at 07:33.
Arkshine is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 09-28-2012 , 08:55   Re: set_task with B flag - replace with entity think API?
Reply With Quote #8

I started to code module a while ago that replaces set_task with optimized version that uses single thinking entity and custom data structures instead of STL, but it's still unfinished.

From what I remember a task running with "b" flag is not that cpu expensive, but the most expensive operation is creating task and the time required to do it rises significantly with increasing amount of tasks running across all plugins.

I did some tests on machine with 2GHz athlon xp and the results were:
Creating 50k tasks with default set_task takes 159 sec. The cpu usage while tasks are running is: 38%-40%
Creating 50k tasks with my module takes less than 1 sec. The cpu usage while tasks are running is: 33%-36%
__________________
Impossible is Nothing
Sylwester is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 09-28-2012 , 10:27   Re: set_task with B flag - replace with entity think API?
Reply With Quote #9

That's awesome, are you going to continue working on it?
Backstabnoob is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 09-28-2012 , 11:15   Re: set_task with B flag - replace with entity think API?
Reply With Quote #10

Yes, I will try to finish it soon, since there is no point in anyone starting to code this when I already have working version with some details left to code. At first I tried to do it with a plugin like you suggested in your first post, but as Arkshine said that was wrong way (inter plugin communication made it very inefficient).
__________________
Impossible is Nothing
Sylwester is offline
Reply



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 08:17.


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