Skip to content

Commit 00f77fd

Browse files
authored
Merge pull request webpack#7165 from webpack/performance/cache-hash
improve NormalModule performance
2 parents 6b0dd00 + ff950e7 commit 00f77fd

File tree

3 files changed

+135
-164
lines changed

3 files changed

+135
-164
lines changed

lib/NormalModule.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const ModuleParseError = require("./ModuleParseError");
2222
const ModuleBuildError = require("./ModuleBuildError");
2323
const ModuleError = require("./ModuleError");
2424
const ModuleWarning = require("./ModuleWarning");
25+
const createHash = require("./util/createHash");
2526

2627
const asString = buf => {
2728
if (Buffer.isBuffer(buf)) {
@@ -89,6 +90,7 @@ class NormalModule extends Module {
8990
// Info from Build
9091
this.error = null;
9192
this._source = null;
93+
this._buildHash = "";
9294
this.buildTimestamp = undefined;
9395
this._cachedSource = undefined;
9496
this._cachedSourceHash = undefined;
@@ -327,11 +329,23 @@ class NormalModule extends Module {
327329
return false;
328330
}
329331

332+
_initBuildHash(compilation) {
333+
const hash = createHash(compilation.outputOptions.hashFunction);
334+
if (this._source) {
335+
hash.update("source");
336+
this._source.updateHash(hash);
337+
}
338+
hash.update("meta");
339+
hash.update(JSON.stringify(this.buildMeta));
340+
this._buildHash = hash.digest("hex");
341+
}
342+
330343
build(options, compilation, resolver, fs, callback) {
331344
this.buildTimestamp = Date.now();
332345
this.built = true;
333346
this._source = null;
334347
this._ast = null;
348+
this._buildHash = "";
335349
this.error = null;
336350
this.errors.length = 0;
337351
this.warnings.length = 0;
@@ -349,25 +363,29 @@ class NormalModule extends Module {
349363
// if we have an error mark module as failed and exit
350364
if (err) {
351365
this.markModuleAsErrored(err);
366+
this._initBuildHash(compilation);
352367
return callback();
353368
}
354369

355370
// check if this module should !not! be parsed.
356371
// if so, exit here;
357372
const noParseRule = options.module && options.module.noParse;
358373
if (this.shouldPreventParsing(noParseRule, this.request)) {
374+
this._initBuildHash(compilation);
359375
return callback();
360376
}
361377

362378
const handleParseError = e => {
363379
const source = this._source.source();
364380
const error = new ModuleParseError(this, source, e);
365381
this.markModuleAsErrored(error);
382+
this._initBuildHash(compilation);
366383
return callback();
367384
};
368385

369386
const handleParseResult = result => {
370387
this._lastSuccessfulBuildMeta = this.buildMeta;
388+
this._initBuildHash(compilation);
371389
return callback();
372390
};
373391

@@ -454,23 +472,8 @@ class NormalModule extends Module {
454472
return this._source ? this._source.size() : -1;
455473
}
456474

457-
updateHashWithSource(hash) {
458-
if (!this._source) {
459-
hash.update("null");
460-
return;
461-
}
462-
hash.update("source");
463-
this._source.updateHash(hash);
464-
}
465-
466-
updateHashWithMeta(hash) {
467-
hash.update("meta");
468-
hash.update(JSON.stringify(this.buildMeta));
469-
}
470-
471475
updateHash(hash) {
472-
this.updateHashWithSource(hash);
473-
this.updateHashWithMeta(hash);
476+
hash.update(this._buildHash);
474477
super.updateHash(hash);
475478
}
476479
}

test/NormalModule.unittest.js

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -184,38 +184,6 @@ describe("NormalModule", () => {
184184
});
185185
});
186186

187-
describe("#updateHashWithSource", () => {
188-
let hashSpy;
189-
let hash;
190-
beforeEach(() => {
191-
hashSpy = sinon.spy();
192-
hash = {
193-
update: hashSpy
194-
};
195-
});
196-
describe("without the module having any source", () => {
197-
beforeEach(() => {
198-
normalModule._source = null;
199-
});
200-
it('calls hash function with "null"', () => {
201-
normalModule.updateHashWithSource(hash);
202-
expect(hashSpy.callCount).toBe(1);
203-
expect(hashSpy.args[0][0]).toBe("null");
204-
});
205-
});
206-
describe("without the module having source", () => {
207-
let expectedSource = "some source";
208-
beforeEach(() => {
209-
normalModule._source = new RawSource(expectedSource);
210-
});
211-
it('calls hash function with "source" and then the actual source of the module', function() {
212-
normalModule.updateHashWithSource(hash);
213-
expect(hashSpy.callCount).toBe(2);
214-
expect(hashSpy.args[0][0]).toBe("source");
215-
expect(hashSpy.args[1][0]).toBe(expectedSource);
216-
});
217-
});
218-
});
219187
describe("#hasDependencies", () => {
220188
it("returns true if has dependencies", () => {
221189
normalModule.addDependency(new NullDependency());

0 commit comments

Comments
 (0)