RemoveParentModulesPlugin.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { STAGE_BASIC } = require("../OptimizationStages");
  7. /** @typedef {import("../Chunk")} Chunk */
  8. /** @typedef {import("../ChunkGroup")} ChunkGroup */
  9. /** @typedef {import("../Compiler")} Compiler */
  10. /** @typedef {import("../Module")} Module */
  11. /**
  12. * Intersects multiple masks represented as bigints
  13. * @param {bigint[]} masks The module masks to intersect
  14. * @returns {bigint} The intersection of all masks
  15. */
  16. function intersectMasks(masks) {
  17. let result = masks[0];
  18. for (let i = masks.length - 1; i >= 1; i--) {
  19. result &= masks[i];
  20. }
  21. return result;
  22. }
  23. const ZERO_BIGINT = BigInt(0);
  24. const ONE_BIGINT = BigInt(1);
  25. const THIRTY_TWO_BIGINT = BigInt(32);
  26. /**
  27. * Parses the module mask and returns the modules represented by it
  28. * @param {bigint} mask the module mask
  29. * @param {Module[]} ordinalModules the modules in the order they were added to the mask (LSB is index 0)
  30. * @returns {Generator<Module, undefined, undefined>} the modules represented by the mask
  31. */
  32. function* getModulesFromMask(mask, ordinalModules) {
  33. let offset = 31;
  34. while (mask !== ZERO_BIGINT) {
  35. // Consider the last 32 bits, since that's what Math.clz32 can handle
  36. let last32 = Number(BigInt.asUintN(32, mask));
  37. while (last32 > 0) {
  38. const last = Math.clz32(last32);
  39. // The number of trailing zeros is the number trimmed off the input mask + 31 - the number of leading zeros
  40. // The 32 is baked into the initial value of offset
  41. const moduleIndex = offset - last;
  42. // The number of trailing zeros is the index into the array generated by getOrCreateModuleMask
  43. const module = ordinalModules[moduleIndex];
  44. yield module;
  45. // Remove the matched module from the mask
  46. // Since we can only count leading zeros, not trailing, we can't just downshift the mask
  47. last32 &= ~(1 << (31 - last));
  48. }
  49. // Remove the processed chunk from the mask
  50. mask >>= THIRTY_TWO_BIGINT;
  51. offset += 32;
  52. }
  53. }
  54. const PLUGIN_NAME = "RemoveParentModulesPlugin";
  55. class RemoveParentModulesPlugin {
  56. /**
  57. * @param {Compiler} compiler the compiler
  58. * @returns {void}
  59. */
  60. apply(compiler) {
  61. compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
  62. /**
  63. * @param {Iterable<Chunk>} chunks the chunks
  64. * @param {ChunkGroup[]} chunkGroups the chunk groups
  65. */
  66. const handler = (chunks, chunkGroups) => {
  67. const chunkGraph = compilation.chunkGraph;
  68. /** @type {Set<ChunkGroup>} */
  69. const queue = new Set();
  70. /** @type {WeakMap<ChunkGroup, bigint | undefined>} */
  71. const availableModulesMap = new WeakMap();
  72. let nextModuleMask = ONE_BIGINT;
  73. /** @type {WeakMap<Module, bigint>} */
  74. const maskByModule = new WeakMap();
  75. /** @type {Module[]} */
  76. const ordinalModules = [];
  77. /**
  78. * Gets or creates a unique mask for a module
  79. * @param {Module} mod the module to get the mask for
  80. * @returns {bigint} the module mask to uniquely identify the module
  81. */
  82. const getOrCreateModuleMask = (mod) => {
  83. let id = maskByModule.get(mod);
  84. if (id === undefined) {
  85. id = nextModuleMask;
  86. ordinalModules.push(mod);
  87. maskByModule.set(mod, id);
  88. nextModuleMask <<= ONE_BIGINT;
  89. }
  90. return id;
  91. };
  92. // Initialize masks by chunk and by chunk group for quicker comparisons
  93. /** @type {WeakMap<Chunk, bigint>} */
  94. const chunkMasks = new WeakMap();
  95. for (const chunk of chunks) {
  96. let mask = ZERO_BIGINT;
  97. for (const m of chunkGraph.getChunkModulesIterable(chunk)) {
  98. const id = getOrCreateModuleMask(m);
  99. mask |= id;
  100. }
  101. chunkMasks.set(chunk, mask);
  102. }
  103. /** @type {WeakMap<ChunkGroup, bigint>} */
  104. const chunkGroupMasks = new WeakMap();
  105. for (const chunkGroup of chunkGroups) {
  106. let mask = ZERO_BIGINT;
  107. for (const chunk of chunkGroup.chunks) {
  108. const chunkMask = chunkMasks.get(chunk);
  109. if (chunkMask !== undefined) {
  110. mask |= chunkMask;
  111. }
  112. }
  113. chunkGroupMasks.set(chunkGroup, mask);
  114. }
  115. for (const chunkGroup of compilation.entrypoints.values()) {
  116. // initialize available modules for chunks without parents
  117. availableModulesMap.set(chunkGroup, ZERO_BIGINT);
  118. for (const child of chunkGroup.childrenIterable) {
  119. queue.add(child);
  120. }
  121. }
  122. for (const chunkGroup of compilation.asyncEntrypoints) {
  123. // initialize available modules for chunks without parents
  124. availableModulesMap.set(chunkGroup, ZERO_BIGINT);
  125. for (const child of chunkGroup.childrenIterable) {
  126. queue.add(child);
  127. }
  128. }
  129. for (const chunkGroup of queue) {
  130. let availableModulesMask = availableModulesMap.get(chunkGroup);
  131. let changed = false;
  132. for (const parent of chunkGroup.parentsIterable) {
  133. const availableModulesInParent = availableModulesMap.get(parent);
  134. if (availableModulesInParent !== undefined) {
  135. const parentMask =
  136. availableModulesInParent |
  137. /** @type {bigint} */ (chunkGroupMasks.get(parent));
  138. // If we know the available modules in parent: process these
  139. if (availableModulesMask === undefined) {
  140. // if we have not own info yet: create new entry
  141. availableModulesMask = parentMask;
  142. changed = true;
  143. } else {
  144. const newMask = availableModulesMask & parentMask;
  145. if (newMask !== availableModulesMask) {
  146. changed = true;
  147. availableModulesMask = newMask;
  148. }
  149. }
  150. }
  151. }
  152. if (changed) {
  153. availableModulesMap.set(chunkGroup, availableModulesMask);
  154. // if something changed: enqueue our children
  155. for (const child of chunkGroup.childrenIterable) {
  156. // Push the child to the end of the queue
  157. queue.delete(child);
  158. queue.add(child);
  159. }
  160. }
  161. }
  162. // now we have available modules for every chunk
  163. for (const chunk of chunks) {
  164. const chunkMask = chunkMasks.get(chunk);
  165. if (chunkMask === undefined) continue; // No info about this chunk
  166. const availableModulesSets = Array.from(
  167. chunk.groupsIterable,
  168. (chunkGroup) => availableModulesMap.get(chunkGroup)
  169. );
  170. if (availableModulesSets.includes(undefined)) continue; // No info about this chunk group
  171. const availableModulesMask = intersectMasks(
  172. /** @type {bigint[]} */
  173. (availableModulesSets)
  174. );
  175. const toRemoveMask = chunkMask & availableModulesMask;
  176. if (toRemoveMask !== ZERO_BIGINT) {
  177. for (const module of getModulesFromMask(
  178. toRemoveMask,
  179. ordinalModules
  180. )) {
  181. chunkGraph.disconnectChunkAndModule(chunk, module);
  182. }
  183. }
  184. }
  185. };
  186. compilation.hooks.optimizeChunks.tap(
  187. {
  188. name: PLUGIN_NAME,
  189. stage: STAGE_BASIC
  190. },
  191. handler
  192. );
  193. });
  194. }
  195. }
  196. module.exports = RemoveParentModulesPlugin;