Author Topic: What are some good optimization tips?  (Read 698 times)

I am a huge optimization freak and would like to know some optimization methods/tips including:

  • Making an Add-On have the smallest file size possible.
  • Speeding up loading/downloading for people joining your server. (possibly fits in with above)
  • Preferred methods/norms for code, including things you should absolutely not do. (and ways to make the code run faster!)

And possibly other things that fit the subject of optimization. This topic could also be some kind of source for future and new coders (i am an ametuer so this would be perfect for me)

Coding related things:
1. Do not format your code in a way that conserves space just for the sake of file size. You're saving barely anything and making it harder for people to learn from and modify your code. I even encourage you to add comments, which will increase the file size but also increase the clarity.
2. One common mistake is a for loop being done this way:
for(%i=0;%i<groupobject.getCount();%i++)
This is performing an extra function call each loop. It's pretty noticeable for when you're looping through large groups. Instead:
%count=groupobject.getCount();
for(%i=0;%i<%count;%i++)
This way, .getCount() is only called once
3. Typemask comparisons are better than classname string comparisons.
i.e.:
if(%obj.getClassName() $= "Player")
isn't as efficient as:
if(%obj.getType() & $TypeMasks::PlayerObjectType)
, here's a reference list of typemasks: http://pastebin.com/raw.php?i=dG42p3i6

Problem with #2 is when the group count changes, it'll have some console errors

Don't use objects ever
Don't use functions if you really need speed
In assignments like %x = %y; where you know that %y is an integer, bitwise or it with 0.

Problem with #2 is when the group count changes, it'll have some console errors

You shouldn't be changing the group count in a loop like that anyway

If you are looking for significant performance improvements, you want to try to do three things.
1. Improve the actual asymptotic runtime of your code.
2. Try to use built in functions instead of writing it in torque.
3. Cache stuff

To improve the asymptotic runtime of your code, you need to actually understand how algorithms work and how to determine if something can be done faster.  For example, if you want to sort an array, you can easily do it by finding the largest number in O(n) time, then finding the next largest, and so on until the array is sorted.  However, this results in O(n^2) runtime.  If you implement a real sorting algorithm such as quicksort, you can do this in O(nlogn) time, which is much faster.

You really don't want to actually sort a large list using torquescript.  TS is super slow.  You should not write your own substring method when you can just use the built in getsubstr() method.  Similarly, there is a built in sorting algorithm available to objects with the class GUITextListCtrl.  Making a function that puts your data into a GUITextListCtrl and calls .sort() on it is much faster than actually sorting it yourself.

The easiest way to improve the speed of your code is to cache results that you know you will use over and over again.  Torque global variables are hashed and don't really slow down even when when you are using millions of them.  Go ahead and store whatever data you think might be useful in a $globalvar.  However, using global objects is considerably slower.

You should absolutely NOT worry about the size of your source code.  Trying to minimize your code just makes it harder for yourself and others to understand and maintain it later down the road.
« Last Edit: October 01, 2015, 01:34:58 AM by Nexus »

File size for scripts is completely irrelevant to anything you should care about (as long as you're not deliberately spamming it, of course). Doing so would sacrifice readability.

Things like JavaScript and CSS are often minified in web pages, because the entire file is sent to each client. But Torque never sends any script to any client, all it sends is datablocks, but it sends the actual object, not your script's formatted representation of it.

The biggest factor for add-ons affecting client loading speeds is files they actually do download, like models, sounds, textures, etc.
« Last Edit: October 01, 2015, 08:46:58 AM by Headcrab Zombie »

You shouldn't be changing the group count in a loop like that anyway
And if you did, just adjust %i and %count within the loop accordingly.