-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathMemory segment emulation.js
95 lines (82 loc) · 3.09 KB
/
Memory segment emulation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// dissi 29 November 2016 at 07:15
global.CACHE_MEMORY_SEGMENT = {};
global.CACHE_MEMORY_SEGMENT_VERSION = {};
global.CACHE_MEMORY_SEGMENT_DIRTY = {};
global.MEMSEG_ROUTES_matrixes = "MatrixesSwap";
var preLoadOnInit = [
MEMSEG_ROUTES_matrixes
];
var tickSaved = Game.time - 1;
global.initMemorySegments = function() {
var cpuNow = Game.cpu.getUsed();
Memory;
reportDuration("MEMLOAD_ALL", Game.cpu.getUsed() - cpuNow);
for (var i = 0; i < preLoadOnInit.length; i++) {
getMemorySegment(preLoadOnInit[i]);
}
}
global.getMemorySegment = function(theSegmentName) {
if (!Memory.segments) {
Memory.segments = {};
}
if (!Memory.segments[theSegmentName]) {
return {};
}
if (!CACHE_MEMORY_SEGMENT[theSegmentName] || CACHE_MEMORY_SEGMENT_VERSION[theSegmentName] != getMemorySegmentVersion(theSegmentName)) {
var cpuNow = Game.cpu.getUsed();
CACHE_MEMORY_SEGMENT[theSegmentName] = JSON.parse(Memory.segments[theSegmentName]);
CACHE_MEMORY_SEGMENT_VERSION[theSegmentName] = getMemorySegmentVersion(theSegmentName);
var used = Game.cpu.getUsed() - cpuNow;
reportDuration("MEMLOAD_" + theSegmentName, used);
log("Loaded memory segment: [" + theSegmentName + "] took " + used.toFixed(2) + "ms", "memstore")
}
return CACHE_MEMORY_SEGMENT[theSegmentName];
}
global.getMemorySegmentVersion = function(theSegmentName) {
if (!Memory.segmentsBookKeeping) {
Memory.segmentsBookKeeping = {};
}
if (!Memory.segmentsBookKeeping[theSegmentName]) {
Memory.segmentsBookKeeping[theSegmentName] = 0;
}
return Memory.segmentsBookKeeping[theSegmentName];
}
global.increaseMemorySegmentVersion = function(theSegmentName) {
Memory.segmentsBookKeeping[theSegmentName]++;
}
global.storeMemorySegment = function(theSegmentName, theData) {
if (!Memory.segments) {
Memory.segments = {};
}
CACHE_MEMORY_SEGMENT[theSegmentName] = theData;
if (tickSaved == Game.time) // after game loop access
{
handleSaveOfDirtySegment(theSegmentName);
} else {
CACHE_MEMORY_SEGMENT_DIRTY[theSegmentName] = true;
}
}
global.setMemoryDelayStore = function() {
tickSaved = Game.time - 1;
}
global.storeDirtyMemory = function() {
for (var key in CACHE_MEMORY_SEGMENT_DIRTY) {
handleSaveOfDirtySegment(key);
}
CACHE_MEMORY_SEGMENT_DIRTY = {};
tickSaved = Game.time;
}
function handleSaveOfDirtySegment(theSegmentName) {
var cpuNow = Game.cpu.getUsed();
Memory.segments[theSegmentName] = JSON.stringify(CACHE_MEMORY_SEGMENT[theSegmentName]);
increaseMemorySegmentVersion(theSegmentName);
var used = Game.cpu.getUsed() - cpuNow;
reportDuration("MEMSTORE_" + theSegmentName, used);
log("Stored memory segment: [" + theSegmentName + "] took " + used.toFixed(2) + "ms", "memstore");
}
function log(theMessage, theCategory) {
console.log("[" + theCategory + "] - " + theMessage);
}
function reportDuration(theOperationName, theUsedCpu); {
log("Operation [" + theOperationName + "] took [" + theUsedCpu.toFixed(2) + "] CPU", "durationReport")
}