Page 1 of 1

What takes more resourses?

Posted: Thu Aug 20, 2009 6:07 pm
by Damien White
Can anyone tell me please...

Let's say you have 100 lines of code, and it runs each time an item is used.

What takes more system resourses? 10 lines of code script in 10 linked (hooked) scripts...
ItemUse.ecl is 10 lines which then calls 9 SubUse.ecl scripts 10 lines each
or
1 ItemUse.ecl script that is 100 lines long?

My thinking is this...If the main script calls 9 subscripts to perform 9 different functions, and upon completing 3 functions, no longer needs say 3 subscripts. Will you then be left with perhaps 70 lines of active code?

Would be nice to hear the logic behind it.

Let me see if I can think of an example:
Let's hypotheticaly say you have a magic staff.
When you double click the staff the following occurs...
1. The main script runs, checks for other magic properties and runs hooked scripts as a result (10 lines of code)
2. A light script is launched (10 lines of code)
3. Change colour of staff launched (10 lines of code)
4. Staff is equiped to hand (10 lines of code)
5. Regenerates user 2 hps every 15 seconds (10 lines of code)
6. Regen stamina 2 every 15 seconds (10 lines of code)
7. Lets user hear father (10 lines of code)
8. Makes user invisible (10 lines of code)
9. Plays bird sound effects (10 lines of code)
10. Gives player the hiccups (10 lines of code)

Yes I was having a hard time thing up suitable codes :oP

Okay so there we have 100 lines of code.
Now lets say a few seconds after the item use and initial launch of the 10 scripts, #3, #4 and #8 finish their tasks and the programs end.

Are we left with just 70 lines of active code? Making this a far more effective way to code?

Thanks in advance for the feedback!

Re: What takes more resourses?

Posted: Fri Aug 21, 2009 4:06 am
by Yukiko
I'd think that if you're using "Start_Script" to launch all those SubUse.ecl scripts that you'd have a lot of data being pushed and pulled from the POL VM stack so I'm thinking that might use more resources, both in memory and in processor cycles, than a straight 100 line script.

I'm not sure if my terminology is accurate or my understanding of how POL works as I've never written anything remotely close to what it does. However I do know that whenever you jump from one "program" to another with the intent of possibly returning to the original "program" you need to save a lot of information to make that possible and that takes resources.

Re: What takes more resourses?

Posted: Fri Aug 21, 2009 4:42 am
by Turley
This is a very good question :)
Pol cashes every (different) script with the instructions. So starting 2 times the same script doesnt take more ressources (if you only consider this).
But it creates for every instance a valuestack that holds the variables (+ControlStack for functions). So starting 2 times the same script takes (at the same state Value/Control-Stack size differs depending where the script is)
~ecl size + 2*valuestack+2*ControlStack
Btw script.var_size does not show the size of the complete valuestack only the current element (in other words current function)

I would say its always better to have one bigger script then dozen of small scripts.
But thats my opinion :)

p.s.: is it not 100% correct

Re: What takes more resourses?

Posted: Fri Aug 21, 2009 8:34 am
by Damien White
Thanks for all the input.

I think perhaps I have expressed myself wrong.

What I am perposing is that let's say when an item is used, the MainScript.ecl is launched. This will run in the background until all SubScript.ecl are complete. Then it will no longer need to run and shut down. The same way the scripts of an NPC run until the NPC is killed.

So we have 10 lines of code run as the MainScript.ecl. This code then detects, perhaps through reading CProps (yes I like CProps :P) off an item, what SubScript.ecls need to run. The MainScript.ecl then exicutes a SubScript.ecl for the 9 Cprops in finds. As each SubScript.ecl is completed it should then discontinue and cease to exist. Eventualy getting to the point where the MainScript.ecl is all that is left running. When it then finds that all 9 SubScript.ecl are complete and no longer running it then discontinues itself.

It should look something like this:
Initial use : MainScript.ecl is launched (10 lines of code) -> Cprops read -> 9 SubScripts.ecl exicuted (90 lines of code) = 100 lines of code
A few seconds later : MainScript.ecl is still running (This is incase new Cprops are added and need to be controlled) (10 lines of code) -> 3 SubScripts.ecl complete thier tasks and discontinue (6 x 10 lines of code left running) = 70 lines of code currently running
All SubScript.ecl complete taskes : MainScript.ecl is still running (This is incase new Cprops are added and need to be controlled) (10 lines of code) -> No more SubScript.ecl = 10 lines of code (Only MainScript.ecl running)
MainScript.ecl Closes : All SubScript.ecl discontinues, no Cprops detected MainScript.ecl discontinues = 0 lines of code.

Can you see how at times, 70 lines (or whatever number of SubScript.ecl running) of code are in the server, or only 10 lines of code (if just MainScript.ecl), rather then always having all 100 lines of code until all 9 tasks are completed. Not to mention several tasks can be run simulatiously rather then qued.

Light and invis and equip etc all at pretty much the same time, rather then Light then Invis then Equip. These are minor SubScript.ecl idealisms, that would take only a brief instance and so running subscripts for them might not have a HUGE effect on server load. But lets say you were working with 10,000 lines of code (1000 + (9 x 1000)). I could see how having 9 scripts running LARGE tasks at the same time, might be well worth considering.

One of the key points here is that, MainScript.ecl, and each SubScript.ecl only exicutes once. And when complete discontinues and cashe for the script is removed. So the cashe would hopefuly be optamised at any given time.

Another point might be, that yes, "starting" a 100 line code might take less resourse then "starting" 10 small scripts. But what about over say a 1 minute period.

Let's say it takes 1 minute for the whole script to complete. If you have a 100 line code, for 1 whole minute you have 100 lines of code in the server.

But lets say you load 10 small scripts. For 5 seconds you have 100 lines, then from 6-30 seconds you have 70 lines of code, then from 31-55 you have 40 lines of code, then from 56-60 you have only 10.

Again, take these line of code numbers and multiply them by 10, 100 or even 1000. And as a shard as a whole, what is more effective? Where is the bigger drain, new scripts starting, or huge numbers of lines of code held up in the server?

Please more feedback would be greatly appreciated!

Re: What takes more resourses?

Posted: Fri Aug 21, 2009 11:20 am
by Turley
I think you missunderstood me. :)
It doesnt count how much lines of codes you have, what does count is the varsize and ControlStack thats the biggest memory "loss".
A small example:

Code: Select all

program 
  call function1
endprogram
function1
 call function2
endfunction
function2
  call function3
endfunction
function3
....
That means for pol when the script is inside function3 it needs to save the vars from program,function1,function2,function3,....
If you write it like

Code: Select all

program 
  call function1
  call function2
endprogram
function1
 do stuff
endfunction
function2
  do stuff
endfunction
It only needs to save the vars from program.
So CallDepth is the main factor for ressource usage.

Thats almost the same for your example. Every childscript need (depending what you really want to do) the same vars as the mainscript. You can interpret it like my first example every function call stands for a script.
It would be faster, plus you have more control how big the ressources usage is at every state, when you make only one script.

Btw scriptcontrol with CProps is one of the worst ideas :)
You should take a look at Events.
your code would look like

Code: Select all

while (1)
  if (cprop)
    do stuff
    exit
  endif
  sleep()
endwhile
there you have a script that keeps running, you have the problem to find a good value for sleep so it does not uses to much instructions but still reacts fast.
if you write

Code: Select all

while(1)
  ev:=Wait_For_Events(99999);
  if (ev)
    dostuff
    exit
  endif
endwhile
there you have an sleeping script that only (and immediately) acts when something has happend.

Re: What takes more resourses?

Posted: Fri Aug 21, 2009 12:27 pm
by Turley
And dont forget "byref" also very good to transfer big vars from one function to another without an complete copy inside the stack :)

Re: What takes more resourses?

Posted: Fri Aug 21, 2009 5:32 pm
by MuadDib
byref ftw!

GOD I HATE POINTERS

Re: What takes more resourses?

Posted: Fri Aug 21, 2009 8:08 pm
by Damien White
Very interesting, the stacking of variables as such. I can understand why it would need to be done that way. It's too back you can't define what variable would be passed on, ignoring all the rest, thus in turn limiting the stack size. And I am sure this has all been suggested before. What might even be really nice, so as to make the world easier for newbie scripters is a toggle of some sort. One that says, use all the MainScript.ecl variable plus all the SubScript.ecl variables without bias, or use just the variables passed to SubScript.ecl from MainScript.ecl.

Where's Eric when I have a great idea? Heh...

Thanks for all the feedback guys. You have just opened my eyes to what will one day turn out to be a great set of optimized scripts.

Re: What takes more resourses?

Posted: Fri Aug 21, 2009 10:29 pm
by Yukiko
Also remember that a script is kept in memory after its first run. So there's no load time for the *.ecl file after the initial instance.

Re: What takes more resourses?

Posted: Sat Aug 22, 2009 9:18 am
by Damien White
Okay so if I run a script, say Vendor.ecl off an NPC, the NPC is then killed. The script continues to run? What if say I have 50 unique items, with unique scripts. And all the items are single use. And they all get used and will never call the scripts again. Those 50 scripts will continue to run anyways?

I must be confused...because that would after years of a shard running become very messy.

Is thier a way to call a script, exicute it to completion, then unload it? Or could MainScript.ecl, unload from the server all the SubScript.ecls?

Re: What takes more resourses?

Posted: Sat Aug 22, 2009 9:57 am
by Turley
An attached script stops if the attached object is destroyed/killed.
So if an npc is killed the ai stops.
Take a look at os::Unload_Scripts(scriptname := "")
But if I where you i wouldnt care about cached scripts, we are speaking of a few kbytes per ecl. If you keep unloading scripts you will slow down your shard cause it needs to reload them on call.
You should see the scriptcache as a constant factor of your memoryusage, try to optimize your varsizes thats the bigger factor especially if you have scripts with many invocations.

Re: What takes more resourses?

Posted: Sat Aug 22, 2009 7:18 pm
by Damien White
I can't begin to thank you enough. This is the meat and potatoes of coding here.

Over the next few days I am going to post a few scripts, maybe I can get you folks to look them over and see if they could be better optamised another way.

Thanks again.