summaryrefslogtreecommitdiff
path: root/src/node_v8.cc
diff options
context:
space:
mode:
authorVladimir Kurchatkin <vladimir.kurchatkin@gmail.com>2015-01-16 19:15:17 +0300
committerBen Noordhuis <info@bnoordhuis.nl>2015-01-21 01:25:54 +0100
commit5435cf2f1619721745c7a8ac06b4f833d0b80d25 (patch)
tree5e6324259fdc616eb538edeeafa920d8937d8e54 /src/node_v8.cc
parent5d014637b618af7eac6ab0fce8d67884598c7b35 (diff)
downloadnode-new-5435cf2f1619721745c7a8ac06b4f833d0b80d25.tar.gz
v8: optimize `getHeapStatistics`
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: https://github.com/iojs/io.js/pull/469 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Diffstat (limited to 'src/node_v8.cc')
-rw-r--r--src/node_v8.cc60
1 files changed, 47 insertions, 13 deletions
diff --git a/src/node_v8.cc b/src/node_v8.cc
index 47e802e41b..f3bdda409d 100644
--- a/src/node_v8.cc
+++ b/src/node_v8.cc
@@ -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
+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