MemoryWithGcCachePlugin.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Cache = require("../Cache");
  7. /** @typedef {import("webpack-sources").Source} Source */
  8. /** @typedef {import("../Cache").Etag} Etag */
  9. /** @typedef {import("../Compiler")} Compiler */
  10. /** @typedef {import("../Module")} Module */
  11. class MemoryWithGcCachePlugin {
  12. constructor({ maxGenerations }) {
  13. this._maxGenerations = maxGenerations;
  14. }
  15. /**
  16. * Apply the plugin
  17. * @param {Compiler} compiler the compiler instance
  18. * @returns {void}
  19. */
  20. apply(compiler) {
  21. const maxGenerations = this._maxGenerations;
  22. /** @type {Map<string, { etag: Etag | null, data: any }>} */
  23. const cache = new Map();
  24. /** @type {Map<string, { entry: { etag: Etag | null, data: any }, until: number }>} */
  25. const oldCache = new Map();
  26. let generation = 0;
  27. let cachePosition = 0;
  28. const logger = compiler.getInfrastructureLogger("MemoryWithGcCachePlugin");
  29. compiler.hooks.afterDone.tap("MemoryWithGcCachePlugin", () => {
  30. generation++;
  31. let clearedEntries = 0;
  32. let lastClearedIdentifier;
  33. for (const [identifier, entry] of oldCache) {
  34. if (entry.until > generation) break;
  35. oldCache.delete(identifier);
  36. if (cache.get(identifier) === undefined) {
  37. cache.delete(identifier);
  38. clearedEntries++;
  39. lastClearedIdentifier = identifier;
  40. }
  41. }
  42. if (clearedEntries > 0 || oldCache.size > 0) {
  43. logger.log(
  44. `${cache.size - oldCache.size} active entries, ${
  45. oldCache.size
  46. } recently unused cached entries${
  47. clearedEntries > 0
  48. ? `, ${clearedEntries} old unused cache entries removed e. g. ${lastClearedIdentifier}`
  49. : ""
  50. }`
  51. );
  52. }
  53. let i = (cache.size / maxGenerations) | 0;
  54. let j = cachePosition >= cache.size ? 0 : cachePosition;
  55. cachePosition = j + i;
  56. for (const [identifier, entry] of cache) {
  57. if (j !== 0) {
  58. j--;
  59. continue;
  60. }
  61. if (entry !== undefined) {
  62. // We don't delete the cache entry, but set it to undefined instead
  63. // This reserves the location in the data table and avoids rehashing
  64. // when constantly adding and removing entries.
  65. // It will be deleted when removed from oldCache.
  66. cache.set(identifier, undefined);
  67. oldCache.delete(identifier);
  68. oldCache.set(identifier, {
  69. entry,
  70. until: generation + maxGenerations
  71. });
  72. if (i-- === 0) break;
  73. }
  74. }
  75. });
  76. compiler.cache.hooks.store.tap(
  77. { name: "MemoryWithGcCachePlugin", stage: Cache.STAGE_MEMORY },
  78. (identifier, etag, data) => {
  79. cache.set(identifier, { etag, data });
  80. }
  81. );
  82. compiler.cache.hooks.get.tap(
  83. { name: "MemoryWithGcCachePlugin", stage: Cache.STAGE_MEMORY },
  84. (identifier, etag, gotHandlers) => {
  85. const cacheEntry = cache.get(identifier);
  86. if (cacheEntry === null) {
  87. return null;
  88. } else if (cacheEntry !== undefined) {
  89. return cacheEntry.etag === etag ? cacheEntry.data : null;
  90. }
  91. const oldCacheEntry = oldCache.get(identifier);
  92. if (oldCacheEntry !== undefined) {
  93. const cacheEntry = oldCacheEntry.entry;
  94. if (cacheEntry === null) {
  95. oldCache.delete(identifier);
  96. cache.set(identifier, cacheEntry);
  97. return null;
  98. } else {
  99. if (cacheEntry.etag !== etag) return null;
  100. oldCache.delete(identifier);
  101. cache.set(identifier, cacheEntry);
  102. return cacheEntry.data;
  103. }
  104. }
  105. gotHandlers.push((result, callback) => {
  106. if (result === undefined) {
  107. cache.set(identifier, null);
  108. } else {
  109. cache.set(identifier, { etag, data: result });
  110. }
  111. return callback();
  112. });
  113. }
  114. );
  115. compiler.cache.hooks.shutdown.tap(
  116. { name: "MemoryWithGcCachePlugin", stage: Cache.STAGE_MEMORY },
  117. () => {
  118. cache.clear();
  119. oldCache.clear();
  120. }
  121. );
  122. }
  123. }
  124. module.exports = MemoryWithGcCachePlugin;