mirror of https://github.com/webpack/webpack.git
				
				
				
			Merge pull request #5312 from webpack/performance/aggressive-splitting
performance improvements and comments
This commit is contained in:
		
						commit
						2b0943f618
					
				|  | @ -39,6 +39,13 @@ class AggressiveSplittingPlugin { | ||||||
| 	apply(compiler) { | 	apply(compiler) { | ||||||
| 		compiler.plugin("this-compilation", (compilation) => { | 		compiler.plugin("this-compilation", (compilation) => { | ||||||
| 			compilation.plugin("optimize-chunks-advanced", (chunks) => { | 			compilation.plugin("optimize-chunks-advanced", (chunks) => { | ||||||
|  | 				// Precompute stuff
 | ||||||
|  | 				const nameToModuleMap = new Map(); | ||||||
|  | 				compilation.modules.forEach(m => { | ||||||
|  | 					const name = identifierUtils.makePathsRelative(compiler.context, m.identifier(), compilation.cache); | ||||||
|  | 					nameToModuleMap.set(name, m); | ||||||
|  | 				}); | ||||||
|  | 
 | ||||||
| 				const savedSplits = compilation.records && compilation.records.aggressiveSplits || []; | 				const savedSplits = compilation.records && compilation.records.aggressiveSplits || []; | ||||||
| 				const usedSplits = compilation._aggressiveSplittingSplits ? | 				const usedSplits = compilation._aggressiveSplittingSplits ? | ||||||
| 					savedSplits.concat(compilation._aggressiveSplittingSplits) : savedSplits; | 					savedSplits.concat(compilation._aggressiveSplittingSplits) : savedSplits; | ||||||
|  | @ -48,45 +55,51 @@ class AggressiveSplittingPlugin { | ||||||
| 				// 1. try to restore to recorded splitting
 | 				// 1. try to restore to recorded splitting
 | ||||||
| 				for(let j = 0; j < usedSplits.length; j++) { | 				for(let j = 0; j < usedSplits.length; j++) { | ||||||
| 					const splitData = usedSplits[j]; | 					const splitData = usedSplits[j]; | ||||||
| 					for(let i = 0; i < chunks.length; i++) { | 					const selectedModules = splitData.modules.map(name => nameToModuleMap.get(name)); | ||||||
| 						const chunk = chunks[i]; |  | ||||||
| 
 | 
 | ||||||
| 						if(chunk.getNumberOfModules() < splitData.modules.length) | 					// Does the modules exist at all?
 | ||||||
| 							continue; | 					if(selectedModules.every(Boolean)) { | ||||||
| 
 | 
 | ||||||
| 						const nameToModuleMap = new Map(); | 						// Find all chunks containing all modules in the split
 | ||||||
| 						chunk.forEachModule(m => { | 						for(let i = 0; i < chunks.length; i++) { | ||||||
| 							const name = identifierUtils.makePathsRelative(compiler.context, m.identifier(), compilation.cache); | 							const chunk = chunks[i]; | ||||||
| 							nameToModuleMap.set(name, m); |  | ||||||
| 						}); |  | ||||||
| 
 | 
 | ||||||
| 						const selectedModules = splitData.modules.map(name => nameToModuleMap.get(name)); | 							// Cheap check if chunk is suitable at all
 | ||||||
| 						if(selectedModules.every(Boolean)) { | 							if(chunk.getNumberOfModules() < splitData.modules.length) | ||||||
| 							if(chunk.getNumberOfModules() > splitData.modules.length) { | 								continue; | ||||||
| 								const newChunk = compilation.addChunk(); | 
 | ||||||
| 								selectedModules.forEach(moveModuleBetween(chunk, newChunk)); | 							// Check if all modules are in the chunk
 | ||||||
| 								chunk.split(newChunk); | 							if(selectedModules.every(m => chunk.containsModule(m))) { | ||||||
| 								chunk.name = null; | 
 | ||||||
| 								newChunk._fromAggressiveSplitting = true; | 								// Is chunk identical to the split or do we need to split it?
 | ||||||
| 								if(j < savedSplits.length) | 								if(chunk.getNumberOfModules() > splitData.modules.length) { | ||||||
| 									newChunk._fromAggressiveSplittingIndex = j; | 									// split the chunk into two parts
 | ||||||
| 								if(splitData.id !== null && splitData.id !== undefined) { | 									const newChunk = compilation.addChunk(); | ||||||
| 									newChunk.id = splitData.id; | 									selectedModules.forEach(moveModuleBetween(chunk, newChunk)); | ||||||
| 								} | 									chunk.split(newChunk); | ||||||
| 								newChunk.origins = chunk.origins.map(copyWithReason); | 									chunk.name = null; | ||||||
| 								chunk.origins = chunk.origins.map(copyWithReason); | 									newChunk._fromAggressiveSplitting = true; | ||||||
| 								return true; | 									if(j < savedSplits.length) | ||||||
| 							} else { | 										newChunk._fromAggressiveSplittingIndex = j; | ||||||
| 								if(j < savedSplits.length) | 									if(splitData.id !== null && splitData.id !== undefined) { | ||||||
| 									chunk._fromAggressiveSplittingIndex = j; | 										newChunk.id = splitData.id; | ||||||
| 								chunk.name = null; | 									} | ||||||
| 								if(splitData.id !== null && splitData.id !== undefined) { | 									newChunk.origins = chunk.origins.map(copyWithReason); | ||||||
| 									chunk.id = splitData.id; | 									chunk.origins = chunk.origins.map(copyWithReason); | ||||||
|  | 									return true; | ||||||
|  | 								} else { // chunk is identical to the split
 | ||||||
|  | 									if(j < savedSplits.length) | ||||||
|  | 										chunk._fromAggressiveSplittingIndex = j; | ||||||
|  | 									chunk.name = null; | ||||||
|  | 									if(splitData.id !== null && splitData.id !== undefined) { | ||||||
|  | 										chunk.id = splitData.id; | ||||||
|  | 									} | ||||||
| 								} | 								} | ||||||
| 							} | 							} | ||||||
| 						} | 						} | ||||||
| 					} | 					} | ||||||
| 				} | 				} | ||||||
|  | 
 | ||||||
| 				// 2. for any other chunk which isn't splitted yet, split it
 | 				// 2. for any other chunk which isn't splitted yet, split it
 | ||||||
| 				for(let i = 0; i < chunks.length; i++) { | 				for(let i = 0; i < chunks.length; i++) { | ||||||
| 					const chunk = chunks[i]; | 					const chunk = chunks[i]; | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue