-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Since setting object properties in C++ can be slow, pass data to JS using preallocated smalloc buffer and create object in JS instead. PR-URL: #469 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
- Loading branch information
1 parent
5d01463
commit 5435cf2
Showing
4 changed files
with
73 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
namespace node { | ||
|
||
using v8::Context; | ||
using v8::ExternalArrayType; | ||
using v8::Function; | ||
using v8::FunctionCallbackInfo; | ||
using v8::Handle; | ||
|
@@ -20,25 +21,41 @@ using v8::Uint32; | |
using v8::V8; | ||
using v8::Value; | ||
|
||
#define HEAP_STATISTICS_PROPERTIES(V) \ | ||
V(0, total_heap_size, kTotalHeapSizeIndex) \ | ||
V(1, total_heap_size_executable, kTotalHeapSizeExecutableIndex) \ | ||
V(2, total_physical_size, kTotalPhysicalSizeIndex) \ | ||
V(3, used_heap_size, kUsedHeapSizeIndex) \ | ||
V(4, heap_size_limit, kHeapSizeLimitIndex) | ||
|
||
#define V(a, b, c) +1 | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
bnoordhuis
Member
|
||
static const size_t kHeapStatisticsBufferLength = HEAP_STATISTICS_PROPERTIES(V); | ||
#undef V | ||
|
||
static const ExternalArrayType kHeapStatisticsBufferType = | ||
v8::kExternalUint32Array; | ||
|
||
void GetHeapStatistics(const FunctionCallbackInfo<Value>& args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
CHECK(args.Length() == 1 && args[0]->IsObject()); | ||
|
||
Isolate* isolate = args.GetIsolate(); | ||
HeapStatistics s; | ||
isolate->GetHeapStatistics(&s); | ||
Local<Object> info = Object::New(isolate); | ||
// TODO(trevnorris): Setting many object properties in C++ is a significant | ||
// performance hit. Redo this to pass the results to JS and create/set the | ||
// properties there. | ||
#define V(name) \ | ||
info->Set(env->name ## _string(), Uint32::NewFromUnsigned(isolate, s.name())) | ||
V(total_heap_size); | ||
V(total_heap_size_executable); | ||
V(total_physical_size); | ||
V(used_heap_size); | ||
V(heap_size_limit); | ||
Local<Object> obj = args[0].As<Object>(); | ||
uint32_t* data = | ||
static_cast<uint32_t*>(obj->GetIndexedPropertiesExternalArrayData()); | ||
|
||
CHECK_NE(data, nullptr); | ||
ASSERT_EQ(obj->GetIndexedPropertiesExternalArrayDataType(), | ||
kHeapStatisticsBufferType); | ||
ASSERT_EQ(obj->GetIndexedPropertiesExternalArrayDataLength(), | ||
kHeapStatisticsBufferLength); | ||
|
||
#define V(i, name, _) \ | ||
data[i] = static_cast<uint32_t>(s.name()); | ||
|
||
HEAP_STATISTICS_PROPERTIES(V) | ||
#undef V | ||
args.GetReturnValue().Set(info); | ||
} | ||
|
||
|
||
|
@@ -54,6 +71,23 @@ void InitializeV8Bindings(Handle<Object> target, | |
Environment* env = Environment::GetCurrent(context); | ||
env->SetMethod(target, "getHeapStatistics", GetHeapStatistics); | ||
env->SetMethod(target, "setFlagsFromString", SetFlagsFromString); | ||
|
||
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), | ||
"kHeapStatisticsBufferLength"), | ||
Uint32::NewFromUnsigned(env->isolate(), | ||
kHeapStatisticsBufferLength)); | ||
|
||
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), | ||
"kHeapStatisticsBufferType"), | ||
Uint32::NewFromUnsigned(env->isolate(), | ||
kHeapStatisticsBufferType)); | ||
|
||
#define V(i, _, name) \ | ||
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #name), \ | ||
Uint32::NewFromUnsigned(env->isolate(), i)); | ||
|
||
HEAP_STATISTICS_PROPERTIES(V) | ||
#undef V | ||
} | ||
|
||
} // namespace node | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Why does this end with
+1
?