Confusing information - Memory CPU use in parsing



  • "The more small objects in the Memory, the more CPU spent on its parsing." -tip

    I don't really know what parsing is. I assume it has to do with the "banches". The bigger the tree the longer it takes to go to its smalles branch.

    But as I am not sure I ask : Why small objects = more parsing = more CPU usage? In the sense that we should have bigger objects and less division? Example?

    Thank you in advance for the replies!


  • Culture

    Memory refers to the information stored in the `Memory` object. This is your persistent data. It gets stored as a JSON object, and thus needs to be parsed as JSON when you first access the Memory object. This has nothing to do with code branches.



  • A single large variable will be faster to store and restore every tick than an equivalent amount of data stored in many small variables.

    The decision of whether you should store one big variable (and parse it out yourself) or many smaller variables (and let the built in code handle everything for you) depends mainly on two factors: "Is it worth optimizing yet?", and "Do I access all this data every time, or frequently access only part of it?"

    For example, the system stores the room cost matrix into a single variable, because when you are making a path you usually need most of the matrix anyway. But paths are stored as an array of steps, because you usually only need to see one step (the next step in the path), and there is no reason to decode the entire path out just to retrieve one step.

    It's important to understand that the built-in memory handling uses memoization: you don't pay the decoding cost until you access the variable. So if you don't read the value of a piece of memory it never gets decoded.

    Because of all these factors, the real answer is 'it's complicated'. But in general, if you are storing an array of arrays directly into the memory object, you're probably doing it inefficiently. If access to that memory is frequent, then it's likely eating up a notable amount of CPU and it's worth looking into squishing it into a big string and parsing that string into the full array yourself.



  • Thanks you very much for your answers!

    So I don't need to make a single string for my paths since I only need one direction for each ticks. I may just need to increment a value like :
    i=Memory.paths[pathName].positionInc
    directionToMove = Memory.paths[pathName].arrayOfDirections[i]

    But when using FlowFields to move and entire army, it is better to memoryze the entire FlowField in a string since We'll need the entirety of it all the time. We don't want the retrieve 200 addresses everyticks.

    One last check :
    Wich one of thoses line will take more CPU ?
    Game.screeps.Alin_Du_Jardin.name
    Memory.screeps.Alin_Du_Jardin.name
    We assume that we have a copy or Game.screeps in Memory.screeps.