forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: implement flushCompileCache()
This implements an API for users to intentionally flush the accumulated compile cache instead of waiting until process shutdown. It may be useful for application that loads dependencies first and then either reload itself in other instances, or spawning other instances that load an overlapping set of its dependencies - in this case its useful to flush the cache early instead of waiting until the shutdown of itself. Currently flushing is triggered by either process shutdown or user requests. In the future we should simply start the writes right after module loading on a separate thread, and this method only blocks until all the pending writes (if any) on the other thread are finished. In that case, the off-thread writes should finish long before any attempt of flushing is made so the method would then only incur a negligible overhead from thread synchronization. PR-URL: nodejs#54971 Fixes: nodejs#54770 Fixes: nodejs#54465 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
- Loading branch information
1 parent
7d7b3da
commit e32c625
Showing
8 changed files
with
130 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
const { flushCompileCache, getCompileCacheDir } = require('module'); | ||
const { spawnSync } = require('child_process'); | ||
const assert = require('assert'); | ||
|
||
if (process.argv[2] !== 'child') { | ||
// The test should be run with the compile cache already enabled and NODE_DEBUG_NATIVE=COMPILE_CACHE. | ||
assert(getCompileCacheDir()); | ||
assert(process.env.NODE_DEBUG_NATIVE.includes('COMPILE_CACHE')); | ||
|
||
flushCompileCache(); | ||
|
||
const child1 = spawnSync(process.execPath, [__filename, 'child']); | ||
console.log(child1.stderr.toString().trim().split('\n').map(line => `[child1]${line}`).join('\n')); | ||
|
||
flushCompileCache(); | ||
|
||
const child2 = spawnSync(process.execPath, [__filename, 'child']); | ||
console.log(child2.stderr.toString().trim().split('\n').map(line => `[child2]${line}`).join('\n')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
|
||
// This tests module.flushCompileCache() works as expected. | ||
|
||
require('../common'); | ||
const { spawnSyncAndAssert } = require('../common/child_process'); | ||
const assert = require('assert'); | ||
const tmpdir = require('../common/tmpdir'); | ||
const fixtures = require('../common/fixtures'); | ||
|
||
{ | ||
// Test that it works with non-existent directory. | ||
tmpdir.refresh(); | ||
const cacheDir = tmpdir.resolve('compile_cache'); | ||
spawnSyncAndAssert( | ||
process.execPath, | ||
[fixtures.path('compile-cache-flush.js')], | ||
{ | ||
env: { | ||
...process.env, | ||
NODE_DEBUG_NATIVE: 'COMPILE_CACHE', | ||
NODE_COMPILE_CACHE: cacheDir, | ||
}, | ||
cwd: tmpdir.path | ||
}, | ||
{ | ||
stdout(output) { | ||
// This contains output from the nested spawnings of compile-cache-flush.js. | ||
assert.match(output, /child1.* cache for .*compile-cache-flush\.js was accepted, keeping the in-memory entry/); | ||
assert.match(output, /child2.* cache for .*compile-cache-flush\.js was accepted, keeping the in-memory entry/); | ||
return true; | ||
}, | ||
stderr(output) { | ||
// This contains output from the top-level spawning of compile-cache-flush.js. | ||
assert.match(output, /reading cache from .*compile_cache.* for CommonJS .*compile-cache-flush\.js/); | ||
assert.match(output, /compile-cache-flush\.js was not initialized, initializing the in-memory entry/); | ||
|
||
const writeRE = /writing cache for .*compile-cache-flush\.js.*success/; | ||
const flushRE = /module\.flushCompileCache\(\) finished/; | ||
assert.match(output, writeRE); | ||
assert.match(output, flushRE); | ||
// The cache writing should happen before flushing finishes i.e. it's not delayed until process shutdown. | ||
assert(output.match(writeRE).index < output.match(flushRE).index); | ||
return true; | ||
} | ||
}); | ||
} |