node/lib/internal/bootstrap/cache.js
Joyee Cheung 0858e5d9d8
src: always compile and store code cache for native modules
This patch changes the NativeModuleLoader to always try to find
code cache for native modules when it compiles them, and always
produce and store the code cache after compilation. The cache
map is protected by a mutex and can be accessed by different
threads - including the worker threads and the main thread. Hence any
thread can reuse the code cache if the native module has already
been compiled by another thread - in particular the cache of the
bootstrappers and per_context.js will always be hit when a new thread
is spun.

This results in a ~6% startup overhead in the worst case
(when only the main thread is launched without requiring any additional
native module - it now needs to do the extra work of finding and
storing caches), which balances out the recent improvements by moving
the compilation to C++, but it also leads to a ~60% improvement in
the best case (when a worker thread is spun and requires a lot of native
modules thus hitting the cache compiled by the main thread).

PR-URL: https://github.com/nodejs/node/pull/24950
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2018-12-18 18:02:11 +08:00

78 lines
2.0 KiB
JavaScript

'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 } = require('internal/bootstrap/loaders');
const {
source, getCodeCache, compileFunction
} = internalBinding('native_module');
const { hasTracing } = process.binding('config');
const depsModule = Object.keys(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 = [
'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);
// Skip modules that cannot be required when they are not
// built into the binary.
if (process.config.variables.v8_enable_inspector !== 1) {
cannotUseCache.push(
'inspector',
'internal/util/inspector',
);
}
if (!hasTracing) {
cannotUseCache.push('trace_events');
}
if (!process.versions.openssl) {
cannotUseCache.push(
'crypto',
'https',
'http2',
'tls',
'_tls_common',
'_tls_wrap',
'internal/crypto/certificate',
'internal/crypto/cipher',
'internal/crypto/diffiehellman',
'internal/crypto/hash',
'internal/crypto/keygen',
'internal/crypto/pbkdf2',
'internal/crypto/random',
'internal/crypto/scrypt',
'internal/crypto/sig',
'internal/crypto/util',
'internal/http2/core',
'internal/http2/compat',
'internal/streams/lazy_transform',
);
}
module.exports = {
cachableBuiltins: Object.keys(source).filter(
(key) => !cannotUseCache.includes(key)
),
getSource(id) { return source[id]; },
getCodeCache,
compileFunction,
cannotUseCache
};