MemoryWithGcCachePlugin.js 4.0 KB

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