RemoveParentModulesPlugin.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. * Applies the plugin by registering its hooks on the compiler.
  58. * @param {Compiler} compiler the compiler
  59. * @returns {void}
  60. */
  61. apply(compiler) {
  62. compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
  63. /**
  64. * Handles the hook callback for this code path.
  65. * @param {Iterable<Chunk>} chunks the chunks
  66. * @param {ChunkGroup[]} chunkGroups the chunk groups
  67. */
  68. const handler = (chunks, chunkGroups) => {
  69. const chunkGraph = compilation.chunkGraph;
  70. /** @type {Set<ChunkGroup>} */
  71. const queue = new Set();
  72. /** @type {WeakMap<ChunkGroup, bigint | undefined>} */
  73. const availableModulesMap = new WeakMap();
  74. let nextModuleMask = ONE_BIGINT;
  75. /** @type {WeakMap<Module, bigint>} */
  76. const maskByModule = new WeakMap();
  77. /** @type {Module[]} */
  78. const ordinalModules = [];
  79. /**
  80. * Gets or create module mask.
  81. * @param {Module} mod the module to get the mask for
  82. * @returns {bigint} the module mask to uniquely identify the module
  83. */
  84. const getOrCreateModuleMask = (mod) => {
  85. let id = maskByModule.get(mod);
  86. if (id === undefined) {
  87. id = nextModuleMask;
  88. ordinalModules.push(mod);
  89. maskByModule.set(mod, id);
  90. nextModuleMask <<= ONE_BIGINT;
  91. }
  92. return id;
  93. };
  94. // Initialize masks by chunk and by chunk group for quicker comparisons
  95. /** @type {WeakMap<Chunk, bigint>} */
  96. const chunkMasks = new WeakMap();
  97. for (const chunk of chunks) {
  98. let mask = ZERO_BIGINT;
  99. for (const m of chunkGraph.getChunkModulesIterable(chunk)) {
  100. const id = getOrCreateModuleMask(m);
  101. mask |= id;
  102. }
  103. chunkMasks.set(chunk, mask);
  104. }
  105. /** @type {WeakMap<ChunkGroup, bigint>} */
  106. const chunkGroupMasks = new WeakMap();
  107. for (const chunkGroup of chunkGroups) {
  108. let mask = ZERO_BIGINT;
  109. for (const chunk of chunkGroup.chunks) {
  110. const chunkMask = chunkMasks.get(chunk);
  111. if (chunkMask !== undefined) {
  112. mask |= chunkMask;
  113. }
  114. }
  115. chunkGroupMasks.set(chunkGroup, mask);
  116. }
  117. for (const chunkGroup of compilation.entrypoints.values()) {
  118. // initialize available modules for chunks without parents
  119. availableModulesMap.set(chunkGroup, ZERO_BIGINT);
  120. for (const child of chunkGroup.childrenIterable) {
  121. queue.add(child);
  122. }
  123. }
  124. for (const chunkGroup of compilation.asyncEntrypoints) {
  125. // initialize available modules for chunks without parents
  126. availableModulesMap.set(chunkGroup, ZERO_BIGINT);
  127. for (const child of chunkGroup.childrenIterable) {
  128. queue.add(child);
  129. }
  130. }
  131. for (const chunkGroup of queue) {
  132. let availableModulesMask = availableModulesMap.get(chunkGroup);
  133. let changed = false;
  134. for (const parent of chunkGroup.parentsIterable) {
  135. const availableModulesInParent = availableModulesMap.get(parent);
  136. if (availableModulesInParent !== undefined) {
  137. const parentMask =
  138. availableModulesInParent |
  139. /** @type {bigint} */ (chunkGroupMasks.get(parent));
  140. // If we know the available modules in parent: process these
  141. if (availableModulesMask === undefined) {
  142. // if we have not own info yet: create new entry
  143. availableModulesMask = parentMask;
  144. changed = true;
  145. } else {
  146. const newMask = availableModulesMask & parentMask;
  147. if (newMask !== availableModulesMask) {
  148. changed = true;
  149. availableModulesMask = newMask;
  150. }
  151. }
  152. }
  153. }
  154. if (changed) {
  155. availableModulesMap.set(chunkGroup, availableModulesMask);
  156. // if something changed: enqueue our children
  157. for (const child of chunkGroup.childrenIterable) {
  158. // Push the child to the end of the queue
  159. queue.delete(child);
  160. queue.add(child);
  161. }
  162. }
  163. }
  164. // now we have available modules for every chunk
  165. for (const chunk of chunks) {
  166. const chunkMask = chunkMasks.get(chunk);
  167. if (chunkMask === undefined) continue; // No info about this chunk
  168. const availableModulesSets = Array.from(
  169. chunk.groupsIterable,
  170. (chunkGroup) => availableModulesMap.get(chunkGroup)
  171. );
  172. if (availableModulesSets.includes(undefined)) continue; // No info about this chunk group
  173. const availableModulesMask = intersectMasks(
  174. /** @type {bigint[]} */
  175. (availableModulesSets)
  176. );
  177. const toRemoveMask = chunkMask & availableModulesMask;
  178. if (toRemoveMask !== ZERO_BIGINT) {
  179. for (const module of getModulesFromMask(
  180. toRemoveMask,
  181. ordinalModules
  182. )) {
  183. chunkGraph.disconnectChunkAndModule(chunk, module);
  184. }
  185. }
  186. }
  187. };
  188. compilation.hooks.optimizeChunks.tap(
  189. {
  190. name: PLUGIN_NAME,
  191. stage: STAGE_BASIC
  192. },
  193. handler
  194. );
  195. });
  196. }
  197. }
  198. module.exports = RemoveParentModulesPlugin;