-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
build: speed up startup with V8 code cache #21405
Changes from 1 commit
005a274
30a3827
4cca13e
29cc730
7c4b001
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.target | ||
CODE_CACHE_FILE ?= $(CODE_CACHE_DIR)/node_code_cache.cc | ||
|
||
.PHONY: with-code-cache | ||
with-code-cache: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be possible to put this into the gyp file, but given that gyp has not been supported by V8 and we are trying to migrate into a new build system, it's easier to put the two-pass build step into Makefile (but to support that on Windows we would need to port to It may also be possible to generate the file using an executable compiled from C++ that includes |
||
$(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 \ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,15 +125,54 @@ | |
|
||
const config = getBinding('config'); | ||
|
||
const codeCache = getInternalBinding('code_cache'); | ||
const compiledWithoutCache = []; | ||
const 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'; | ||
|
||
// 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 cacheExports = { | ||
get builtinSource() { | ||
return getBinding('natives'); | ||
}, | ||
get codeCache() { | ||
return getInternalBinding('code_cache'); | ||
}, | ||
compiledWithoutCache, | ||
compiledWithCache, | ||
nativeModuleWrap(script) { | ||
return NativeModule.wrap(script); | ||
}, | ||
// Modules with source code compiled in js2c that | ||
// cannot be compiled with the code cache | ||
cannotUseCache: [ | ||
'config', | ||
'internal/bootstrap/cache', | ||
// 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' | ||
] | ||
}; | ||
const cacheId = 'internal/bootstrap/cache'; | ||
|
||
NativeModule.require = function(id) { | ||
if (id === loaderId) { | ||
return loaderExports; | ||
} | ||
|
||
if (id === cacheId) { | ||
return cacheExports; | ||
} | ||
|
||
const cached = NativeModule.getCached(id); | ||
if (cached && (cached.loaded || cached.loading)) { | ||
return cached.exports; | ||
|
@@ -184,7 +223,7 @@ | |
if (id === loaderId) { | ||
return false; | ||
} | ||
return NativeModule.exists(id); | ||
return id === cacheId || NativeModule.exists(id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can it be handled as any other internal (in its own file)? That way it avoids conditional checks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jdalton Good idea, I'll do that |
||
}; | ||
|
||
NativeModule.isInternal = function(id) { | ||
|
@@ -229,7 +268,24 @@ | |
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. | ||
// TODO(joyeecheung): figure out how to resolve the dependency | ||
// issue. When the code cache is introduced we are at a point where | ||
// refactoring node.gyp may not be worth the effort. | ||
if (!codeCache[this.id] || script.cachedDataRejected) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this should happen. If it does, something bad must have happened. Probably best to crash here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hashseed I assume you are talking about the second condition? The first happens when the cache is not built into the binary. Throwing on the second one ( |
||
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/') ? | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -1595,10 +1596,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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a functional difference here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @TimothyGu I was adding the |
||
|
||
args.GetReturnValue().Set(exports); | ||
} | ||
|
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_ |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
prefix v8-updates | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops, copy paste error :P |
||
|
||
# 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] |
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`); | ||
} |
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') |
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.
I think most all generated code is in
out/$(BUILDTYPE)/obj/gen
?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.
@addaleax Yes, not sure why I picked
obj.target
...thanks for pointing that out, I'll fix it