-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix: use lru-cache for packuments #7463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,77 @@ | ||
| const { LRUCache } = require('lru-cache') | ||
| const { getHeapStatistics } = require('node:v8') | ||
| const { log } = require('proc-log') | ||
|
|
||
| // This is an in-memory cache that Pacote uses for packuments. | ||
| // Packuments are usually cached on disk. This allows for rapid re-requests | ||
| // of the same packument to bypass disk reads. The tradeoff here is memory | ||
| // usage for disk reads. | ||
| class PackumentCache extends LRUCache { | ||
| static #heapLimit = Math.floor(getHeapStatistics().heap_size_limit) | ||
|
|
||
| #sizeKey | ||
| #disposed = new Set() | ||
|
|
||
| #log (...args) { | ||
| log.silly('packumentCache', ...args) | ||
| } | ||
|
|
||
| constructor ({ | ||
| // How much of this.#heapLimit to take up | ||
| heapFactor = 0.25, | ||
| // How much of this.#maxSize we allow any one packument to take up | ||
| // Anything over this is not cached | ||
| maxEntryFactor = 0.5, | ||
| sizeKey = '_contentLength', | ||
| } = {}) { | ||
| const maxSize = Math.floor(PackumentCache.#heapLimit * heapFactor) | ||
| const maxEntrySize = Math.floor(maxSize * maxEntryFactor) | ||
| super({ | ||
| maxSize, | ||
| maxEntrySize, | ||
| sizeCalculation: (p) => { | ||
| // Don't cache if we dont know the size | ||
| // Some versions of pacote set this to `0`, newer versions set it to `null` | ||
| if (!p[sizeKey]) { | ||
| return maxEntrySize + 1 | ||
| } | ||
| if (p[sizeKey] < 10_000) { | ||
| return p[sizeKey] * 2 | ||
| } | ||
| if (p[sizeKey] < 1_000_000) { | ||
| return Math.floor(p[sizeKey] * 1.5) | ||
| } | ||
| // It is less beneficial to store a small amount of super large things | ||
| // at the cost of all other packuments. | ||
| return 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.