-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: speed up startup with V8 code cache
This patch speeds up the startup time and reduce the startup memory footprint by using V8 code cache when comiling builtin modules. The current approach is demonstrated in the `with-code-cache` Makefile target (no corresponding Windows target at the moment). 1. Build the binary normally (`src/node_code_cache_stub.cc` is used), by now `internalBinding('code_cache')` is an empty object 2. Run `tools/generate_code_cache.js` with the binary, which generates the code caches by reading source code of builtin modules off source code exposed by `require('internal/bootstrap/cache').builtinSource` and then generate a C++ file containing static char arrays of the code cache, using a format similar to `node_javascript.cc` 3. Run `configure` with the `--code-cache-path` option so that the newly generated C++ file will be used when compiling the new binary. The generated C++ file will put the cache into the `internalBinding('code_cache')` object with the module ids as keys 4. The new binary tries to read the code cache from `internalBinding('code_cache')` and use it to compile builtin modules. If the cache is used, it will put the id into `require('internal/bootstrap/cache').compiledWithCache` for bookkeeping, otherwise the id will be pushed into `require('internal/bootstrap/cache').compiledWithoutCache` This patch also added tests that verify the code cache is generated and used when compiling builtin modules. The binary with code cache: - Is ~1MB bigger than the binary without code cahe - Consumes ~1MB less memory during start up - Starts up about 60% faster PR-URL: #21405 Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Gus Caplan <me@gus.host>
- Loading branch information
1 parent
76ef7ac
commit a7505c0
Showing
13 changed files
with
325 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
// This is only exposed for internal build steps and testing purposes. | ||
// We create new copies of the source and the code cache | ||
// so the resources eventually used to compile builtin modules | ||
// cannot be tampered with even with --expose-internals | ||
|
||
const { | ||
NativeModule, internalBinding | ||
} = require('internal/bootstrap/loaders'); | ||
|
||
module.exports = { | ||
builtinSource: Object.assign({}, NativeModule._source), | ||
codeCache: internalBinding('code_cache'), | ||
compiledWithoutCache: NativeModule.compiledWithoutCache, | ||
compiledWithCache: NativeModule.compiledWithCache, | ||
nativeModuleWrap(script) { | ||
return NativeModule.wrap(script); | ||
}, | ||
// Modules with source code compiled in js2c that | ||
// cannot be compiled with the code cache | ||
cannotUseCache: [ | ||
'config', | ||
// TODO(joyeecheung): update the C++ side so that | ||
// the code cache is also used when compiling these | ||
// two files. | ||
'internal/bootstrap/loaders', | ||
'internal/bootstrap/node' | ||
] | ||
}; |
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,16 @@ | ||
#ifndef SRC_NODE_CODE_CACHE_H_ | ||
#define SRC_NODE_CODE_CACHE_H_ | ||
|
||
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#include "node_internals.h" | ||
|
||
namespace node { | ||
|
||
void DefineCodeCache(Environment* env, v8::Local<v8::Object> target); | ||
|
||
} // namespace node | ||
|
||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#endif // SRC_NODE_CODE_CACHE_H_ |
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,14 @@ | ||
|
||
#include "node_code_cache.h" | ||
|
||
// This is supposed to be generated by tools/generate_code_cache.js | ||
// The stub here is used when configure is run without `--code-cache-path` | ||
|
||
namespace node { | ||
void DefineCodeCache(Environment* env, v8::Local<v8::Object> target) { | ||
// When we do not produce code cache for builtin modules, | ||
// `internalBinding('code_cache')` returns an empty object | ||
// (here as `target`) so this is a noop. | ||
} | ||
|
||
} // namespace node |
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 @@ | ||
prefix code-cache | ||
|
||
# To mark a test as flaky, list the test name in the appropriate section | ||
# below, without ".js", followed by ": PASS,FLAKY". Example: | ||
# sample-test : PASS,FLAKY | ||
|
||
[true] # This section applies to all platforms | ||
|
||
[$system==win32] | ||
|
||
[$system==linux] | ||
|
||
[$system==macos] | ||
|
||
[$arch==arm || $arch==arm64] | ||
|
||
[$system==solaris] # Also applies to SmartOS | ||
|
||
[$system==freebsd] | ||
|
||
[$system==aix] |
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,42 @@ | ||
'use strict'; | ||
|
||
// Flags: --expose-internals | ||
// This test verifies that the binary is compiled with code cache and the | ||
// cache is used when built in modules are compiled. | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const { | ||
types: { | ||
isUint8Array | ||
} | ||
} = require('util'); | ||
const { | ||
builtinSource, | ||
codeCache, | ||
cannotUseCache, | ||
compiledWithCache, | ||
compiledWithoutCache | ||
} = require('internal/bootstrap/cache'); | ||
|
||
assert.strictEqual( | ||
typeof process.config.variables.node_code_cache_path, | ||
'string' | ||
); | ||
|
||
assert.deepStrictEqual(compiledWithoutCache, []); | ||
|
||
const loadedModules = process.moduleLoadList | ||
.filter((m) => m.startsWith('NativeModule')) | ||
.map((m) => m.replace('NativeModule ', '')); | ||
|
||
for (const key of loadedModules) { | ||
assert(compiledWithCache.includes(key), | ||
`"${key}" should've been compiled with code cache`); | ||
} | ||
|
||
for (const key of Object.keys(builtinSource)) { | ||
if (cannotUseCache.includes(key)) continue; | ||
assert(isUint8Array(codeCache[key]) && codeCache[key].length > 0, | ||
`Code cache for "${key}" should've been generated`); | ||
} |
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,6 @@ | ||
import sys, os | ||
sys.path.append(os.path.join(os.path.dirname(__file__), '..')) | ||
import testpy | ||
|
||
def GetConfiguration(context, root): | ||
return testpy.ParallelTestConfiguration(context, root, 'code-cache') |
Oops, something went wrong.