Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

Code Optimization Tips and Tricks

Discussion in 'Programming (In-game)' started by ASmilingNinja, Mar 6, 2019.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. ASmilingNinja Trainee Engineer

    Messages:
    4
    Hello all,

    I'm looking for some tips on how to make scripts run as fast as possible.

    What's the best place to use the GetBlock methods? Is it possible to just get the blocks the first time the program runs and keep them for all following executions or is this a bad idea?

    What other things have you figured out that can improve execution time?

    Thanks
     
  2. mojomann71 Senior Engineer

    Messages:
    1,952
    I have seen this several times now. What are you guys doing to make scripts not be fast? I have only found one script that caused issues out of dozens that I have used. :)
     
    • Funny Funny x 1
  3. Old Garyman Trainee Engineer

    Messages:
    10
    I think with ingame scripts you don't have the possibility to force the CPU to its knees unless you program an infinite loop. Haven't tested it myself.
     
  4. ASmilingNinja Trainee Engineer

    Messages:
    4
    It's not that it runs slow, I just don't see a downside to writing the best code you can to use fewer resources.
     
  5. Sero93 Trainee Engineer

    Messages:
    47
    first of all, yes performance is a thing, and the most important one for multiplayer. @Old Garyman is on the wrong track. yes you cannot write super cpu demanding scripts, because there are hardcaps for how many executions and lines of code in place, but you have to take into account, that the scripts are running ontop of the game logic. to get a decent FPS you have around 17ms execution time per Frame, *including* all game physic etc. many scripts will simply laag out your multiplayer game with ease, for example the TIM inventory manager is a very common usedscript that ruins performance. Most Top scripts from Whiplash and all the other top authors a very intensivly optimised to be suitable to run in MP. Especially if you consider that multiple people use the same script on a server with many other scripts together, you are actullay quite limited in what you are doing.

    i am also not very informed about performance but the basic principles i can explain.

    1.) caching
    as you said, gaining the reference to a block once and reusing is a very fundamental one. Whenever you "GetBlocks", the script has to iterate over EACH block on that grid, performing that each script execution will quickly make your game a diashow. Cache all block references at startup or very saldom on special occasions when needed only.
    Also declaring big variable structs like huge lists etc. should be avoided. instead of creating a list, using it and deleteing it each loop, just keep the list around as field and just adapt the data as needed. same for other datastructures.

    2.) general performance
    Your script does run ontop of your game and is kinda embeded into it, so you cannot compare the code speeds to a console application, where the code is low level accesing your cpu. you should aim for all the performance gains you can. as such avoid big string allocations/operations, consider hashing strings or use string builders. Avoid using ".length" and use ".lengthSqrt" if possible. ".length" uses a squareroot, a very costly operation. it is much better to square the other site of the equation than using a square root instead. Memmory allocation is always costly. Stay away from unneccesarry huge loops and try to find a smarter math way for complex calculations.

    3.) spliting / runs per minute
    At some points long, exsessive calculations might be needed, or you need to step through big grids etc. these things might not be optimisable enough so splitting too complex things up is a solution. you could use a statmachine or similar systems to spread your load over multiple or even many loops. The loops itself should also be watched. just do what you need to when you need to do. you can use timings on the script itself and don`t run everything every tick. consider the maximum needed tickrate for components of your script and execute them only when really needed.

    and many more.... have a look in the keen discord, those guys know more than me and will mostlikely help you even further.
     
    • Like Like x 2
    • Agree Agree x 1
  6. Malware Master Engineer

    Messages:
    9,837
    @Sero93 this is a very good summary, well done. I stand behind everything you said here.

    In short, scripts must be viewed as "hot code" as they run within the tight logic update loop of the game and not independently. Unfortunately.
     
    • Like Like x 1
    • Friendly Friendly x 1
  7. Sero93 Trainee Engineer

    Messages:
    47
    Thanks @Malware, all learned by you and the rest of the gang at keens discord. Running under „Simon_Winter“ there, but wasn’t active for a while
     
  8. Old Garyman Trainee Engineer

    Messages:
    10
    Yeah, I was on the wrong track. I thought about it once and my statement at the beginning of the post is wrong and unproductive, sorry.

    Maybe a few tips from me:
    - Stop loops prematurely, if possible
    - Create strings with string.format or stringBuilder
    - Avoid casts, if possible
     
    • Like Like x 1
  9. Malware Master Engineer

    Messages:
    9,837
    Casts aren't that costly.
     
Thread Status:
This last post in this thread was made more than 31 days old.