-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: limit packument cache size based on heap size
- Loading branch information
Showing
10 changed files
with
92 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const { LRUCache } = require('lru-cache') | ||
const { getHeapStatistics } = require('node:v8') | ||
const { log } = require('proc-log') | ||
|
||
class PackumentCache extends LRUCache { | ||
static #heapLimit = Math.floor(getHeapStatistics().heap_size_limit) | ||
|
||
#sizeKey | ||
#disposed = new Set() | ||
|
||
#log (...args) { | ||
log.silly('packumentCache', ...args) | ||
} | ||
|
||
constructor ({ | ||
heapFactor = 0.25, | ||
maxEntryFactor = 0.5, | ||
sizeKey = '_contentLength', | ||
} = {}) { | ||
const maxSize = Math.floor(PackumentCache.#heapLimit * heapFactor) | ||
const maxEntrySize = Math.floor(maxSize * maxEntryFactor) | ||
super({ | ||
maxSize, | ||
maxEntrySize, | ||
// Don't cache if we dont know the size | ||
sizeCalculation: (p) => p[sizeKey] || maxEntrySize + 1, | ||
dispose: (v, k) => { | ||
this.#disposed.add(k) | ||
this.#log(k, 'dispose') | ||
}, | ||
}) | ||
this.#sizeKey = sizeKey | ||
this.#log(`heap:${PackumentCache.#heapLimit} maxSize:${maxSize} maxEntrySize:${maxEntrySize}`) | ||
} | ||
|
||
set (k, v, ...args) { | ||
// we use disposed only for a logging signal if we are setting packuments that | ||
// have already been evicted from the cache previously. logging here could help | ||
// us tune this in the future. | ||
const disposed = this.#disposed.has(k) | ||
/* istanbul ignore next - this doesnt happen consistently so hard to test without resorting to unit tests */ | ||
if (disposed) { | ||
this.#disposed.delete(k) | ||
} | ||
this.#log(k, 'set', `size:${v[this.#sizeKey]} disposed:${disposed}`) | ||
return super.set(k, v, ...args) | ||
} | ||
|
||
has (k, ...args) { | ||
const has = super.has(k, ...args) | ||
this.#log(k, `cache-${has ? 'hit' : 'miss'}`) | ||
return has | ||
} | ||
} | ||
|
||
module.exports = PackumentCache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,7 +197,7 @@ t.test('verify dep flags in script environments', async t => { | |
const file = resolve(path, 'node_modules', pkg, 'env') | ||
t.strictSame(flags, fs.readFileSync(file, 'utf8').split('\n'), pkg) | ||
} | ||
t.strictSame(checkLogs().sort((a, b) => | ||
t.strictSame(checkLogs().filter(l => l[0] === 'info' && l[1] === 'run').sort((a, b) => | ||
localeCompare(a[2], b[2]) || (typeof a[4] === 'string' ? -1 : 1)), [ | ||
['info', 'run', '[email protected]', 'postinstall', 'node_modules/devdep', 'node ../../env.js'], | ||
['info', 'run', '[email protected]', 'postinstall', { code: 0, signal: null }], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters