RemoveParentModulesPlugin.js 6.4 KB

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