Parsing time vs code size



  • Hi, am new to screeps and have a question regarding code style.

    Normally I try to optimize for readability, splitting up things in many small functions and modules, and trying to make each part easy to comprehend.

    My running code is just a prototype to get to know game and up the GCL, but I have started on writing something more robust.

    Having read about the new game loop architecture and the CPU buffer, I am still somewhat concerned how careful I should be to keep the LOC count down.

    I have read about the extra overhead when require()'ing many smaller modules. I will bundle things with webpack, but I guess there will still be some overhead.

    Have any of you run into CPU trouble during initialization, even with the buffer?

    Should I take care not to get the code base to big, at the expense of readability?

    Note: I am not talking about performance in the loop, only time used for parsing the source code when the game state is cleared once in a while.


  • Culture

    The require cache itself works pretty well. When you run "require" the code that you're pulling it gets cached, so future statements do not have to parse the code. This means that in essence your code will only be parsed when global resets occur, and those either happen rarely (when uploading code as well as periodically in very small batches throughout the day) or they happen in a large cluster (which we refer to as a reset storm) which the admins compensate for by increasing your bucket.

    In other words, it's not something you should focus on as a source of optimization, especially if it means sacrificing readability. If you do decide you want to reduce your code I recommend using a javascript optimizer (tie it into Grunt if you want) so you can keep your code clean and still get the smaller version. For what it's worth I did try this and the benchmarks showed basically no improvement (and I have an obscene amount of code).



  • This is very nice to know, tedivm. I will go on writing spacious code as usual then... Thanks for your help 🙂



  • mh..  do not underestimate the cpu usage of require. sure, once the code is up and running, it doesn't matter too much, but I had a codebase with a lot of requires at first, and whenever I updated my code it would result in my entire, usually full bucket being completely drained over the next few ticks. Which is annoying if you're the type of coder who likes to do quick iterations, working on live code.

    Since I rewrote my code and started concating with grunt instead of requireing everything, this is not happening anymore.