MemoryWithGcCachePlugin.js 4.2 KB

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