summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2018-06-19 01:02:57 +0800
committerJoyee Cheung <joyeec9h3@gmail.com>2018-06-27 21:11:31 +0800
commit4750ce26f2e6079b5fee92bdee5356c279171d22 (patch)
tree1a53ec9b0b18b658109b453b236f053b0b457850 /lib
parent7c452845b8d44287f5db96a7f19e7d395e1899ab (diff)
downloadandroid-node-v8-4750ce26f2e6079b5fee92bdee5356c279171d22.tar.gz
android-node-v8-4750ce26f2e6079b5fee92bdee5356c279171d22.tar.bz2
android-node-v8-4750ce26f2e6079b5fee92bdee5356c279171d22.zip
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: https://github.com/nodejs/node/pull/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>
Diffstat (limited to 'lib')
-rw-r--r--lib/internal/bootstrap/cache.js30
-rw-r--r--lib/internal/bootstrap/loaders.js29
2 files changed, 58 insertions, 1 deletions
diff --git a/lib/internal/bootstrap/cache.js b/lib/internal/bootstrap/cache.js
new file mode 100644
index 0000000000..a3d22ba020
--- /dev/null
+++ b/lib/internal/bootstrap/cache.js
@@ -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'
+ ]
+};
diff --git a/lib/internal/bootstrap/loaders.js b/lib/internal/bootstrap/loaders.js
index de911eb841..c141c9adcf 100644
--- a/lib/internal/bootstrap/loaders.js
+++ b/lib/internal/bootstrap/loaders.js
@@ -125,10 +125,15 @@
const config = getBinding('config');
+ const codeCache = getInternalBinding('code_cache');
+ const compiledWithoutCache = NativeModule.compiledWithoutCache = [];
+ const compiledWithCache = NativeModule.compiledWithCache = [];
+
// Think of this as module.exports in this file even though it is not
// written in CommonJS style.
const loaderExports = { internalBinding, NativeModule };
const loaderId = 'internal/bootstrap/loaders';
+
NativeModule.require = function(id) {
if (id === loaderId) {
return loaderExports;
@@ -229,7 +234,29 @@
this.loading = true;
try {
- const script = new ContextifyScript(source, this.filename);
+ // (code, filename, lineOffset, columnOffset
+ // cachedData, produceCachedData, parsingContext)
+ const script = new ContextifyScript(
+ source, this.filename, 0, 0,
+ codeCache[this.id], false, undefined
+ );
+
+ // One of these conditions may be false when any of the inputs
+ // of the `node_js2c` target in node.gyp is modified.
+ // FIXME(joyeecheung):
+ // 1. Figure out how to resolve the dependency issue. When the
+ // code cache was introduced we were at a point where refactoring
+ // node.gyp may not be worth the effort.
+ // 2. Calculate checksums in both js2c and generate_code_cache.js
+ // and compare them before compiling the native modules since
+ // V8 only checks the length of the source to decide whether to
+ // reject the cache.
+ if (!codeCache[this.id] || script.cachedDataRejected) {
+ compiledWithoutCache.push(this.id);
+ } else {
+ compiledWithCache.push(this.id);
+ }
+
// Arguments: timeout, displayErrors, breakOnSigint
const fn = script.runInThisContext(-1, true, false);
const requireFn = this.id.startsWith('internal/deps/') ?