RemoveParentModulesPlugin.js 6.3 KB

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