CPU Usage with require



  • Hello my fellow Screepsers! (Screepsers?)

    I know this topic has been already treated but I didn't find a single answer really going through some particulars about CPU usage and module inclusions.

    It's stated that including modules has a cost in CPU, going against maximum available CPU for every tick.

    It's also stated that every X ticks the game rebuilds modules so the cost in CPU for modules inclusion is higher.

    It is not clear how much does modules weight and if this weight is influenced by the number of entities the modules are touching. For example if I have a module extending the Creep prototype, will the cost of its inclusion rise if I have more creeps? Or is it always the same and then the creeps actions will normally weight more the more I have?

    And another question: would the code consume less CPU should I put all my functions, prototypes, everything, in the main file? Ok it woul be really messy, but should it remove CPU costs for including, it could be useful anyway, no?

    Thanks again in advance for all the useful answers!

     



  • Your modules will be cached (with a past update) and so CPU usage is remarkably minimal most of the time (a jump at the start for the cache is normal).

    Theoretically you may receive some kind of boost if you included everything into one file but since this change this hasn't been an issue in terms of CPU performance. Your not going to gain anything significant by changing this, you'll just lose sanity by having to stare at a gigantic file.

    I haven't noticed any cpu gain by using my scripts more often either. I suggest if you're looking for improvements you take a look at the CPU profiler



  • It may be useful if I add some statistics I got about my CPU usage in different stages of my scripts.

    The standard usage is like this:

    Used 2.3 for Modules /-/ Used 0.7 for Flags /-/ Used 3.9 for Creeps /-/ Used 0.7 for Rooms

    With peaks going about this, doubling the CPU needed for modules inclusion:

    Used 5.2 for Modules /-/ Used 0.8 for Flags /-/ Used 3.5 for Creeps /-/ Used 0.4 for Rooms

    Then sometimes I get this:

    Used 21.1 for Modules /-/ Used 0.9 for Flags /-/ Used 3.8 for Creeps /-/ Used 0.6 for Rooms

    Now being my module files always the same, how can it take ten times to includ them sometimes? I know it's only a peak and the bucket can absorb it, but I'm also managing a single room with quite few different logics. I will of course need to extend a lot my code, thus increasing modules count and size. It can be hard to balance their weight being their inclusion cost so variable.

    Tips? Suggestions? 🙂



  • You need to reevaluate how you are getting those numbers, I have a feeling you're measuring something else other then the modules being loaded in. I've done tests in the past and they never came close to even 1 CPU.



  • RaskVann we were writing at the same time. 😛

    Thanks for the link, I'll take a look at it. Also thanks for the useful informations. 🙂



  • Ouch, we wrote again at the same time! 😛

    Well I've just put a Game.cpu.getUsed() at the start of the main file, and one after the last require instruction.

    All the required file are prototype extensions and module exportations, there's not further code executed before the main cycle (unless Skynet took over and I'm not aware!). 🙂