Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/compile_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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_) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/compile_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
47 changes: 47 additions & 0 deletions test/parallel/test-compile-cache-consumed.js
Original file line number Diff line number Diff line change
@@ -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;
}));
Loading