Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup! build: speed up startup with V8 code cache
  • Loading branch information
joyeecheung committed Jun 25, 2018
commit 30a3827b729080ec8069c710ed9266e1f5c24f4d
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'
]
};
40 changes: 3 additions & 37 deletions lib/internal/bootstrap/loaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,53 +126,19 @@
const config = getBinding('config');

const codeCache = getInternalBinding('code_cache');
const compiledWithoutCache = [];
const compiledWithCache = [];
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';

// 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;
Expand Down Expand Up @@ -223,7 +189,7 @@
if (id === loaderId) {
return false;
}
return id === cacheId || NativeModule.exists(id);
return NativeModule.exists(id);
};

NativeModule.isInternal = function(id) {
Expand Down
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -25,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