diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2018-06-19 01:02:57 +0800 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2018-06-27 21:11:31 +0800 |
commit | 4750ce26f2e6079b5fee92bdee5356c279171d22 (patch) | |
tree | 1a53ec9b0b18b658109b453b236f053b0b457850 /src | |
parent | 7c452845b8d44287f5db96a7f19e7d395e1899ab (diff) | |
download | android-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 'src')
-rw-r--r-- | src/node.cc | 13 | ||||
-rw-r--r-- | src/node_code_cache.h | 16 | ||||
-rw-r--r-- | src/node_code_cache_stub.cc | 14 |
3 files changed, 41 insertions, 2 deletions
diff --git a/src/node.cc b/src/node.cc index 513e95f2ed..acc5ab3a1e 100644 --- a/src/node.cc +++ b/src/node.cc @@ -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" @@ -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); } diff --git a/src/node_code_cache.h b/src/node_code_cache.h new file mode 100644 index 0000000000..d4775a2b65 --- /dev/null +++ b/src/node_code_cache.h @@ -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_ diff --git a/src/node_code_cache_stub.cc b/src/node_code_cache_stub.cc new file mode 100644 index 0000000000..3e9e5960c1 --- /dev/null +++ b/src/node_code_cache_stub.cc @@ -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 |