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
mknichel/dedupe strings in module layers #18340
Open
mknichel
wants to merge
6
commits into
webpack:main
Choose a base branch
from
mknichel:mknichel/dedupe-strings-in-module-layers
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bdf23f9
Add a weak cache for possible duplicate strings across multiple modul…
mknichel a35031b
[Memory] Dedupe strings across modules in multiple layers
mknichel 43fcdb9
Revert missed change
mknichel 6d26874
Simplify the module layer cache and reuse more logic
mknichel 4530148
Use module.resource
mknichel 63cdf15
Rename method
mknichel File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
Author @mknichel | ||
*/ | ||
|
||
"use strict"; | ||
|
||
/** @type {WeakMap<Object, Map<string, string | Buffer>>} */ | ||
const weaklyHeldCaches = new WeakMap(); | ||
|
||
/** | ||
* Fetches a cached value if it exists or adds it to the cache and returns the | ||
* value. This is a weak cache, meaning that it will be collected as soon as | ||
* `associatedObjectForCache` is no longer reachable. This cache is useful | ||
* for storing information that could be shared by modules across different | ||
* layers. This can be useful for memory management since the same file that | ||
* appears in multiple layers may duplicate large strings, such as the contents | ||
* of the source file. | ||
* | ||
* @param {Object} associatedObjectForCache The object to associate the cached data with so that the data is collected when this object is no longer reachable. | ||
* @param {string} key The cache key for the value. | ||
* @param {string | Buffer} value The target value. This is used to see if the cached value is exactly the same or to add the value to the cache. | ||
* @returns {string | Buffer} The cached value if it exists and matches or the original value. | ||
*/ | ||
function maybeUseOrSetCachedValue(associatedObjectForCache, key, value) { | ||
if (typeof associatedObjectForCache !== "object") { | ||
throw new Error("`associatedObjectForCache` must be an object"); | ||
} | ||
if (!weaklyHeldCaches.has(associatedObjectForCache)) { | ||
weaklyHeldCaches.set(associatedObjectForCache, new Map()); | ||
} | ||
const map = weaklyHeldCaches.get(associatedObjectForCache); | ||
if (map.has(key)) { | ||
const cachedValue = map.get(key); | ||
if ( | ||
(Buffer.isBuffer(value) && | ||
Buffer.isBuffer(cachedValue) && | ||
value.equals(cachedValue)) || | ||
value === cachedValue | ||
) { | ||
return cachedValue; | ||
} | ||
} else { | ||
map.set(key, value); | ||
} | ||
return value; | ||
} | ||
module.exports = maybeUseOrSetCachedValue; |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move it to method, to avoid duplicate logic (for example to the compilation class)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Sorry for the delay. Let me know if this is what you had in mind.