mirror of
https://github.com/nodejs/node.git
synced 2026-11-29 14:27:57 -05:00
src: use STL containers instead of v8 values for static module data
Instead of putting the source code and the cache in v8::Objects,
put them in per-process std::maps. This has the following benefits:
- It's slightly lighter in weight compared to storing things on the
v8 heap. Also it may be slightly faster since the preivous v8::Object
is already in dictionary mode - though the difference is very small
given the number of native modules is limited.
- The source and code cache generation templates are now much simpler
since they just initialize static arrays and manipulate STL
constructs.
- The static native module data can be accessed independently of any
Environment or Isolate, and it's easy to look them up from the
C++'s side.
- It's now impossible to mutate the source code used to compile
native modules from the JS land since it's completely separate
from the v8 heap. We can still get the constant strings from
process.binding('natives') but that's all.
A few drive-by fixes:
- Remove DecorateErrorStack in LookupAndCompile - We don't need to
capture the exception to decorate when we encounter
errors during native module compilation, as those errors should be
syntax errors and v8 is able to decorate them well. We use
CompileFunctionInContext so there is no need to worry about
wrappers either.
- The code cache could be rejected when node is started with v8 flags.
Instead of aborting in that case, simply keep a record in the
native_module_without_cache set.
- Refactor js2c.py a bit, reduce code duplication and inline Render()
to make the one-byte/two-byte special treatment easier to read.
PR-URL: https://github.com/nodejs/node/pull/24384
Fixes: https://github.com/Remove
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
0a08cd714e
commit
21e9aa2bf4
16 files changed
+460
-422
No files matched your search
@@ -13,6 +13,12 @@ const {
|
||||
cachableBuiltins
|
||||
} = require('internal/bootstrap/cache');
|
||||
|
||||
const {
|
||||
types: {
|
||||
isUint8Array
|
||||
}
|
||||
} = require('util');
|
||||
|
||||
function hash(str) {
|
||||
if (process.versions.openssl) {
|
||||
return require('crypto').createHash('sha256').update(str).digest('hex');
|
||||
@@ -52,29 +58,25 @@ function formatSize(num) {
|
||||
* initializers of the code cache.
|
||||
*
|
||||
* @param {string} key ID of the builtin module
|
||||
* @param {Buffer} cache Code cache of the builtin module
|
||||
* @param {Uint8Array} 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` +
|
||||
const defName = `${key.replace(/\//g, '_').replace(/-/g, '_')}_raw`;
|
||||
const definition = `static const uint8_t ${defName}[] = {\n` +
|
||||
`${cache.join(',')}\n};`;
|
||||
const source = getSource(key);
|
||||
const sourceHash = hash(source);
|
||||
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();
|
||||
`;
|
||||
const hashIntializer = `
|
||||
target->Set(context,
|
||||
FIXED_ONE_BYTE_STRING(isolate, "${key}"),
|
||||
OneByteString(isolate, "${sourceHash}")).FromJust();
|
||||
`;
|
||||
const initializer =
|
||||
'code_cache_.emplace(\n' +
|
||||
` "${key}",\n` +
|
||||
` UnionBytes(${defName}, arraysize(${defName}))\n` +
|
||||
');';
|
||||
const hashIntializer =
|
||||
'code_cache_hash_.emplace(\n' +
|
||||
` "${key}",\n` +
|
||||
` "${sourceHash}"\n` +
|
||||
');';
|
||||
return {
|
||||
definition, initializer, hashIntializer, sourceHash
|
||||
};
|
||||
@@ -85,55 +87,55 @@ const cacheInitializers = [];
|
||||
const cacheHashInitializers = [];
|
||||
let totalCacheSize = 0;
|
||||
|
||||
function lexical(a, b) {
|
||||
if (a < b) {
|
||||
return -1;
|
||||
}
|
||||
if (a > b) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (const key of cachableBuiltins) {
|
||||
for (const key of cachableBuiltins.sort(lexical)) {
|
||||
const cachedData = getCodeCache(key);
|
||||
if (!cachedData.length) {
|
||||
if (!isUint8Array(cachedData)) {
|
||||
console.error(`Failed to generate code cache for '${key}'`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const length = cachedData.length;
|
||||
totalCacheSize += length;
|
||||
const size = cachedData.byteLength;
|
||||
totalCacheSize += size;
|
||||
const {
|
||||
definition, initializer, hashIntializer, sourceHash
|
||||
} = getInitalizer(key, cachedData);
|
||||
cacheDefinitions.push(definition);
|
||||
cacheInitializers.push(initializer);
|
||||
cacheHashInitializers.push(hashIntializer);
|
||||
console.log(`Generated cache for '${key}', size = ${formatSize(length)}` +
|
||||
console.log(`Generated cache for '${key}', size = ${formatSize(size)}` +
|
||||
`, hash = ${sourceHash}, total = ${formatSize(totalCacheSize)}`);
|
||||
}
|
||||
|
||||
const result = `#include "node.h"
|
||||
#include "node_code_cache.h"
|
||||
#include "v8.h"
|
||||
#include "env.h"
|
||||
#include "env-inl.h"
|
||||
const result = `#include "node_native_module.h"
|
||||
#include "node_internals.h"
|
||||
|
||||
// This file is generated by tools/generate_code_cache.js
|
||||
// and is used when configure is run with \`--code-cache-path\`
|
||||
|
||||
namespace node {
|
||||
|
||||
namespace native_module {
|
||||
${cacheDefinitions.join('\n\n')}
|
||||
|
||||
const bool native_module_has_code_cache = true;
|
||||
|
||||
// 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')}
|
||||
void NativeModuleLoader::LoadCodeCache() {
|
||||
has_code_cache_ = true;
|
||||
${cacheInitializers.join('\n ')}
|
||||
}
|
||||
|
||||
// The target here will be returned as \`internalBinding('code_cache_hash')\`
|
||||
void DefineCodeCacheHash(Environment* env, v8::Local<v8::Object> target) {
|
||||
v8::Isolate* isolate = env->isolate();
|
||||
v8::Local<v8::Context> context = env->context();
|
||||
${cacheHashInitializers.join('\n')}
|
||||
void NativeModuleLoader::LoadCodeCacheHash() {
|
||||
${cacheHashInitializers.join('\n ')}
|
||||
}
|
||||
|
||||
} // namespace native_module
|
||||
} // namespace node
|
||||
`;
|
||||
|
||||
|
||||
Reference in new issue
Block a user