https://wiki.screepspl.us/index.php?title=MemHack&feed=atom&action=historyMemHack - Revision history2024-03-28T21:20:16ZRevision history for this page on the wikiMediaWiki 1.37.1https://wiki.screepspl.us/index.php?title=MemHack&diff=3405&oldid=prevFaff: Added the words 'Memory Hack' so that the wiki search will actually produce this page.2021-07-30T02:09:58Z<p>Added the words 'Memory Hack' so that the wiki search will actually produce this page.</p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 02:09, 30 July 2021</td>
</tr><tr>
<td colspan="2" class="diff-lineno">Line 1:</td>
<td colspan="2" class="diff-lineno">Line 1:</td>
</tr>
<tr>
<td class="diff-marker">−</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>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, thus saving [[CPU]] every tick. Note that the hack is exactly that: a hack. It relies on the underlying implementation of the API, not the published API itself, giving it the potential to become obsolete or broken in the future. </div></td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>MemHack<ins style="font-weight: bold; text-decoration: none;"> (''Memory Hack'')</ins> 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, thus saving [[CPU]] every tick. Note that the hack is exactly that: a hack. It relies on the underlying implementation of the API, not the published API itself, giving it the potential to become obsolete or broken in the future. </div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Memory Implementation ==</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Memory Implementation ==</div></td>
</tr>
</table>Faffhttps://wiki.screepspl.us/index.php?title=MemHack&diff=3399&oldid=prevSemperRabbit: /* Risky Business */2021-06-27T20:58:13Z<p><span dir="auto"><span class="autocomment">Risky Business</span></span></p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 20:58, 27 June 2021</td>
</tr><tr>
<td colspan="2" class="diff-lineno">Line 10:</td>
<td colspan="2" class="diff-lineno">Line 10:</td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Risky Business ==</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Risky Business ==</div></td>
</tr>
<tr>
<td class="diff-marker">−</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>If you are risk adverse or want additional savings, you can delete <code>RawMemory._parsed</code> for a tick, preventing serialization for that tick. You can add a control structure using <code>Game.time % x === 0</code> to have it only serialize every 5, 10, etc ticks. The lower the gap between saves, the easier it would be to recover from the older copy of Memory serialized in the backend. This could lose data such as recent [[Market]] transactions, intelligence gathered from [[StructureObserver|Observers]], and other things.</div></td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>If you are<ins style="font-weight: bold; text-decoration: none;"> not</ins> risk adverse or want additional savings, you can delete <code>RawMemory._parsed</code> for a tick, preventing serialization for that tick. You can add a control structure using <code>Game.time % x === 0</code> to have it only serialize every 5, 10, etc ticks. The lower the gap between saves, the easier it would be to recover from the older copy of Memory serialized in the backend. This could lose data such as recent [[Market]] transactions, intelligence gathered from [[StructureObserver|Observers]], and other things.</div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Code Examples ==</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Code Examples ==</div></td>
</tr>
</table>SemperRabbithttps://wiki.screepspl.us/index.php?title=MemHack&diff=3398&oldid=prevSemperRabbit: General cleanup; addition of "Risky Business" section, detailing preventing serialization after the tick.2021-06-26T13:01:02Z<p>General cleanup; addition of "Risky Business" section, detailing preventing serialization after the tick.</p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 13:01, 26 June 2021</td>
</tr><tr>
<td colspan="2" class="diff-lineno">Line 1:</td>
<td colspan="2" class="diff-lineno">Line 1:</td>
</tr>
<tr>
<td class="diff-marker">−</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>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, thus saving [[CPU]] every tick. </div></td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>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, thus saving [[CPU]] every tick<ins style="font-weight: bold; text-decoration: none;">. Note that the hack is exactly that: a hack. It relies on the underlying implementation of the API, not the published API itself, giving it the potential to become obsolete or broken in the future</ins>. </div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>== Memory Implementation ==</div></td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker"><a class="mw-diff-movedpara-right" title="Paragraph was moved. Click to jump to old location." href="#movedpara_7_0_lhs">⚫</a></td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><a name="movedpara_2_1_rhs"></a>The way the <code>Memory</code> object works is when first accessed in a game tick it is run though a <code>JSON.parse()</code> which converts it to an object from its stored-as-a-string state, then after any changes have been made end-of-tick it is run though a <code>JSON.stringify()</code> for storage. The user running the code pays for this parsing/stringifying with [[CPU]]<ins style="font-weight: bold; text-decoration: none;">:</ins> the larger the <code>Memory</code> object<ins style="font-weight: bold; text-decoration: none;">,</ins> the more costly it is to process. </div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>When Memory is parsed naturally, the parsed object is stored in <code>RawMemory._parsed</code>. At the end of the tick (after your code ends), the engine serializes the value in <code>RawMemory._parsed</code> into the database, for use in the next tick. The time taken for serialization is included in your CPU usage. You can read more about the <code>Memory</code> object in [https://docs.screeps.com/global-objects.html#Memory-object Screep's official docs]. </div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>== Operation ==</div></td>
</tr>
<tr>
<td class="diff-marker"><a class="mw-diff-movedpara-left" title="Paragraph was moved. Click to jump to new location." href="#movedpara_2_1_rhs">⚫</a></td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><a name="movedpara_7_0_lhs"></a>The way the <code>Memory</code> object works is when first accessed in a game tick it is run though a <code>JSON.parse()</code> which converts it to an object from its stored-as-a-string state, then after any changes have been made end-of-tick it is run though a <code>JSON.stringify()</code> for storage. The user running the code pays for this parsing/stringifying with [[CPU]] the larger the <code>Memory</code> object the more costly it is to process. <del style="font-weight: bold; text-decoration: none;"> You can read more about the <code>Memory</code> object in [https://docs.screeps.com/global-objects.html#Memory-object Screep's offical docs].</del> </div></td>
<td colspan="2" class="diff-empty"> </td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker"><a class="mw-diff-movedpara-right" title="Paragraph was moved. Click to jump to old location." href="#movedpara_13_0_lhs">⚫</a></td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><a name="movedpara_8_0_rhs"></a>MemHack works by creating an object in <code>global</code> to store the <code>Memory</code> object when initializing the global (and needs to be rebuilt if global dies due to code push, global reset, ect)<ins style="font-weight: bold; text-decoration: none;">.</ins> <ins style="font-weight: bold; text-decoration: none;">At</ins> the start of <ins style="font-weight: bold; text-decoration: none;">subsequent</ins> <ins style="font-weight: bold; text-decoration: none;">ticks</ins> (start of a user's main loop before any other <code>Memory</code> related calls) 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. Then it sets <code>RawMemory._parsed</code> to the current global MemHack obj as well, this insures changes are updated to the normal <code>Memory</code> object, so that in the event of a global reset 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 strategy to become defunct in the future, should this be changed.</div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>== Risky Business ==</div></td>
</tr>
<tr>
<td class="diff-marker">−</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"></td>
<td colspan="2" class="diff-empty"> </td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>If you are risk adverse or want additional savings, you can delete <code>RawMemory._parsed</code> for a tick, preventing serialization for that tick. You can add a control structure using <code>Game.time % x === 0</code> to have it only serialize every 5, 10, etc ticks. The lower the gap between saves, the easier it would be to recover from the older copy of Memory serialized in the backend. This could lose data such as recent [[Market]] transactions, intelligence gathered from [[StructureObserver|Observers]], and other things.</div></td>
</tr>
<tr>
<td class="diff-marker"><a class="mw-diff-movedpara-left" title="Paragraph was moved. Click to jump to new location." href="#movedpara_8_0_rhs">⚫</a></td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><a name="movedpara_13_0_lhs"></a><del style="font-weight: bold; text-decoration: none;">So, </del>MemHack works by creating an object in <code>global</code> to store the <code>Memory</code> object when initializing the global (and needs to be rebuilt if global dies due to code push, global reset, ect)<del style="font-weight: bold; text-decoration: none;">, then</del> <del style="font-weight: bold; text-decoration: none;">at</del> the start of <del style="font-weight: bold; text-decoration: none;">a</del> <del style="font-weight: bold; text-decoration: none;">tick</del> (start of a user's main loop before any other <code>Memory</code> related calls) 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. Then it sets <code>RawMemory._parsed</code> to the current global MemHack obj as well, this insures changes are updated to the normal <code>Memory</code> object, so that in the event of a global reset 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 strategy to become defunct in the future, should this be changed.</div></td>
<td colspan="2" class="diff-empty"> </td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Code Examples ==</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Code Examples ==</div></td>
</tr>
</table>SemperRabbithttps://wiki.screepspl.us/index.php?title=MemHack&diff=3360&oldid=prevDonatzor: wording.2021-03-29T23:43:45Z<p>wording.</p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 23:43, 29 March 2021</td>
</tr><tr>
<td colspan="2" class="diff-lineno">Line 1:</td>
<td colspan="2" class="diff-lineno">Line 1:</td>
</tr>
<tr>
<td class="diff-marker">−</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;">memHack</del> 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. </div></td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">MemHack</ins> 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<ins style="font-weight: bold; text-decoration: none;"> tick, thus saving [[CPU]] every</ins> tick. </div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td class="diff-marker"><a class="mw-diff-movedpara-left" title="Paragraph was moved. Click to jump to new location." href="#movedpara_4_0_rhs">⚫</a></td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><a name="movedpara_2_0_lhs"></a><del style="font-weight: bold; text-decoration: none;">Normally, the</del> way <code>Memory</code> works is when first accessed in a tick <del style="font-weight: bold; text-decoration: none;">all of <code>Memory</code></del> is run though a <code>JSON.parse()</code> <del style="font-weight: bold; text-decoration: none;">for</del> <del style="font-weight: bold; text-decoration: none;">the</del> <del style="font-weight: bold; text-decoration: none;">tick</del> it <del style="font-weight: bold; text-decoration: none;">is</del> <del style="font-weight: bold; text-decoration: none;">being</del> <del style="font-weight: bold; text-decoration: none;">used</del>, then after any changes have been made end-of-tick it is run though <code>JSON.stringify()</code> for storage<del style="font-weight: bold; text-decoration: none;"> for the next tick it is accessed</del>. The user running <del style="font-weight: bold; text-decoration: none;">their</del> code pays for this parsing/stringifying with [[CPU]] the larger the <code>Memory</code> object the more costly it is to process. </div></td>
<td colspan="2" class="diff-empty"> </td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker"><a class="mw-diff-movedpara-right" title="Paragraph was moved. Click to jump to old location." href="#movedpara_2_0_lhs">⚫</a></td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><a name="movedpara_4_0_rhs"></a><ins style="font-weight: bold; text-decoration: none;">The</ins> way<ins style="font-weight: bold; text-decoration: none;"> the</ins> <code>Memory</code><ins style="font-weight: bold; text-decoration: none;"> object</ins> works is when first accessed in a<ins style="font-weight: bold; text-decoration: none;"> game</ins> tick <ins style="font-weight: bold; text-decoration: none;">it</ins> is run though a <code>JSON.parse()</code> <ins style="font-weight: bold; text-decoration: none;">which</ins> <ins style="font-weight: bold; text-decoration: none;">converts</ins> it <ins style="font-weight: bold; text-decoration: none;">to</ins> <ins style="font-weight: bold; text-decoration: none;">an object from its stored-as-a-string</ins> <ins style="font-weight: bold; text-decoration: none;">state</ins>, then after any changes have been made end-of-tick it is run though<ins style="font-weight: bold; text-decoration: none;"> a</ins> <code>JSON.stringify()</code> for storage. The user running <ins style="font-weight: bold; text-decoration: none;">the</ins> code pays for this parsing/stringifying with [[CPU]] the larger the <code>Memory</code> object the more costly it is to process<ins style="font-weight: bold; text-decoration: none;">. You can read more about the <code>Memory</code> object in [https://docs.screeps.com/global-objects.html#Memory-object Screep's offical docs]</ins>. </div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td class="diff-marker">−</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>So, <del style="font-weight: bold; text-decoration: none;">memHack</del> works by creating an object to store <code>Memory</code> <del style="font-weight: bold; text-decoration: none;">in global</del> when <del style="font-weight: bold; text-decoration: none;">global</del> <del style="font-weight: bold; text-decoration: none;">is</del> <del style="font-weight: bold; text-decoration: none;">'first created'</del> (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. <del style="font-weight: bold; text-decoration: none;">It</del> <del style="font-weight: bold; text-decoration: none;">finally</del> sets<del style="font-weight: bold; text-decoration: none;"> the</del> <code>RawMemory._parsed</code> to the current global <del style="font-weight: bold; text-decoration: none;">memHack</del> obj, this insures changes are updated to the normal <code>Memory</code> object, so that in the event of a global reset<del style="font-weight: bold; text-decoration: none;"> when reconstructing</del> 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 <del style="font-weight: bold; text-decoration: none;">method</del> to become defunct in the future.</div></td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>So, <ins style="font-weight: bold; text-decoration: none;">MemHack</ins> works by creating an object<ins style="font-weight: bold; text-decoration: none;"> in <code>global</code></ins> to store<ins style="font-weight: bold; text-decoration: none;"> the</ins> <code>Memory</code> <ins style="font-weight: bold; text-decoration: none;">object</ins> when <ins style="font-weight: bold; text-decoration: none;">initializing</ins> <ins style="font-weight: bold; text-decoration: none;">the</ins> <ins style="font-weight: bold; text-decoration: none;">global</ins> (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<ins style="font-weight: bold; text-decoration: none;"> before any other <code>Memory</code> related calls</ins>) 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. <ins style="font-weight: bold; text-decoration: none;">Then</ins> <ins style="font-weight: bold; text-decoration: none;">it</ins> sets <code>RawMemory._parsed</code> to the current global <ins style="font-weight: bold; text-decoration: none;">MemHack</ins> obj<ins style="font-weight: bold; text-decoration: none;"> as well</ins>, this insures changes are updated to the normal <code>Memory</code> object, so that in the event of a global reset the <code>Memory</code> object will still have the changes and<ins style="font-weight: bold; text-decoration: none;">,</ins> 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<ins style="font-weight: bold; text-decoration: none;">,</ins> it has been around and accessible for several years. It is possible for this <ins style="font-weight: bold; text-decoration: none;">strategy</ins> to become defunct in the future<ins style="font-weight: bold; text-decoration: none;">, should this be changed</ins>.</div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Code Examples ==</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Code Examples ==</div></td>
</tr>
</table>Donatzorhttps://wiki.screepspl.us/index.php?title=MemHack&diff=3359&oldid=prevDonatzor at 17:55, 29 March 20212021-03-29T17:55:08Z<p></p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 17:55, 29 March 2021</td>
</tr><tr>
<td colspan="2" class="diff-lineno">Line 10:</td>
<td colspan="2" class="diff-lineno">Line 10:</td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>There are a few places you can view examples of this code:</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>There are a few places you can view examples of this code:</div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td class="diff-marker">−</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>[https://github.com/screepers/screeps-snippets/blob/master/src/misc/JavaScript/Memory%20Cache.js postCrafter - <del style="font-weight: bold; text-decoration: none;">Screep</del>'<del style="font-weight: bold; text-decoration: none;">s</del> Snippets]</div></td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>[https://github.com/screepers/screeps-snippets/blob/master/src/misc/JavaScript/Memory%20Cache.js postCrafter - <ins style="font-weight: bold; text-decoration: none;">Screepers</ins>' Snippets]</div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[https://github.com/ags131/ZeSwarm/blob/v1.1/src/MemHack.js ags131 - ZeSwarm]</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[https://github.com/ags131/ZeSwarm/blob/v1.1/src/MemHack.js ags131 - ZeSwarm]</div></td>
</tr>
</table>Donatzorhttps://wiki.screepspl.us/index.php?title=MemHack&diff=3358&oldid=prevDonatzor at 17:52, 29 March 20212021-03-29T17:52:58Z<p></p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 17:52, 29 March 2021</td>
</tr><tr>
<td colspan="2" class="diff-lineno">Line 1:</td>
<td colspan="2" class="diff-lineno">Line 1:</td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>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. </div></td>
</tr>
<tr>
<td class="diff-marker"><a class="mw-diff-movedpara-left" title="Paragraph was moved. Click to jump to new location." href="#movedpara_2_5_rhs">⚫</a></td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><a name="movedpara_1_0_lhs"></a><del style="font-weight: bold; text-decoration: none;">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. </del>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.</div></td>
<td colspan="2" class="diff-empty"> </td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>Normally, the way <code>Memory</code> works is when first accessed in a tick all of <code>Memory</code> is run though a <code>JSON.parse()</code> for the tick it is being used, then after any changes have been made end-of-tick it is run though <code>JSON.stringify()</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. </div></td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker"><a class="mw-diff-movedpara-right" title="Paragraph was moved. Click to jump to old location." href="#movedpara_1_0_lhs">⚫</a></td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><a name="movedpara_2_5_rhs"></a>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.</div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Code Examples ==</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Code Examples ==</div></td>
</tr>
</table>Donatzorhttps://wiki.screepspl.us/index.php?title=MemHack&diff=3357&oldid=prevDonatzor at 17:51, 29 March 20212021-03-29T17:51:14Z<p></p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 17:51, 29 March 2021</td>
</tr><tr>
<td colspan="2" class="diff-lineno">Line 1:</td>
<td colspan="2" class="diff-lineno">Line 1:</td>
</tr>
<tr>
<td class="diff-marker">−</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>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.<del style="font-weight: bold; text-decoration: none;">stringified</del>()</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.</div></td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>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.<ins style="font-weight: bold; text-decoration: none;">stringify</ins>()</code><ins style="font-weight: bold; text-decoration: none;"> ed</ins> 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.</div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Code Examples ==</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>== Code Examples ==</div></td>
</tr>
</table>Donatzorhttps://wiki.screepspl.us/index.php?title=MemHack&diff=3356&oldid=prevDonatzor: MemHack2021-03-29T17:42:44Z<p>MemHack</p>
<p><b>New page</b></p><div>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.<br />
<br />
== Code Examples ==<br />
There are a few places you can view examples of this code:<br />
<br />
[https://github.com/screepers/screeps-snippets/blob/master/src/misc/JavaScript/Memory%20Cache.js postCrafter - Screep's Snippets]<br />
<br />
[https://github.com/ags131/ZeSwarm/blob/v1.1/src/MemHack.js ags131 - ZeSwarm]<br />
<br />
[https://github.com/bencbartlett/Overmind/blob/master/src/memory/Memory.ts#L83 bencbartlett - OverMind]</div>Donatzor