Difference between revisions of "MemHack"

From Screeps Wiki
Jump to navigation Jump to search
(MemHack)
 
Line 1: Line 1:
memHack refers to the strategy of storing a user's <code>Memory</code> object in <code>global</code> to skip the <code>JSON.parse()</code> normally required at first-access of <code>Memory</code> in a tick.  Normally, the way <code>Memory</code> works is when first accessed in a tick all of <code>Memory</code> is <code>JSON.parsed()</code> for the tick it is being used, then after any changes have been made end-of-tick it is <code>JSON.stringified()</code> for storage for the next tick it is accessed.  The user running their code pays for this parsing/stringifying with [[CPU]] the larger the <code>Memory</code> object the more costly it is to process.  So, memHack works by creating an object to store <code>Memory</code> in global when global is 'first created' (and needs to be rebuilt if global dies due to code push, global reset, ect), then at the start of a tick (start of a user's main loop) deletes the 'normal' <code>Memory</code> object and replaces it with the global version which due to global persisting between ticks does not need to be parsed.  It finally sets the <code>RawMemory._parsed</code> to the current global memHack obj, this insures changes are updated to the normal <code>Memory</code> object, so that in the event of a global reset when reconstructing the <code>Memory</code> object will still have the changes and that the Memory watcher / console changing still work.  It is important to note, that <code>RawMemory._parsed</code> is not officially documented in the API however it has been around and accessible for several years.  It is possible for this method to become defunct in the future.
+
memHack refers to the strategy of storing a user's <code>Memory</code> object in <code>global</code> to skip the <code>JSON.parse()</code> normally required at first-access of <code>Memory</code> in a tick.  Normally, the way <code>Memory</code> works is when first accessed in a tick all of <code>Memory</code> is <code>JSON.parsed()</code> for the tick it is being used, then after any changes have been made end-of-tick it is <code>JSON.stringify()</code> ed for storage for the next tick it is accessed.  The user running their code pays for this parsing/stringifying with [[CPU]] the larger the <code>Memory</code> object the more costly it is to process.  So, memHack works by creating an object to store <code>Memory</code> in global when global is 'first created' (and needs to be rebuilt if global dies due to code push, global reset, ect), then at the start of a tick (start of a user's main loop) deletes the 'normal' <code>Memory</code> object and replaces it with the global version which due to global persisting between ticks does not need to be parsed.  It finally sets the <code>RawMemory._parsed</code> to the current global memHack obj, this insures changes are updated to the normal <code>Memory</code> object, so that in the event of a global reset when reconstructing the <code>Memory</code> object will still have the changes and that the Memory watcher / console changing still work.  It is important to note, that <code>RawMemory._parsed</code> is not officially documented in the API however it has been around and accessible for several years.  It is possible for this method to become defunct in the future.
  
 
== Code Examples ==
 
== Code Examples ==

Revision as of 17:51, 29 March 2021

memHack refers to the strategy of storing a user's Memory object in global to skip the JSON.parse() normally required at first-access of Memory in a tick. Normally, the way Memory works is when first accessed in a tick all of Memory is JSON.parsed() for the tick it is being used, then after any changes have been made end-of-tick it is JSON.stringify() ed for storage for the next tick it is accessed. The user running their code pays for this parsing/stringifying with CPU the larger the Memory object the more costly it is to process. So, memHack works by creating an object to store Memory in global when global is 'first created' (and needs to be rebuilt if global dies due to code push, global reset, ect), then at the start of a tick (start of a user's main loop) deletes the 'normal' Memory object and replaces it with the global version which due to global persisting between ticks does not need to be parsed. It finally sets the RawMemory._parsed to the current global memHack obj, this insures changes are updated to the normal Memory object, so that in the event of a global reset when reconstructing the Memory object will still have the changes and that the Memory watcher / console changing still work. It is important to note, that RawMemory._parsed is not officially documented in the API however it has been around and accessible for several years. It is possible for this method to become defunct in the future.

Code Examples

There are a few places you can view examples of this code:

postCrafter - Screep's Snippets

ags131 - ZeSwarm

bencbartlett - OverMind