node/tools/generate_code_cache.js
Joyee Cheung 4750ce26f2
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>
2018-06-27 21:11:31 +08:00

124 lines
3.5 KiB
JavaScript

'use strict';
// Flags: --expose-internals
// This file generates the code cache for builtin modules and
// writes them into static char arrays of a C++ file that can be
// compiled into the binary using the `--code-cache-path` option
// of `configure`.
const {
nativeModuleWrap,
builtinSource,
cannotUseCache
} = require('internal/bootstrap/cache');
const vm = require('vm');
const fs = require('fs');
const resultPath = process.argv[2];
if (!resultPath) {
console.error(`Usage: ${process.argv[0]} ${process.argv[1]}` +
'path/to/node_code_cache.cc');
process.exit(1);
}
/**
* Format a number of a size in bytes into human-readable strings
* @param {number} num
* @return {string}
*/
function formatSize(num) {
if (num < 1024) {
return `${(num).toFixed(2)}B`;
} else if (num < 1024 ** 2) {
return `${(num / 1024).toFixed(2)}KB`;
} else if (num < 1024 ** 3) {
return `${(num / (1024 ** 2)).toFixed(2)}MB`;
} else {
return `${(num / (1024 ** 3)).toFixed(2)}GB`;
}
}
/**
* Generates the source code of definitions of the char arrays
* that contains the code cache and the source code of the
* initializers of the code cache.
*
* @param {string} key ID of the builtin module
* @param {Buffer} cache Code cache of the builtin module
* @return { definition: string, initializer: string }
*/
function getInitalizer(key, cache) {
const defName = key.replace(/\//g, '_').replace(/-/g, '_');
const definition = `static uint8_t ${defName}_raw[] = {\n` +
`${cache.join(',')}\n};`;
const initializer = `
v8::Local<v8::ArrayBuffer> ${defName}_ab =
v8::ArrayBuffer::New(isolate, ${defName}_raw, ${cache.length});
v8::Local<v8::Uint8Array> ${defName}_array =
v8::Uint8Array::New(${defName}_ab, 0, ${cache.length});
target->Set(context,
FIXED_ONE_BYTE_STRING(isolate, "${key}"),
${defName}_array).FromJust();
`;
return {
definition, initializer
};
}
const cacheDefinitions = [];
const cacheInitializers = [];
let totalCacheSize = 0;
for (const key of Object.keys(builtinSource)) {
if (cannotUseCache.includes(key)) continue;
const code = nativeModuleWrap(builtinSource[key]);
// Note that this must corresponds to the code in
// NativeModule.prototype.compile
const script = new vm.Script(code, {
filename: `${key}.js`,
produceCachedData: true
});
if (!script.cachedData) {
console.error(`Failed to generate code cache for '${key}'`);
process.exit(1);
}
const length = script.cachedData.length;
totalCacheSize += length;
const { definition, initializer } = getInitalizer(key, script.cachedData);
cacheDefinitions.push(definition);
cacheInitializers.push(initializer);
console.log(`Generated cache for '${key}', size = ${formatSize(length)}` +
`, total = ${formatSize(totalCacheSize)}`);
}
const result = `#include "node.h"
#include "node_code_cache.h"
#include "v8.h"
#include "env.h"
#include "env-inl.h"
// This file is generated by tools/generate_code_cache.js
// and is used when configure is run with \`--code-cache-path\`
namespace node {
${cacheDefinitions.join('\n\n')}
// The target here will be returned as \`internalBinding('code_cache')\`
void DefineCodeCache(Environment* env, v8::Local<v8::Object> target) {
v8::Isolate* isolate = env->isolate();
v8::Local<v8::Context> context = env->context();
${cacheInitializers.join('\n')}
}
} // namespace node
`;
fs.writeFileSync(resultPath, result);
console.log(`Generated code cache C++ file to ${resultPath}`);