2020-06-10 13:45:24 +00:00
|
|
|
// Copyright 2020 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "src/api/api.h"
|
|
|
|
#include "src/base/platform/semaphore.h"
|
|
|
|
#include "src/handles/handles-inl.h"
|
|
|
|
#include "src/handles/local-handles-inl.h"
|
|
|
|
#include "src/handles/persistent-handles.h"
|
|
|
|
#include "src/heap/heap.h"
|
2020-07-10 08:58:37 +00:00
|
|
|
#include "src/heap/local-heap-inl.h"
|
2020-06-10 13:45:24 +00:00
|
|
|
#include "src/heap/local-heap.h"
|
2020-11-17 10:16:09 +00:00
|
|
|
#include "src/heap/parked-scope.h"
|
2020-06-10 13:45:24 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
|
|
|
#include "test/cctest/heap/heap-utils.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
static constexpr int kNumHandles = kHandleBlockSize * 2 + kHandleBlockSize / 2;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-07-14 11:18:36 +00:00
|
|
|
class ConcurrentSearchThread final : public v8::base::Thread {
|
2020-06-10 13:45:24 +00:00
|
|
|
public:
|
2020-07-14 11:18:36 +00:00
|
|
|
ConcurrentSearchThread(Heap* heap, std::vector<Handle<JSObject>> handles,
|
|
|
|
std::unique_ptr<PersistentHandles> ph,
|
|
|
|
Handle<Name> name, base::Semaphore* sema_started)
|
2020-06-10 13:45:24 +00:00
|
|
|
: v8::base::Thread(base::Thread::Options("ThreadWithLocalHeap")),
|
|
|
|
heap_(heap),
|
|
|
|
handles_(std::move(handles)),
|
|
|
|
ph_(std::move(ph)),
|
|
|
|
name_(name),
|
|
|
|
sema_started_(sema_started) {}
|
|
|
|
|
|
|
|
void Run() override {
|
2020-10-14 11:47:05 +00:00
|
|
|
LocalHeap local_heap(heap_, ThreadKind::kBackground, std::move(ph_));
|
2020-10-15 10:57:22 +00:00
|
|
|
UnparkedScope unparked_scope(&local_heap);
|
2020-06-10 13:45:24 +00:00
|
|
|
LocalHandleScope scope(&local_heap);
|
|
|
|
|
|
|
|
for (int i = 0; i < kNumHandles; i++) {
|
2020-07-10 08:58:37 +00:00
|
|
|
handles_.push_back(local_heap.NewPersistentHandle(handles_[0]));
|
2020-06-10 13:45:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sema_started_->Signal();
|
|
|
|
|
|
|
|
for (Handle<JSObject> handle : handles_) {
|
|
|
|
// Lookup the named property on the {map}.
|
|
|
|
CHECK(name_->IsUniqueName());
|
|
|
|
Handle<Map> map(handle->map(), &local_heap);
|
|
|
|
|
|
|
|
Handle<DescriptorArray> descriptors(
|
2020-10-05 09:22:13 +00:00
|
|
|
map->instance_descriptors(kAcquireLoad), &local_heap);
|
2020-06-10 13:45:24 +00:00
|
|
|
bool is_background_thread = true;
|
|
|
|
InternalIndex const number =
|
|
|
|
descriptors->Search(*name_, *map, is_background_thread);
|
|
|
|
CHECK(number.is_found());
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_EQ(handles_.size(), kNumHandles * 2);
|
|
|
|
}
|
|
|
|
|
2020-10-09 14:10:42 +00:00
|
|
|
private:
|
2020-06-10 13:45:24 +00:00
|
|
|
Heap* heap_;
|
|
|
|
std::vector<Handle<JSObject>> handles_;
|
|
|
|
std::unique_ptr<PersistentHandles> ph_;
|
|
|
|
Handle<Name> name_;
|
|
|
|
base::Semaphore* sema_started_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Uses linear search on a flat object, with up to 8 elements.
|
|
|
|
TEST(LinearSearchFlatObject) {
|
2020-09-11 12:38:53 +00:00
|
|
|
CcTest::InitializeVM();
|
2020-06-10 13:45:24 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
|
|
|
|
std::unique_ptr<PersistentHandles> ph = isolate->NewPersistentHandles();
|
|
|
|
std::vector<Handle<JSObject>> handles;
|
|
|
|
|
|
|
|
auto factory = isolate->factory();
|
|
|
|
HandleScope handle_scope(isolate);
|
|
|
|
|
|
|
|
Handle<JSFunction> function =
|
2020-11-10 11:22:49 +00:00
|
|
|
factory->NewFunctionForTesting(factory->empty_string());
|
2020-06-10 13:45:24 +00:00
|
|
|
Handle<JSObject> js_object = factory->NewJSObject(function);
|
|
|
|
Handle<String> name = CcTest::MakeString("property");
|
|
|
|
Handle<Object> value = CcTest::MakeString("dummy_value");
|
|
|
|
// For the default constructor function no in-object properties are reserved
|
|
|
|
// hence adding a single property will initialize the property-array.
|
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(js_object, name, value,
|
|
|
|
NONE)
|
|
|
|
.Check();
|
|
|
|
|
|
|
|
for (int i = 0; i < kNumHandles; i++) {
|
2020-07-10 08:58:37 +00:00
|
|
|
handles.push_back(ph->NewHandle(js_object));
|
2020-06-10 13:45:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 08:58:37 +00:00
|
|
|
Handle<Name> persistent_name = ph->NewHandle(name);
|
2020-06-10 13:45:24 +00:00
|
|
|
|
|
|
|
base::Semaphore sema_started(0);
|
|
|
|
|
|
|
|
// Pass persistent handles to background thread.
|
2020-07-14 11:18:36 +00:00
|
|
|
std::unique_ptr<ConcurrentSearchThread> thread(new ConcurrentSearchThread(
|
2020-06-10 13:45:24 +00:00
|
|
|
isolate->heap(), std::move(handles), std::move(ph), persistent_name,
|
|
|
|
&sema_started));
|
|
|
|
CHECK(thread->Start());
|
|
|
|
|
|
|
|
sema_started.Wait();
|
|
|
|
|
|
|
|
// Exercise descriptor in main thread too.
|
|
|
|
for (int i = 0; i < 7; ++i) {
|
|
|
|
Handle<String> filler_name = CcTest::MakeName("filler_property_", i);
|
|
|
|
Handle<Object> filler_value = CcTest::MakeString("dummy_value");
|
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(js_object, filler_name,
|
|
|
|
filler_value, NONE)
|
|
|
|
.Check();
|
|
|
|
}
|
|
|
|
CHECK_EQ(js_object->map().NumberOfOwnDescriptors(), 8);
|
|
|
|
|
|
|
|
thread->Join();
|
|
|
|
}
|
|
|
|
|
2020-06-12 09:21:46 +00:00
|
|
|
// Uses linear search on a flat object, which has more than 8 elements.
|
|
|
|
TEST(LinearSearchFlatObject_ManyElements) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
|
|
|
|
std::unique_ptr<PersistentHandles> ph = isolate->NewPersistentHandles();
|
|
|
|
std::vector<Handle<JSObject>> handles;
|
|
|
|
|
|
|
|
auto factory = isolate->factory();
|
|
|
|
HandleScope handle_scope(isolate);
|
|
|
|
|
|
|
|
Handle<JSFunction> function =
|
2020-11-10 11:22:49 +00:00
|
|
|
factory->NewFunctionForTesting(factory->empty_string());
|
2020-06-12 09:21:46 +00:00
|
|
|
Handle<JSObject> js_object = factory->NewJSObject(function);
|
|
|
|
Handle<String> name = CcTest::MakeString("property");
|
|
|
|
Handle<Object> value = CcTest::MakeString("dummy_value");
|
|
|
|
// For the default constructor function no in-object properties are reserved
|
|
|
|
// hence adding a single property will initialize the property-array.
|
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(js_object, name, value,
|
|
|
|
NONE)
|
|
|
|
.Check();
|
|
|
|
|
|
|
|
// If we have more than 8 properties we would do a binary search. However,
|
|
|
|
// since we are going search in a background thread, we force a linear search
|
|
|
|
// that is safe to do in the background.
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
Handle<String> filler_name = CcTest::MakeName("filler_property_", i);
|
|
|
|
Handle<Object> filler_value = CcTest::MakeString("dummy_value");
|
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(js_object, filler_name,
|
|
|
|
filler_value, NONE)
|
|
|
|
.Check();
|
|
|
|
}
|
|
|
|
CHECK_GT(js_object->map().NumberOfOwnDescriptors(), 8);
|
|
|
|
|
|
|
|
for (int i = 0; i < kNumHandles; i++) {
|
2020-07-10 08:58:37 +00:00
|
|
|
handles.push_back(ph->NewHandle(js_object));
|
2020-06-12 09:21:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 08:58:37 +00:00
|
|
|
Handle<Name> persistent_name = ph->NewHandle(name);
|
2020-06-12 09:21:46 +00:00
|
|
|
|
|
|
|
base::Semaphore sema_started(0);
|
|
|
|
|
|
|
|
// Pass persistent handles to background thread.
|
2020-07-14 11:18:36 +00:00
|
|
|
std::unique_ptr<ConcurrentSearchThread> thread(new ConcurrentSearchThread(
|
2020-06-12 09:21:46 +00:00
|
|
|
isolate->heap(), std::move(handles), std::move(ph), persistent_name,
|
|
|
|
&sema_started));
|
|
|
|
CHECK(thread->Start());
|
|
|
|
|
|
|
|
sema_started.Wait();
|
|
|
|
|
|
|
|
// Exercise descriptor in main thread too.
|
|
|
|
for (int i = 10; i < 20; ++i) {
|
|
|
|
Handle<String> filler_name = CcTest::MakeName("filler_property_", i);
|
|
|
|
Handle<Object> filler_value = CcTest::MakeString("dummy_value");
|
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(js_object, filler_name,
|
|
|
|
filler_value, NONE)
|
|
|
|
.Check();
|
|
|
|
}
|
|
|
|
|
|
|
|
thread->Join();
|
|
|
|
}
|
|
|
|
|
2020-06-10 13:45:24 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|