Easily solvable memory parsing inefficency



  • I have been experimenting with the Memory object for awhile. I decided to modify the JSON.parse and JSON.stringify method to see when Screeps was parsing and stringifying Memory:

    let stringify = JSON.stringify;
    JSON.stringify = (object) => {
      if(object === Memory) {
        console.log("stringifyed Memory");
      }
      return stringify(object);
    };
    let parse = JSON.parse;
    JSON.parse = (string) => {
      //closest way to checking if the string being parsed is Memory. 
      //should work for any Memory that is correctly formatted.
      if(parse(string).creeps) {
        console.log("parsed Memory");
      }
      return parse(string);
    }
    module.exports.loop = function () {
      //Memory is referenced to cause Screeps to perceve memory as active
      Memory;
    }
    

    each tick the probe code posts this to the console:

    [shard3] parsed Memory
    [shard3] stringifyed Memory
    

    Business as normal right? Here is the issue: variables can be loaded before the game loop on startup. There is no reason Screeps should parse the Memory each tick. Memory could be parsed before the game loop, then stringifyed each tick. it would get the job done without as much overhead.

    Is there any reason that the Memory has to be parsed each tick? If so I would be curious to know why.



  • If you're talking about parsing it outside the game loop to save overhead, then you're just shifting the cost of the JSON parsing from the user's code, to the server. The cost is still paid.

    The first time you touch Memory in a tick, it gets parsed from the string. There's a technique called the "memory trick" which you can find on slack, which stores the Memory object in global between ticks, and then at the start of the tick it retrieves the saved Memory object, rather than parsing it from the string representation. This means that on most ticks you don't need to parse Memory at all and save some valuable CPU.

    On the other hand, the post-tick stringifying is hard to avoid. To minimise the effect of this, you can store as much as possible in the segments instead of Memory, and then you can control how frequently data is committed to segments and the cost of any encoding you choose to use.


  • Dev Team

    Please see this feature request. Closing, since this is not a bug.


Locked