-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathLRUCache.js
379 lines (253 loc) · 8.16 KB
/
LRUCache.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
const GIGABYTE_BYTES = 2 ** 30;
class LRUCache {
get unloadPriorityCallback() {
return this._unloadPriorityCallback;
}
set unloadPriorityCallback( cb ) {
if ( cb.length === 1 ) {
console.warn( 'LRUCache: "unloadPriorityCallback" function has been changed to take two arguments.' );
this._unloadPriorityCallback = ( a, b ) => {
const valA = cb( a );
const valB = cb( b );
if ( valA < valB ) return - 1;
if ( valA > valB ) return 1;
return 0;
};
} else {
this._unloadPriorityCallback = cb;
}
}
constructor() {
// options
this.minSize = 6000;
this.maxSize = 8000;
this.minBytesSize = 0.3 * GIGABYTE_BYTES;
this.maxBytesSize = 0.4 * GIGABYTE_BYTES;
this.unloadPercent = 0.05;
this.autoMarkUnused = true;
// "itemSet" doubles as both the list of the full set of items currently
// stored in the cache (keys) as well as a map to the time the item was last
// used so it can be sorted appropriately.
this.itemSet = new Map();
this.itemList = [];
this.usedSet = new Set();
this.callbacks = new Map();
this.unloadingHandle = - 1;
this.cachedBytes = 0;
this.bytesMap = new Map();
this.loadedSet = new Set();
this._unloadPriorityCallback = null;
this.computeMemoryUsageCallback = () => null;
const itemSet = this.itemSet;
this.defaultPriorityCallback = item => itemSet.get( item );
}
// Returns whether or not the cache has reached the maximum size
isFull() {
return this.itemSet.size >= this.maxSize || this.cachedBytes >= this.maxBytesSize;
}
getMemoryUsage( item ) {
return this.bytesMap.get( item ) ?? null;
}
add( item, removeCb ) {
const itemSet = this.itemSet;
if ( itemSet.has( item ) ) {
return false;
}
if ( this.isFull() ) {
return false;
}
const usedSet = this.usedSet;
const itemList = this.itemList;
const callbacks = this.callbacks;
const bytesMap = this.bytesMap;
itemList.push( item );
usedSet.add( item );
itemSet.set( item, Date.now() );
callbacks.set( item, removeCb );
// computeMemoryUsageCallback can return "null" if memory usage is not known, yet
const bytes = this.computeMemoryUsageCallback( item );
this.cachedBytes += bytes || 0;
bytesMap.set( item, bytes );
return true;
}
has( item ) {
return this.itemSet.has( item );
}
remove( item ) {
const usedSet = this.usedSet;
const itemSet = this.itemSet;
const itemList = this.itemList;
const bytesMap = this.bytesMap;
const callbacks = this.callbacks;
const loadedSet = this.loadedSet;
if ( itemSet.has( item ) ) {
this.cachedBytes -= bytesMap.get( item ) || 0;
bytesMap.delete( item );
callbacks.get( item )( item );
const index = itemList.indexOf( item );
itemList.splice( index, 1 );
usedSet.delete( item );
itemSet.delete( item );
callbacks.delete( item );
loadedSet.delete( item );
return true;
}
return false;
}
// Marks whether tiles in the cache have been completely loaded or not. Tiles that have not been completely
// loaded are subject to being disposed early if the cache is full above its max size limits, even if they
// are marked as used.
setLoaded( item, value ) {
const { itemSet, loadedSet } = this;
if ( itemSet.has( item ) ) {
if ( value === true ) {
loadedSet.add( item );
} else {
loadedSet.delete( item );
}
}
}
updateMemoryUsage( item ) {
const itemSet = this.itemSet;
const bytesMap = this.bytesMap;
if ( ! itemSet.has( item ) ) {
return;
}
this.cachedBytes -= bytesMap.get( item ) || 0;
const bytes = this.computeMemoryUsageCallback( item );
bytesMap.set( item, bytes );
this.cachedBytes += bytes;
}
markUsed( item ) {
const itemSet = this.itemSet;
const usedSet = this.usedSet;
if ( itemSet.has( item ) && ! usedSet.has( item ) ) {
itemSet.set( item, Date.now() );
usedSet.add( item );
}
}
markUnused( item ) {
this.usedSet.delete( item );
}
markAllUnused() {
this.usedSet.clear();
}
// TODO: this should be renamed because it's not necessarily unloading all unused content
// Maybe call it "cleanup" or "unloadToMinSize"
unloadUnusedContent() {
const {
unloadPercent,
minSize,
maxSize,
itemList,
itemSet,
usedSet,
loadedSet,
callbacks,
bytesMap,
minBytesSize,
maxBytesSize,
} = this;
const unused = itemList.length - usedSet.size;
const unloaded = itemList.length - loadedSet.size;
const excessNodes = Math.max( Math.min( itemList.length - minSize, unused ), 0 );
const excessBytes = this.cachedBytes - minBytesSize;
const unloadPriorityCallback = this.unloadPriorityCallback || this.defaultPriorityCallback;
let needsRerun = false;
const hasNodesToUnload = excessNodes > 0 && unused > 0 || unloaded && itemList.length > maxSize;
const hasBytesToUnload = unused && this.cachedBytes > minBytesSize || unloaded && this.cachedBytes > maxBytesSize;
if ( hasBytesToUnload || hasNodesToUnload ) {
// used items should be at the end of the array, "unloaded" items in the middle of the array
itemList.sort( ( a, b ) => {
const usedA = usedSet.has( a );
const usedB = usedSet.has( b );
if ( usedA === usedB ) {
const loadedA = loadedSet.has( a );
const loadedB = loadedSet.has( b );
if ( loadedA === loadedB ) {
// Use the sort function otherwise
// higher priority should be further to the left
return - unloadPriorityCallback( a, b );
} else {
return loadedA ? 1 : - 1;
}
} else {
// If one is used and the other is not move the used one towards the end of the array
return usedA ? 1 : - 1;
}
} );
// address corner cases where the minSize might be zero or smaller than maxSize - minSize,
// which would result in a very small or no items being unloaded.
const maxUnload = Math.max( minSize * unloadPercent, excessNodes * unloadPercent );
const nodesToUnload = Math.ceil( Math.min( maxUnload, unused, excessNodes ) );
const maxBytesUnload = Math.max( unloadPercent * excessBytes, unloadPercent * minBytesSize );
const bytesToUnload = Math.min( maxBytesUnload, excessBytes );
let removedNodes = 0;
let removedBytes = 0;
// evict up to the max node or bytes size, keeping one more item over the max bytes limit
// so the "full" function behaves correctly.
while (
this.cachedBytes - removedBytes > maxBytesSize ||
itemList.length - removedNodes > maxSize
) {
const item = itemList[ removedNodes ];
const bytes = bytesMap.get( item ) || 0;
if (
usedSet.has( item ) && loadedSet.has( item ) ||
this.cachedBytes - removedBytes - bytes < maxBytesSize &&
itemList.length - removedNodes <= maxSize
) {
break;
}
removedBytes += bytes;
removedNodes ++;
}
// evict up to the min node or bytes size, keeping one more item over the min bytes limit
// so we're meeting it
while (
removedBytes < bytesToUnload ||
removedNodes < nodesToUnload
) {
const item = itemList[ removedNodes ];
const bytes = bytesMap.get( item ) || 0;
if (
usedSet.has( item ) ||
this.cachedBytes - removedBytes - bytes < minBytesSize &&
removedNodes >= nodesToUnload
) {
break;
}
removedBytes += bytes;
removedNodes ++;
}
// remove the nodes
itemList.splice( 0, removedNodes ).forEach( item => {
this.cachedBytes -= bytesMap.get( item ) || 0;
callbacks.get( item )( item );
bytesMap.delete( item );
itemSet.delete( item );
callbacks.delete( item );
loadedSet.delete( item );
usedSet.delete( item );
} );
// if we didn't remove enough nodes or we still have excess bytes and there are nodes to removed
// then we want to fire another round of unloading
needsRerun = removedNodes < excessNodes || removedBytes < excessBytes && removedNodes < unused;
needsRerun = needsRerun && removedNodes > 0;
}
if ( needsRerun ) {
this.unloadingHandle = requestAnimationFrame( () => this.scheduleUnload() );
}
}
scheduleUnload() {
cancelAnimationFrame( this.unloadingHandle );
if ( ! this.scheduled ) {
this.scheduled = true;
queueMicrotask( () => {
this.scheduled = false;
this.unloadUnusedContent();
} );
}
}
}
export { LRUCache };