Skip to content

Commit

Permalink
Merge pull request #16759 from ryanwilsonperkin/real-content-hash-reg…
Browse files Browse the repository at this point in the history
…ex-perf

Improve performance of hashRegExp lookup
  • Loading branch information
TheLarkInn authored Mar 8, 2023
2 parents c98e9e0 + cfdb1df commit b84efe6
Showing 1 changed file with 44 additions and 14 deletions.
58 changes: 44 additions & 14 deletions lib/optimize/RealContentHashPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,43 @@ class RealContentHashPlugin {
}
}
if (hashToAssets.size === 0) return;
const hashRegExp = new RegExp(
Array.from(hashToAssets.keys(), quoteMeta).join("|"),
"g"
const hashRegExps = Array.from(hashToAssets.keys(), quoteMeta).map(
hash => new RegExp(hash, "g")
);

/**
* @param {string} str string to be matched against all hashRegExps
* @returns {string[] | null} matches found
*/
const hashMatch = str => {
/** @type {string[]} */
const results = [];
for (const hashRegExp of hashRegExps) {
const matches = str.match(hashRegExp);
if (matches) {
matches.forEach(match => results.push(match));
}
}
if (results.length) {
return results;
} else {
return null;
}
};

/**
* @param {string} str string to be replaced with all hashRegExps
* @param {function(string): string} fn replacement function to use when a hash is found
* @returns {string} replaced content
*/
const hashReplace = (str, fn) => {
let result = str;
for (const hashRegExp of hashRegExps) {
result = result.replace(hashRegExp, fn);
}
return result;
};

await Promise.all(
assetsWithInfo.map(async asset => {
const { name, source, content, hashes } = asset;
Expand All @@ -198,7 +231,7 @@ class RealContentHashPlugin {
await cacheAnalyse.providePromise(name, etag, () => {
const referencedHashes = new Set();
let ownHashes = new Set();
const inContent = content.match(hashRegExp);
const inContent = hashMatch(content);
if (inContent) {
for (const hash of inContent) {
if (hashes.has(hash)) {
Expand Down Expand Up @@ -298,7 +331,7 @@ ${referencingAssets
identifier,
etag,
() => {
const newContent = asset.content.replace(hashRegExp, hash =>
const newContent = hashReplace(asset.content, hash =>
hashToNewHash.get(hash)
);
return new RawSource(newContent);
Expand All @@ -323,15 +356,12 @@ ${referencingAssets
identifier,
etag,
() => {
const newContent = asset.content.replace(
hashRegExp,
hash => {
if (asset.ownHashes.has(hash)) {
return "";
}
return hashToNewHash.get(hash);
const newContent = hashReplace(asset.content, hash => {
if (asset.ownHashes.has(hash)) {
return "";
}
);
return hashToNewHash.get(hash);
});
return new RawSource(newContent);
}
);
Expand Down Expand Up @@ -374,7 +404,7 @@ ${referencingAssets
await Promise.all(
assetsWithInfo.map(async asset => {
await computeNewContent(asset);
const newName = asset.name.replace(hashRegExp, hash =>
const newName = hashReplace(asset.name, hash =>
hashToNewHash.get(hash)
);

Expand Down

0 comments on commit b84efe6

Please sign in to comment.