weak-lru-cache
Advanced tools
Comparing version 1.1.4 to 1.2.0
@@ -7,3 +7,3 @@ const PINNED_IN_MEMORY = 0x7fffffff | ||
/* bit pattern: | ||
* < is-in-lru 1 bit > ...< mask/or bits 4 bits > <lru index 4 bits > < position in cache - 16 bits > | ||
* < is-in-lru 1 bit > ...< mask/or bits 6 bits > <lru index 2 bits > < position in cache - 22 bits > | ||
*/ | ||
@@ -13,2 +13,4 @@ export class LRFUExpirer { | ||
this.lruSize = options && options.lruSize || 0x2000 | ||
if (this.lruSize > 0x400000) | ||
throw new Error('The LRU/cache size was larger than the maximum cache size of 16777216 (LRU size of 4194304)') | ||
this.reset() | ||
@@ -19,3 +21,3 @@ startTimedCleanup(new WeakRef(this), options && options.cleanupInterval || 60000) | ||
if (entry.position < NOT_IN_LRU) { | ||
this.lru[(entry.position >> 16) & 15][entry.position & 0xffff] = null | ||
this.lru[(entry.position >> 22) & 3][entry.position & 0x3fffff] = null | ||
} | ||
@@ -30,3 +32,3 @@ entry.position |= NOT_IN_LRU | ||
if (entry.position < NOT_IN_LRU) { | ||
this.lru[(entry.position >> 16) & 15][entry.position & 0xffff] = null | ||
this.lru[(entry.position >> 22) & 3][entry.position & 0x3fffff] = null | ||
} | ||
@@ -48,3 +50,3 @@ entry.position = PINNED_IN_MEMORY | ||
if (originalPosition >= 0) | ||
expirationPriority = (originalPosition >> 20) & 15 | ||
expirationPriority = (originalPosition >> 24) & 0x3f | ||
else | ||
@@ -57,3 +59,3 @@ expirationPriority = 0 | ||
if (originalPosition < NOT_IN_LRU) { | ||
lruIndex = (originalPosition >> 16) & 15 | ||
lruIndex = (originalPosition >> 22) & 3 | ||
if (lruIndex >= 3) | ||
@@ -66,3 +68,3 @@ return // can't get any higher than this, don't do anything | ||
return // only recently added, don't promote | ||
lru[originalPosition & 0xffff] = null // remove it, we are going to move/promote it | ||
lru[originalPosition & 0x3fffff] = null // remove it, we are going to move/promote it | ||
lruIndex++ | ||
@@ -75,14 +77,14 @@ } else | ||
let lruPosition, nextLru = this.lru[lruIndex] | ||
let orMask = 0xffff >> (16 - expirationPriority) | ||
let orMask = 0x3fffff >> (22 - expirationPriority) | ||
do { | ||
// put it in the next lru | ||
lruPosition = nextLru.position | orMask | ||
let previousEntry = nextLru[lruPosition & 0xffff] | ||
nextLru[lruPosition & 0xffff] = entry | ||
let previousEntry = nextLru[lruPosition & 0x3fffff] | ||
nextLru[lruPosition & 0x3fffff] = entry | ||
if (entry) | ||
entry.position = lruPosition | (expirationPriority << 20) | ||
entry.position = lruPosition | (expirationPriority << 24) | ||
nextLru.position = ++lruPosition | ||
if ((lruPosition & 0xffff) >= this.lruSize) { | ||
if ((lruPosition & 0x3fffff) >= this.lruSize) { | ||
// reset at the beginning of the lru cache | ||
lruPosition &= 0x7fff0000 | ||
lruPosition &= 0x7fc00000 | ||
nextLru.position = lruPosition | ||
@@ -93,4 +95,4 @@ nextLru.cycles++ | ||
if (entry && (nextLru = this.lru[--lruIndex])) { | ||
expirationPriority = ((entry.position || 0) >> 20) & 15 | ||
orMask = 0xffff >> (16 - expirationPriority) | ||
expirationPriority = ((entry.position || 0) >> 24) & 0x3f | ||
orMask = 0x3fffff >> (22 - expirationPriority) | ||
} else | ||
@@ -125,3 +127,3 @@ break | ||
this.lru[i] = new Array(this.lruSize) | ||
this.lru[i].position = i << 16 | ||
this.lru[i].position = i << 22 | ||
this.lru[i].cycles = 0 | ||
@@ -135,9 +137,9 @@ } | ||
for (let j = 0, l = toClear; j < l; j++) { | ||
if (lru[lru.position & 0xffff]) { | ||
if (lru[lru.position & 0x3fffff]) { | ||
toClear-- | ||
this.insertEntry(null, i, 0) | ||
} else { | ||
if ((++lru.position & 0xffff) >= this.lruSize) { | ||
if ((++lru.position & 0x3fffff) >= this.lruSize) { | ||
// reset at the beginning of the lru cache | ||
lru.position &= 0x7fff0000 | ||
lru.position &= 0x7fc00000 | ||
lru.cycles++ | ||
@@ -144,0 +146,0 @@ } |
{ | ||
"name": "weak-lru-cache", | ||
"author": "Kris Zyp", | ||
"version": "1.1.4", | ||
"version": "1.2.0", | ||
"description": "An LRU cache of weak references", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -61,3 +61,3 @@ [![npm version](https://img.shields.io/npm/dw/weak-lru-cache)](https://www.npmjs.org/package/weak-lru-cache) | ||
### cacheSize | ||
This indicates the number of entries to allocate in the cache. This defaults to 32,768. | ||
This indicates the number of entries to allocate in the cache. This defaults to 32,768, and the maximum allowed value is 16,777,216. | ||
@@ -64,0 +64,0 @@ ### expirer |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26957
631