LimitChunkCountPlugin.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { STAGE_ADVANCED } = require("../OptimizationStages");
  7. const LazyBucketSortedSet = require("../util/LazyBucketSortedSet");
  8. const { compareChunks } = require("../util/comparators");
  9. const createSchemaValidation = require("../util/create-schema-validation");
  10. /** @typedef {import("../../declarations/plugins/optimize/LimitChunkCountPlugin").LimitChunkCountPluginOptions} LimitChunkCountPluginOptions */
  11. /** @typedef {import("../Chunk")} Chunk */
  12. /** @typedef {import("../Compiler")} Compiler */
  13. const validate = createSchemaValidation(
  14. require("../../schemas/plugins/optimize/LimitChunkCountPlugin.check"),
  15. () => require("../../schemas/plugins/optimize/LimitChunkCountPlugin.json"),
  16. {
  17. name: "Limit Chunk Count Plugin",
  18. baseDataPath: "options"
  19. }
  20. );
  21. /**
  22. * @typedef {object} ChunkCombination
  23. * @property {boolean} deleted this is set to true when combination was removed
  24. * @property {number} sizeDiff
  25. * @property {number} integratedSize
  26. * @property {Chunk} a
  27. * @property {Chunk} b
  28. * @property {number} aIdx
  29. * @property {number} bIdx
  30. * @property {number} aSize
  31. * @property {number} bSize
  32. */
  33. /**
  34. * @template K, V
  35. * @param {Map<K, Set<V>>} map map
  36. * @param {K} key key
  37. * @param {V} value value
  38. */
  39. const addToSetMap = (map, key, value) => {
  40. const set = map.get(key);
  41. if (set === undefined) {
  42. map.set(key, new Set([value]));
  43. } else {
  44. set.add(value);
  45. }
  46. };
  47. const PLUGIN_NAME = "LimitChunkCountPlugin";
  48. class LimitChunkCountPlugin {
  49. /**
  50. * @param {LimitChunkCountPluginOptions=} options options object
  51. */
  52. constructor(options) {
  53. validate(options);
  54. this.options = /** @type {LimitChunkCountPluginOptions} */ (options);
  55. }
  56. /**
  57. * @param {Compiler} compiler the webpack compiler
  58. * @returns {void}
  59. */
  60. apply(compiler) {
  61. const options = this.options;
  62. compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
  63. compilation.hooks.optimizeChunks.tap(
  64. {
  65. name: PLUGIN_NAME,
  66. stage: STAGE_ADVANCED
  67. },
  68. chunks => {
  69. const chunkGraph = compilation.chunkGraph;
  70. const maxChunks = options.maxChunks;
  71. if (!maxChunks) return;
  72. if (maxChunks < 1) return;
  73. if (compilation.chunks.size <= maxChunks) return;
  74. let remainingChunksToMerge = compilation.chunks.size - maxChunks;
  75. // order chunks in a deterministic way
  76. const compareChunksWithGraph = compareChunks(chunkGraph);
  77. /** @type {Chunk[]} */
  78. const orderedChunks = [...chunks].sort(compareChunksWithGraph);
  79. // create a lazy sorted data structure to keep all combinations
  80. // this is large. Size = chunks * (chunks - 1) / 2
  81. // It uses a multi layer bucket sort plus normal sort in the last layer
  82. // It's also lazy so only accessed buckets are sorted
  83. /** @type {LazyBucketSortedSet<ChunkCombination, number>} */
  84. const combinations = new LazyBucketSortedSet(
  85. // Layer 1: ordered by largest size benefit
  86. c => c.sizeDiff,
  87. (a, b) => b - a,
  88. // Layer 2: ordered by smallest combined size
  89. /**
  90. * @param {ChunkCombination} c combination
  91. * @returns {number} integrated size
  92. */
  93. c => c.integratedSize,
  94. /**
  95. * @param {number} a a
  96. * @param {number} b b
  97. * @returns {number} result
  98. */
  99. (a, b) => a - b,
  100. // Layer 3: ordered by position difference in orderedChunk (-> to be deterministic)
  101. /**
  102. * @param {ChunkCombination} c combination
  103. * @returns {number} position difference
  104. */
  105. c => c.bIdx - c.aIdx,
  106. /**
  107. * @param {number} a a
  108. * @param {number} b b
  109. * @returns {number} result
  110. */
  111. (a, b) => a - b,
  112. // Layer 4: ordered by position in orderedChunk (-> to be deterministic)
  113. /**
  114. * @param {ChunkCombination} a a
  115. * @param {ChunkCombination} b b
  116. * @returns {number} result
  117. */
  118. (a, b) => a.bIdx - b.bIdx
  119. );
  120. // we keep a mapping from chunk to all combinations
  121. // but this mapping is not kept up-to-date with deletions
  122. // so `deleted` flag need to be considered when iterating this
  123. /** @type {Map<Chunk, Set<ChunkCombination>>} */
  124. const combinationsByChunk = new Map();
  125. for (const [bIdx, b] of orderedChunks.entries()) {
  126. // create combination pairs with size and integrated size
  127. for (let aIdx = 0; aIdx < bIdx; aIdx++) {
  128. const a = orderedChunks[aIdx];
  129. // filter pairs that can not be integrated!
  130. if (!chunkGraph.canChunksBeIntegrated(a, b)) continue;
  131. const integratedSize = chunkGraph.getIntegratedChunksSize(
  132. a,
  133. b,
  134. options
  135. );
  136. const aSize = chunkGraph.getChunkSize(a, options);
  137. const bSize = chunkGraph.getChunkSize(b, options);
  138. /** @type {ChunkCombination} */
  139. const c = {
  140. deleted: false,
  141. sizeDiff: aSize + bSize - integratedSize,
  142. integratedSize,
  143. a,
  144. b,
  145. aIdx,
  146. bIdx,
  147. aSize,
  148. bSize
  149. };
  150. combinations.add(c);
  151. addToSetMap(combinationsByChunk, a, c);
  152. addToSetMap(combinationsByChunk, b, c);
  153. }
  154. }
  155. // list of modified chunks during this run
  156. // combinations affected by this change are skipped to allow
  157. // further optimizations
  158. /** @type {Set<Chunk>} */
  159. const modifiedChunks = new Set();
  160. let changed = false;
  161. loop: while (true) {
  162. const combination = combinations.popFirst();
  163. if (combination === undefined) break;
  164. combination.deleted = true;
  165. const { a, b, integratedSize } = combination;
  166. // skip over pair when
  167. // one of the already merged chunks is a parent of one of the chunks
  168. if (modifiedChunks.size > 0) {
  169. const queue = new Set(a.groupsIterable);
  170. for (const group of b.groupsIterable) {
  171. queue.add(group);
  172. }
  173. for (const group of queue) {
  174. for (const mChunk of modifiedChunks) {
  175. if (mChunk !== a && mChunk !== b && mChunk.isInGroup(group)) {
  176. // This is a potential pair which needs recalculation
  177. // We can't do that now, but it merge before following pairs
  178. // so we leave space for it, and consider chunks as modified
  179. // just for the worse case
  180. remainingChunksToMerge--;
  181. if (remainingChunksToMerge <= 0) break loop;
  182. modifiedChunks.add(a);
  183. modifiedChunks.add(b);
  184. continue loop;
  185. }
  186. }
  187. for (const parent of group.parentsIterable) {
  188. queue.add(parent);
  189. }
  190. }
  191. }
  192. // merge the chunks
  193. if (chunkGraph.canChunksBeIntegrated(a, b)) {
  194. chunkGraph.integrateChunks(a, b);
  195. compilation.chunks.delete(b);
  196. // flag chunk a as modified as further optimization are possible for all children here
  197. modifiedChunks.add(a);
  198. changed = true;
  199. remainingChunksToMerge--;
  200. if (remainingChunksToMerge <= 0) break;
  201. // Update all affected combinations
  202. // delete all combination with the removed chunk
  203. // we will use combinations with the kept chunk instead
  204. for (const combination of /** @type {Set<ChunkCombination>} */ (
  205. combinationsByChunk.get(a)
  206. )) {
  207. if (combination.deleted) continue;
  208. combination.deleted = true;
  209. combinations.delete(combination);
  210. }
  211. // Update combinations with the kept chunk with new sizes
  212. for (const combination of /** @type {Set<ChunkCombination>} */ (
  213. combinationsByChunk.get(b)
  214. )) {
  215. if (combination.deleted) continue;
  216. if (combination.a === b) {
  217. if (!chunkGraph.canChunksBeIntegrated(a, combination.b)) {
  218. combination.deleted = true;
  219. combinations.delete(combination);
  220. continue;
  221. }
  222. // Update size
  223. const newIntegratedSize = chunkGraph.getIntegratedChunksSize(
  224. a,
  225. combination.b,
  226. options
  227. );
  228. const finishUpdate = combinations.startUpdate(combination);
  229. combination.a = a;
  230. combination.integratedSize = newIntegratedSize;
  231. combination.aSize = integratedSize;
  232. combination.sizeDiff =
  233. combination.bSize + integratedSize - newIntegratedSize;
  234. finishUpdate();
  235. } else if (combination.b === b) {
  236. if (!chunkGraph.canChunksBeIntegrated(combination.a, a)) {
  237. combination.deleted = true;
  238. combinations.delete(combination);
  239. continue;
  240. }
  241. // Update size
  242. const newIntegratedSize = chunkGraph.getIntegratedChunksSize(
  243. combination.a,
  244. a,
  245. options
  246. );
  247. const finishUpdate = combinations.startUpdate(combination);
  248. combination.b = a;
  249. combination.integratedSize = newIntegratedSize;
  250. combination.bSize = integratedSize;
  251. combination.sizeDiff =
  252. integratedSize + combination.aSize - newIntegratedSize;
  253. finishUpdate();
  254. }
  255. }
  256. combinationsByChunk.set(
  257. a,
  258. /** @type {Set<ChunkCombination>} */ (
  259. combinationsByChunk.get(b)
  260. )
  261. );
  262. combinationsByChunk.delete(b);
  263. }
  264. }
  265. if (changed) return true;
  266. }
  267. );
  268. });
  269. }
  270. }
  271. module.exports = LimitChunkCountPlugin;