Skip to content

Commit

Permalink
dispose code generation results after sealing the compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Apr 8, 2021
1 parent 51bd62d commit cef2a8f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
16 changes: 16 additions & 0 deletions lib/CodeGenerationResults.js
Expand Up @@ -27,6 +27,7 @@ class CodeGenerationResults {
* @returns {CodeGenerationResult} the CodeGenerationResult
*/
get(module, runtime) {
this._verifyAlive();
const entry = this.map.get(module);
if (entry === undefined) {
throw new Error(
Expand Down Expand Up @@ -72,6 +73,7 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
* @returns {boolean} true, when we have data for this
*/
has(module, runtime) {
this._verifyAlive();
const entry = this.map.get(module);
if (entry === undefined) {
return false;
Expand Down Expand Up @@ -142,9 +144,23 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
* @returns {void}
*/
add(module, runtime, result) {
this._verifyAlive();
const map = provide(this.map, module, () => new RuntimeSpecMap());
map.set(runtime, result);
}

_verifyAlive() {
if (this.map === undefined) {
throw new Error(
"CodeGenerationResults has been accessed after its lifetime. They are no longer available after sealing the compilation."
);
}
}

dispose() {
this.map.clear();
this.map = undefined;
}
}

module.exports = CodeGenerationResults;
2 changes: 2 additions & 0 deletions lib/Compilation.js
Expand Up @@ -2132,6 +2132,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
this.assets = {};
this.assetsInfo.clear();
this.moduleGraph.removeAllModuleAttributes();
this.codeGenerationResults = undefined;
}

/**
Expand Down Expand Up @@ -2418,6 +2419,7 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
this.unseal();
return this.seal(callback);
}
this.codeGenerationResults.dispose();
return this.hooks.afterSeal.callAsync(err => {
if (err) {
return callback(
Expand Down
5 changes: 3 additions & 2 deletions lib/Compiler.js
Expand Up @@ -359,12 +359,13 @@ class Compiler {
// e.g. move compilation specific info from Modules into ModuleGraph
_cleanupLastCompilation() {
if (this._lastCompilation !== undefined) {
for (const module of this._lastCompilation.modules) {
const c = this._lastCompilation;
for (const module of c.modules) {
ChunkGraph.clearChunkGraphForModule(module);
ModuleGraph.clearModuleGraphForModule(module);
module.cleanupForCache();
}
for (const chunk of this._lastCompilation.chunks) {
for (const chunk of c.chunks) {
ChunkGraph.clearChunkGraphForChunk(chunk);
}
this._lastCompilation = undefined;
Expand Down
1 change: 1 addition & 0 deletions types.d.ts
Expand Up @@ -1174,6 +1174,7 @@ declare abstract class CodeGenerationResults {
getData(module: Module, runtime: RuntimeSpec, key: string): any;
getHash(module: Module, runtime: RuntimeSpec): any;
add(module: Module, runtime: RuntimeSpec, result: CodeGenerationResult): void;
dispose(): void;
}
type CodeValue =
| undefined
Expand Down

0 comments on commit cef2a8f

Please sign in to comment.