summaryrefslogtreecommitdiff
path: root/lib/internal
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2018-06-27 22:31:01 +0800
committerJoyee Cheung <joyeec9h3@gmail.com>2018-07-27 14:51:27 +0800
commit186c2fb6d88c473c6b811f36c87656785131b2d7 (patch)
tree034b0a47960a67d48f61a3591240529212adb7d6 /lib/internal
parentc91e0ed3ad048deba9639e35570e5b6dcd7b7d39 (diff)
downloadandroid-node-v8-186c2fb6d88c473c6b811f36c87656785131b2d7.tar.gz
android-node-v8-186c2fb6d88c473c6b811f36c87656785131b2d7.tar.bz2
android-node-v8-186c2fb6d88c473c6b811f36c87656785131b2d7.zip
build: create V8 code cache after script is run
This patch makes it possible to generate the code cache for the builtins directly from the original script object (instead of compiling a new one) and after the script has been run (via `NativeModule.require`). Before this patch only the top level functions (the wrapped ones) are included in the cache, after this patch the inner functions in those modules will be included as well. Also blacklists modules from dependencies like V8 and node-inspect since we cannot guarantee that they are suitable to be executed directly. PR-URL: https://github.com/nodejs/node/pull/21567 Refs: https://github.com/nodejs/node/issues/21563 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'lib/internal')
-rw-r--r--lib/internal/bootstrap/cache.js49
-rw-r--r--lib/internal/bootstrap/loaders.js10
2 files changed, 46 insertions, 13 deletions
diff --git a/lib/internal/bootstrap/cache.js b/lib/internal/bootstrap/cache.js
index 17c14e816f..e7e46bdf51 100644
--- a/lib/internal/bootstrap/cache.js
+++ b/lib/internal/bootstrap/cache.js
@@ -9,23 +9,50 @@ const {
NativeModule, internalBinding
} = require('internal/bootstrap/loaders');
+function getCodeCache(id) {
+ const cached = NativeModule.getCached(id);
+ if (cached && (cached.loaded || cached.loading)) {
+ return cached.script.createCachedData();
+ }
+
+ // The script has not been compiled and run
+ NativeModule.require(id);
+ return getCodeCache(id);
+}
+
+const depsModule = Object.keys(NativeModule._source).filter(
+ (key) => NativeModule.isDepsModule(key) || key.startsWith('internal/deps')
+);
+
+// Modules with source code compiled in js2c that
+// cannot be compiled with the code cache
+const cannotUseCache = [
+ 'config',
+ 'sys', // deprecated
+ 'internal/v8_prof_polyfill',
+ 'internal/v8_prof_processor',
+
+ 'internal/per_context',
+
+ 'internal/test/binding',
+ // 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'
+].concat(depsModule);
+
module.exports = {
+ cachableBuiltins: Object.keys(NativeModule._source).filter(
+ (key) => !cannotUseCache.includes(key)
+ ),
builtinSource: Object.assign({}, NativeModule._source),
+ getCodeCache,
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',
- 'internal/per_context',
- ]
+ cannotUseCache
};
diff --git a/lib/internal/bootstrap/loaders.js b/lib/internal/bootstrap/loaders.js
index c141c9adcf..e85d5de9b7 100644
--- a/lib/internal/bootstrap/loaders.js
+++ b/lib/internal/bootstrap/loaders.js
@@ -118,6 +118,7 @@
this.exportKeys = undefined;
this.loaded = false;
this.loading = false;
+ this.script = null; // The ContextifyScript of the module
}
NativeModule._source = getBinding('natives');
@@ -165,11 +166,14 @@
return nativeModule.exports;
};
+ NativeModule.isDepsModule = function(id) {
+ return id.startsWith('node-inspect/') || id.startsWith('v8/');
+ };
+
NativeModule.requireForDeps = function(id) {
if (!NativeModule.exists(id) ||
// TODO(TimothyGu): remove when DEP0084 reaches end of life.
- id.startsWith('node-inspect/') ||
- id.startsWith('v8/')) {
+ NativeModule.isDepsModule(id)) {
id = `internal/deps/${id}`;
}
return NativeModule.require(id);
@@ -241,6 +245,8 @@
codeCache[this.id], false, undefined
);
+ this.script = script;
+
// One of these conditions may be false when any of the inputs
// of the `node_js2c` target in node.gyp is modified.
// FIXME(joyeecheung):