Skip to content

Commit

Permalink
build: speed up startup with V8 code cache
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
  • Loading branch information
joyeecheung committed Jun 27, 2018
1 parent 7c45284 commit 4750ce2
Show file tree
Hide file tree
Showing 13 changed files with 325 additions and 3 deletions.
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ $(NODE_G_EXE): config.gypi out/Makefile
$(MAKE) -C out BUILDTYPE=Debug V=$(V)
if [ ! -r $@ -o ! -L $@ ]; then ln -fs out/Debug/$(NODE_EXE) $@; fi

CODE_CACHE_DIR ?= out/$(BUILDTYPE)/obj/gen
CODE_CACHE_FILE ?= $(CODE_CACHE_DIR)/node_code_cache.cc

.PHONY: with-code-cache
with-code-cache:
$(PYTHON) ./configure
$(MAKE)
mkdir -p $(CODE_CACHE_DIR)
out/$(BUILDTYPE)/$(NODE_EXE) --expose-internals tools/generate_code_cache.js $(CODE_CACHE_FILE)
$(PYTHON) ./configure --code-cache-path $(CODE_CACHE_FILE)
$(MAKE)

.PHONY: test-code-cache
test-code-cache: with-code-cache
$(PYTHON) tools/test.py $(PARALLEL_ARGS) --mode=$(BUILDTYPE_LOWER) code-cache

out/Makefile: common.gypi deps/uv/uv.gyp deps/http_parser/http_parser.gyp \
deps/zlib/zlib.gyp deps/v8/gypfiles/toolchain.gypi \
deps/v8/gypfiles/features.gypi deps/v8/gypfiles/v8.gyp node.gyp \
Expand Down
8 changes: 8 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,12 @@ parser.add_option('--without-snapshot',
dest='without_snapshot',
help=optparse.SUPPRESS_HELP)

parser.add_option('--code-cache-path',
action='store',
dest='code_cache_path',
help='Use a file generated by tools/generate_code_cache.js to compile the'
' code cache for builtin modules into the binary')

parser.add_option('--without-ssl',
action='store_true',
dest='without_ssl',
Expand Down Expand Up @@ -983,6 +989,8 @@ def configure_node(o):
o['variables']['debug_nghttp2'] = 'false'

o['variables']['node_no_browser_globals'] = b(options.no_browser_globals)
if options.code_cache_path:
o['variables']['node_code_cache_path'] = options.code_cache_path
o['variables']['node_shared'] = b(options.shared)
node_module_version = getmoduleversion.get_version()

Expand Down
30 changes: 30 additions & 0 deletions lib/internal/bootstrap/cache.js
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'
]
};
29 changes: 28 additions & 1 deletion lib/internal/bootstrap/loaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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/') ?
Expand Down
8 changes: 8 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'node_use_etw%': 'false',
'node_use_perfctr%': 'false',
'node_no_browser_globals%': 'false',
'node_code_cache_path%': '',
'node_use_v8_platform%': 'true',
'node_use_bundled_v8%': 'true',
'node_shared%': 'false',
Expand All @@ -24,6 +25,7 @@
'node_lib_target_name%': 'node_lib',
'node_intermediate_lib_type%': 'static_library',
'library_files': [
'lib/internal/bootstrap/cache.js',
'lib/internal/bootstrap/loaders.js',
'lib/internal/bootstrap/node.js',
'lib/async_hooks.js',
Expand Down Expand Up @@ -396,6 +398,7 @@
'src/module_wrap.h',
'src/node.h',
'src/node_buffer.h',
'src/node_code_cache.h',
'src/node_constants.h',
'src/node_contextify.h',
'src/node_debug_options.h',
Expand Down Expand Up @@ -459,6 +462,11 @@
'NODE_OPENSSL_SYSTEM_CERT_PATH="<(openssl_system_ca_path)"',
],
'conditions': [
[ 'node_code_cache_path!=""', {
'sources': [ '<(node_code_cache_path)' ]
}, {
'sources': [ 'src/node_code_cache_stub.cc' ]
}],
[ 'node_shared=="true" and node_module_version!="" and OS!="win"', {
'product_extension': '<(shlib_suffix)',
'xcode_settings': {
Expand Down
13 changes: 11 additions & 2 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "node_buffer.h"
#include "node_constants.h"
#include "node_javascript.h"
#include "node_code_cache.h"
#include "node_platform.h"
#include "node_version.h"
#include "node_internals.h"
Expand Down Expand Up @@ -1596,10 +1597,18 @@ static void GetInternalBinding(const FunctionCallbackInfo<Value>& args) {

Local<String> module = args[0].As<String>();
node::Utf8Value module_v(env->isolate(), module);
Local<Object> exports;

node_module* mod = get_internal_module(*module_v);
if (mod == nullptr) return ThrowIfNoSuchModule(env, *module_v);
Local<Object> exports = InitModule(env, mod, module);
if (mod != nullptr) {
exports = InitModule(env, mod, module);
} else if (!strcmp(*module_v, "code_cache")) {
// internalBinding('code_cache')
exports = Object::New(env->isolate());
DefineCodeCache(env, exports);
} else {
return ThrowIfNoSuchModule(env, *module_v);
}

args.GetReturnValue().Set(exports);
}
Expand Down
16 changes: 16 additions & 0 deletions src/node_code_cache.h
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_
14 changes: 14 additions & 0 deletions src/node_code_cache_stub.cc
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
21 changes: 21 additions & 0 deletions test/code-cache/code-cache.status
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]
42 changes: 42 additions & 0 deletions test/code-cache/test-code-cache.js
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`);
}
6 changes: 6 additions & 0 deletions test/code-cache/testcfg.py
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')
Loading

0 comments on commit 4750ce2

Please sign in to comment.