[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const Cache = require("../Cache");
|
---|
| 9 |
|
---|
| 10 | /** @typedef {import("webpack-sources").Source} Source */
|
---|
| 11 | /** @typedef {import("../Cache").Etag} Etag */
|
---|
| 12 | /** @typedef {import("../Compiler")} Compiler */
|
---|
| 13 | /** @typedef {import("../Module")} Module */
|
---|
| 14 |
|
---|
| 15 | class MemoryWithGcCachePlugin {
|
---|
| 16 | /**
|
---|
| 17 | * @param {object} options Options
|
---|
| 18 | * @param {number} options.maxGenerations max generations
|
---|
| 19 | */
|
---|
| 20 | constructor({ maxGenerations }) {
|
---|
| 21 | this._maxGenerations = maxGenerations;
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
| 25 | * Apply the plugin
|
---|
| 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: any } | undefined | null>} */
|
---|
| 32 | const cache = new Map();
|
---|
| 33 | /** @type {Map<string, { entry: { etag: Etag | null, data: any } | null, until: number }>} */
|
---|
| 34 | const oldCache = new Map();
|
---|
| 35 | let generation = 0;
|
---|
| 36 | let cachePosition = 0;
|
---|
| 37 | const logger = compiler.getInfrastructureLogger("MemoryWithGcCachePlugin");
|
---|
| 38 | compiler.hooks.afterDone.tap("MemoryWithGcCachePlugin", () => {
|
---|
| 39 | generation++;
|
---|
| 40 | let clearedEntries = 0;
|
---|
| 41 | let lastClearedIdentifier;
|
---|
| 42 | // Avoid coverage problems due indirect changes
|
---|
| 43 | /* istanbul ignore next */
|
---|
| 44 | for (const [identifier, entry] of oldCache) {
|
---|
| 45 | if (entry.until > generation) break;
|
---|
| 46 |
|
---|
| 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: "MemoryWithGcCachePlugin", stage: Cache.STAGE_MEMORY },
|
---|
| 90 | (identifier, etag, data) => {
|
---|
| 91 | cache.set(identifier, { etag, data });
|
---|
| 92 | }
|
---|
| 93 | );
|
---|
| 94 | compiler.cache.hooks.get.tap(
|
---|
| 95 | { name: "MemoryWithGcCachePlugin", 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: "MemoryWithGcCachePlugin", stage: Cache.STAGE_MEMORY },
|
---|
| 128 | () => {
|
---|
| 129 | cache.clear();
|
---|
| 130 | oldCache.clear();
|
---|
| 131 | }
|
---|
| 132 | );
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | module.exports = MemoryWithGcCachePlugin;
|
---|