Skip to content

Fix v8 Deopt in ModuleGraph due to wrong map  #16829

@TheLarkInn

Description

@TheLarkInn

ModuleGraph.js causes a v8 deoptimization here:

webpack/lib/ModuleGraph.js

Lines 136 to 147 in b7fc4d8

*/
_getModuleGraphModule(module) {
let mgm = this._moduleMap.get(module);
if (mgm === undefined) {
mgm = new ModuleGraphModule();
this._moduleMap.set(module, mgm);
}
return mgm;
}
/**
* @param {Dependency} dependency the dependency

Isolate Log File Snippet

LoadIC,0x6551df9bde,16223580,563,20,1,P,0x01ad7455adf9,_getModuleGraphModule,,
code-deopt,16223649,640,0x1dc8d5a8320,-1,3848,deopt-eager,<C:\\Users\\selarkin\\Code\\webpack\\lib\\ModuleGraph.js:138:18>,wrong map

This is because freeze() method is introducing a new property to the ModuleGraph instance (this._cacheStage) after its already been initialized causing a Map transition and a "wrong map" deoptimization, preventing inline caching.

Below shows the v8 Map of the object changing into Polymorphic caused by freeze() and causing a IC miss:

First Map Shape: Initialization

image

Map Transition: freeze()

image

Initializing this_cacheStage in the constructor will fix this problem:

class ModuleGraph {
	constructor() {
		/** @type {WeakMap<Dependency, ModuleGraphConnection>} */
		this._dependencyMap = new WeakMap();
		/** @type {Map<Module, ModuleGraphModule>} */
		this._moduleMap = new Map();
		/** @type {WeakMap<any, Object>} */
		this._metaMap = new WeakMap();

		/** @type {WeakTupleMap<any[], any>} */
		this._cache = undefined;

		/** @type {Map<Module, WeakTupleMap<any, any>>} */
		this._moduleMemCaches = undefined;

		/** @type {string} */
		this._cacheStage = undefined;
	}
	

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions