2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
8 const Cache = require("../Cache");
10 /** @typedef {import("webpack-sources").Source} Source */
11 /** @typedef {import("../Cache").Etag} Etag */
12 /** @typedef {import("../Compiler")} Compiler */
13 /** @typedef {import("../Module")} Module */
15 class MemoryWithGcCachePlugin {
16 constructor({ maxGenerations }) {
17 this._maxGenerations = maxGenerations;
21 * @param {Compiler} compiler the compiler instance
25 const maxGenerations = this._maxGenerations;
26 /** @type {Map<string, { etag: Etag | null, data: any }>} */
27 const cache = new Map();
28 /** @type {Map<string, { entry: { etag: Etag | null, data: any }, until: number }>} */
29 const oldCache = new Map();
31 let cachePosition = 0;
32 const logger = compiler.getInfrastructureLogger("MemoryWithGcCachePlugin");
33 compiler.hooks.afterDone.tap("MemoryWithGcCachePlugin", () => {
35 let clearedEntries = 0;
36 let lastClearedIdentifier;
37 for (const [identifier, entry] of oldCache) {
38 if (entry.until > generation) break;
40 oldCache.delete(identifier);
41 if (cache.get(identifier) === undefined) {
42 cache.delete(identifier);
44 lastClearedIdentifier = identifier;
47 if (clearedEntries > 0 || oldCache.size > 0) {
49 `${cache.size - oldCache.size} active entries, ${
51 } recently unused cached entries${
53 ? `, ${clearedEntries} old unused cache entries removed e. g. ${lastClearedIdentifier}`
58 let i = (cache.size / maxGenerations) | 0;
59 let j = cachePosition >= cache.size ? 0 : cachePosition;
60 cachePosition = j + i;
61 for (const [identifier, entry] of cache) {
66 if (entry !== undefined) {
67 // We don't delete the cache entry, but set it to undefined instead
68 // This reserves the location in the data table and avoids rehashing
69 // when constantly adding and removing entries.
70 // It will be deleted when removed from oldCache.
71 cache.set(identifier, undefined);
72 oldCache.delete(identifier);
73 oldCache.set(identifier, {
75 until: generation + maxGenerations
81 compiler.cache.hooks.store.tap(
82 { name: "MemoryWithGcCachePlugin", stage: Cache.STAGE_MEMORY },
83 (identifier, etag, data) => {
84 cache.set(identifier, { etag, data });
87 compiler.cache.hooks.get.tap(
88 { name: "MemoryWithGcCachePlugin", stage: Cache.STAGE_MEMORY },
89 (identifier, etag, gotHandlers) => {
90 const cacheEntry = cache.get(identifier);
91 if (cacheEntry === null) {
93 } else if (cacheEntry !== undefined) {
94 return cacheEntry.etag === etag ? cacheEntry.data : null;
96 const oldCacheEntry = oldCache.get(identifier);
97 if (oldCacheEntry !== undefined) {
98 const cacheEntry = oldCacheEntry.entry;
99 if (cacheEntry === null) {
100 oldCache.delete(identifier);
101 cache.set(identifier, cacheEntry);
104 if (cacheEntry.etag !== etag) return null;
105 oldCache.delete(identifier);
106 cache.set(identifier, cacheEntry);
107 return cacheEntry.data;
110 gotHandlers.push((result, callback) => {
111 if (result === undefined) {
112 cache.set(identifier, null);
114 cache.set(identifier, { etag, data: result });
120 compiler.cache.hooks.shutdown.tap(
121 { name: "MemoryWithGcCachePlugin", stage: Cache.STAGE_MEMORY },
129 module.exports = MemoryWithGcCachePlugin;