-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
module: implement flushCompileCache() #54971
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev
Previous commit
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.
- Loading branch information
commit 5d5b549db5d9ae159b18407b7000e138c76b319a
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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; | ||
| } | ||
| }); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
er, I meant to open a separate PR about this (this was why I opened #54633 which removed a coverage regression that made me not include this and left the TODO above in #51672), somehow this ended up in the checkout of this PR due to local git mess up 😅 but it was approved and landed anyway..
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For posterity: the short summary of this small diff that accidentally get landed in this unrelated PR is that it speeds up core startup by not having to compile any inner functions in essential builtins. Previously, only the top-level functions were cached in the snapshot, so during bootstrap, when the inner functions were executed to do the loading, they still needed to be compiled without cache. This diff changed to cache the inner functions too, which adds ~630KB to the binary, in exchange of a slightly faster core startup (and also speeding up the first invocation of most functions provided by the essential builtins).