RemoveParentModulesPlugin.js 6.4 KB

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