mirror of
https://github.com/nodejs/node.git
synced 2026-11-30 11:55:44 -05:00
src,tools: speed up startup by 2.5%
Use zero-copy external string resources for storing the built-in JS source code. Saves a few hundred kilobyte of memory and consistently speeds up `benchmark/misc/startup.js` by 2.5%. Everything old is new again! Commit74954ce("Add string class that uses ExternalAsciiStringResource.") from 2011 did the same thing but I removed that in 2013 in commit34b0a36("src: don't use NewExternal() with unaligned strings") because of a limitation in the V8 API. V8 no longer requires that strings are aligned if they are one-byte strings so it should be safe to re-enable external strings again. PR-URL: https://github.com/nodejs/node/pull/5458 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Robert Jefe Lindstaedt <robert.lindstaedt@gmail.com>
This commit is contained in:
1 parent
b814b4cec7
commit
cc0c736bcc
2 files changed
+61
-150
No files matched your search
+31
-18
@@ -6,33 +6,46 @@
|
||||
|
||||
namespace node {
|
||||
|
||||
using v8::HandleScope;
|
||||
using v8::Local;
|
||||
using v8::NewStringType;
|
||||
using v8::Object;
|
||||
using v8::String;
|
||||
|
||||
// id##_data is defined in node_natives.h.
|
||||
#define V(id) \
|
||||
static struct : public String::ExternalOneByteStringResource { \
|
||||
const char* data() const override { \
|
||||
return reinterpret_cast<const char*>(id##_data); \
|
||||
} \
|
||||
size_t length() const override { return sizeof(id##_data); } \
|
||||
void Dispose() override { /* Default calls `delete this`. */ } \
|
||||
} id##_external_data;
|
||||
NODE_NATIVES_MAP(V)
|
||||
#undef V
|
||||
|
||||
Local<String> MainSource(Environment* env) {
|
||||
return String::NewFromUtf8(
|
||||
env->isolate(),
|
||||
reinterpret_cast<const char*>(internal_bootstrap_node_native),
|
||||
NewStringType::kNormal,
|
||||
sizeof(internal_bootstrap_node_native)).ToLocalChecked();
|
||||
auto maybe_string =
|
||||
String::NewExternalOneByte(
|
||||
env->isolate(),
|
||||
&internal_bootstrap_node_external_data);
|
||||
return maybe_string.ToLocalChecked();
|
||||
}
|
||||
|
||||
void DefineJavaScript(Environment* env, Local<Object> target) {
|
||||
HandleScope scope(env->isolate());
|
||||
|
||||
for (auto native : natives) {
|
||||
if (native.source != internal_bootstrap_node_native) {
|
||||
Local<String> name = String::NewFromUtf8(env->isolate(), native.name);
|
||||
Local<String> source =
|
||||
String::NewFromUtf8(
|
||||
env->isolate(), reinterpret_cast<const char*>(native.source),
|
||||
NewStringType::kNormal, native.source_len).ToLocalChecked();
|
||||
target->Set(name, source);
|
||||
}
|
||||
}
|
||||
auto context = env->context();
|
||||
#define V(id) \
|
||||
do { \
|
||||
auto key = \
|
||||
String::NewFromOneByte( \
|
||||
env->isolate(), id##_name, NewStringType::kNormal, \
|
||||
sizeof(id##_name)).ToLocalChecked(); \
|
||||
auto value = \
|
||||
String::NewExternalOneByte( \
|
||||
env->isolate(), &id##_external_data).ToLocalChecked(); \
|
||||
CHECK(target->Set(context, key, value).FromJust()); \
|
||||
} while (0);
|
||||
NODE_NATIVES_MAP(V)
|
||||
#undef V
|
||||
}
|
||||
|
||||
} // namespace node
|
||||
Reference in new issue
Block a user