From 5cf99d8362508fca178c6e91d8b10a23eb5eddf5 Mon Sep 17 00:00:00 2001 From: Max schwenk Date: Thu, 12 Nov 2026 12:18:19 -0500 Subject: [PATCH] module: release consumed compile cache buffers Once V8 has accepted a cache read from disk, the compile cache keeps the buffer until the process exits, only to pass it to V8 again if the same code is compiled a second time in the process. Release it once V8 has consumed it, and read the file again in that case, so a process no longer holds the caches of all the modules it loaded in memory. Caches produced by the process itself are kept until they are written. --- src/compile_cache.cc | 28 +++++++++--- src/compile_cache.h | 2 + test/parallel/test-compile-cache-consumed.js | 47 ++++++++++++++++++++ 3 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 test/parallel/test-compile-cache-consumed.js diff --git a/src/compile_cache.cc b/src/compile_cache.cc index 90ed63f448ae..d6be8edf49f8 100644 --- a/src/compile_cache.cc +++ b/src/compile_cache.cc @@ -298,7 +298,13 @@ CompileCacheEntry* CompileCacheHandler::GetOrInsert(const uint8_t* code, // TODO(joyeecheung): let V8's in-isolate compilation cache take precedence. if (loaded != compiler_cache_store_.end() && loaded->second->code_hash == code_hash) { - return loaded->second.get(); + CompileCacheEntry* entry = loaded->second.get(); + // Compiled again, e.g. after being deleted from require.cache. The buffer + // of a cache that V8 consumed was released, so read the file again. + if (entry->consumed && entry->cache == nullptr) { + ReadCacheFile(entry); + } + return entry; } // If the code hash mismatches, the code has changed, discard the stale entry @@ -315,6 +321,7 @@ CompileCacheEntry* CompileCacheHandler::GetOrInsert(const uint8_t* code, result->source_filename = filename_utf8.ToString(); result->cache = nullptr; result->type = type; + result->consumed = false; // TODO(joyeecheung): if we fail enough times, stop trying for any future // files. @@ -355,7 +362,17 @@ void CompileCacheHandler::MaybeSaveImpl(CompileCacheEntry* entry, : (entry->cache == nullptr) ? "not initialized" : "accepted"); if (entry->cache != nullptr && !rejected) { // accepted - Debug("keeping the in-memory entry\n"); + if (!entry->refreshed) { + // The cache was read from disk and V8 has deserialized its own copy + // (see CopyCache()), so release the buffer instead of keeping the caches + // of all the modules the process loaded in memory until it exits. + Debug("keeping the in-memory entry, releasing its buffer\n"); + entry->cache.reset(); + entry->consumed = true; + } else { + // Produced by this process and not written yet. + Debug("keeping the in-memory entry\n"); + } return; } if (read_only_) { @@ -452,10 +469,11 @@ void CompileCacheHandler::Persist() { auto* entry = pair.second.get(); const char* type_name = entry->type_name(); if (entry->cache == nullptr) { - Debug("[compile cache] skip persisting %s %s because the cache was not " - "initialized\n", + Debug("[compile cache] skip persisting %s %s because %s\n", type_name, - entry->source_filename); + entry->source_filename, + entry->consumed ? "cache was the same" + : "the cache was not initialized"); continue; } if (entry->refreshed == false) { diff --git a/src/compile_cache.h b/src/compile_cache.h index ec405cf55b80..fd47eda65fde 100644 --- a/src/compile_cache.h +++ b/src/compile_cache.h @@ -38,6 +38,8 @@ struct CompileCacheEntry { CachedCodeType type; bool refreshed = false; bool persisted = false; + // The cache read from disk was accepted by V8 and its buffer released. + bool consumed = false; // Copy the cache into a new store for V8 to consume. Caller takes // ownership. diff --git a/test/parallel/test-compile-cache-consumed.js b/test/parallel/test-compile-cache-consumed.js new file mode 100644 index 000000000000..5b4842e5412c --- /dev/null +++ b/test/parallel/test-compile-cache-consumed.js @@ -0,0 +1,47 @@ +'use strict'; + +// This tests that a module compiled again in the same process, after its +// cache was consumed and released, reads the cache from disk again. + +const common = require('../common'); +const { spawnSyncAndAssert } = require('../common/child_process'); +const assert = require('assert'); +const fs = require('fs'); +const tmpdir = require('../common/tmpdir'); + +tmpdir.refresh(); +const dir = tmpdir.resolve('.compile_cache_dir'); +const modulePath = tmpdir.resolve('module.js'); +const script = tmpdir.resolve('script.js'); +fs.writeFileSync(modulePath, 'module.exports = 1;', 'utf-8'); +fs.writeFileSync(script, ` +require(${JSON.stringify(modulePath)}); +delete require.cache[${JSON.stringify(modulePath)}]; +require(${JSON.stringify(modulePath)}); +`, 'utf-8'); + +function run(stderr) { + spawnSyncAndAssert(process.execPath, [script], { + env: { + ...process.env, + NODE_DEBUG_NATIVE: 'COMPILE_CACHE', + NODE_COMPILE_CACHE: dir, + }, + cwd: tmpdir.path, + }, { stderr }); +} + +run(common.mustCall((output) => { + assert.match(output, /writing cache for .*module\.js.*success/); + return true; +})); + +run(common.mustCall((output) => { + const read = /reading cache from .* for CommonJS .*module\.js.*success/g; + const accepted = /module\.js was accepted, keeping the in-memory entry, releasing its buffer/g; + assert.strictEqual(output.match(read).length, 2); + assert.strictEqual(output.match(accepted).length, 2); + assert.match(output, /skip persisting CommonJS .*module\.js because cache was the same/); + assert.doesNotMatch(output, /writing cache for .*module\.js/); + return true; +}));