From 0e3fece02d574f2bae16b77e8a44c5dd2b4555c6 Mon Sep 17 00:00:00 2001 From: "mstarzinger@chromium.org" Date: Wed, 5 Dec 2012 09:13:53 +0000 Subject: [PATCH] Pass Isolate to Local::New() Our profiling revealed that Local::New() is one of bottlenecks of DOM bindings. BUG= TEST=cctest/test-api/LocalHandle Review URL: https://codereview.chromium.org/11316331 Patch from Kentaro Hara . git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13138 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- include/v8.h | 20 +++++++++++++++++--- src/api.cc | 6 ++++++ test/cctest/test-api.cc | 10 ++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/include/v8.h b/include/v8.h index e93555e..c569078 100644 --- a/include/v8.h +++ b/include/v8.h @@ -308,11 +308,13 @@ template class Local : public Handle { return Local::Cast(*this); } - /** Create a local handle for the content of another handle. - * The referee is kept alive by the local handle even when - * the original handle is destroyed/disposed. + /** + * Create a local handle for the content of another handle. + * The referee is kept alive by the local handle even when + * the original handle is destroyed/disposed. */ V8_INLINE(static Local New(Handle that)); + V8_INLINE(static Local New(Isolate* isolate, Handle that)); }; @@ -494,6 +496,8 @@ class V8EXPORT HandleScope { * Creates a new handle with the given value. */ static internal::Object** CreateHandle(internal::Object* value); + static internal::Object** CreateHandle(internal::Isolate* isolate, + internal::Object* value); // Faster version, uses HeapObject to obtain the current Isolate. static internal::Object** CreateHandle(internal::HeapObject* value); @@ -4285,6 +4289,16 @@ Local Local::New(Handle that) { template + Local Local::New(Isolate* isolate, Handle that) { + if (that.IsEmpty()) return Local(); + T* that_ptr = *that; + internal::Object** p = reinterpret_cast(that_ptr); + return Local(reinterpret_cast(HandleScope::CreateHandle( + reinterpret_cast(isolate), *p))); +} + + +template Persistent Persistent::New(Handle that) { if (that.IsEmpty()) return Persistent(); internal::Object** p = reinterpret_cast(*that); diff --git a/src/api.cc b/src/api.cc index 688b5ce..e19f372 100644 --- a/src/api.cc +++ b/src/api.cc @@ -769,6 +769,12 @@ i::Object** HandleScope::CreateHandle(i::Object* value) { } +i::Object** HandleScope::CreateHandle(i::Isolate* isolate, i::Object* value) { + ASSERT(isolate == i::Isolate::Current()); + return i::HandleScope::CreateHandle(value, isolate); +} + + i::Object** HandleScope::CreateHandle(i::HeapObject* value) { ASSERT(value->IsHeapObject()); return reinterpret_cast( diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc index 1610dcd..b7180d5 100644 --- a/test/cctest/test-api.cc +++ b/test/cctest/test-api.cc @@ -2360,6 +2360,16 @@ THREADED_TEST(GlobalHandle) { } +THREADED_TEST(LocalHandle) { + v8::HandleScope scope; + v8::Local local = v8::Local::New(v8_str("str")); + CHECK_EQ(local->Length(), 3); + + local = v8::Local::New(v8::Isolate::GetCurrent(), v8_str("str")); + CHECK_EQ(local->Length(), 3); +} + + class WeakCallCounter { public: explicit WeakCallCounter(int id) : id_(id), number_of_weak_calls_(0) { } -- 2.7.4