1546be9cf8
Changes the isolate's string table into an off-heap structure. This allows the string table to be resized without allocating on the V8 heap, and potentially triggering a GC. This allows existing strings to be inserted into the string table without requiring allocation. This has two important benefits: 1) It allows the deserializer to insert strings directly into the string table, rather than having to defer string insertion until deserialization completes. 2) It simplifies the concurrent string table lookup to allow resizing the table inside the write lock, therefore eliminating the race where two concurrent lookups could both resize the table. The off-heap string table has the following properties: 1) The general hashmap behaviour matches the HashTable, i.e. open addressing, power-of-two sized, quadratic probing. This could, of course, now be changed. 2) The empty and deleted sentinels are changed to Smi 0 and 1, respectively, to make those comparisons a bit cheaper and not require roots access. 3) When the HashTable is resized, the old elements array is kept alive in a linked list of previous arrays, so that concurrent lookups don't lose the data they're accessing. This linked list is cleared by the GC, as then we know that all threads are in a safepoint. 4) The GC treats the hash table entries as weak roots, and only walks them for non-live reference clearing and for evacuation. 5) Since there is no longer a FixedArray to serialize for the startup snapshot, there is now a custom serialization of the string table, and the string table root is considered unserializable during weak root iteration. As a bonus, the custom serialization is more efficient, as it skips non-string entries. As a drive-by, rename LookupStringExists_NoAllocate to TryStringToIndexOrLookupExisting, to make it clearer that it returns a non-string for the case when the string is an array index. As another drive-by, extract StringSet into a separate header. Bug: v8:10729 Change-Id: I9c990fb2d74d1fe222920408670974a70e969bca Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2339104 Commit-Queue: Leszek Swirski <leszeks@chromium.org> Reviewed-by: Jakob Gruber <jgruber@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{#69270}
222 lines
8.3 KiB
C++
222 lines
8.3 KiB
C++
// Copyright 2016 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 <cmath>
|
|
#include <iostream>
|
|
#include <limits>
|
|
|
|
#include "src/api/api-inl.h"
|
|
#include "src/codegen/compiler.h"
|
|
#include "src/objects/hash-table-inl.h"
|
|
#include "src/objects/objects-inl.h"
|
|
#include "src/objects/objects.h"
|
|
#include "src/objects/string-set.h"
|
|
#include "test/unittests/test-utils.h"
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
namespace v8 {
|
|
namespace internal {
|
|
|
|
namespace {
|
|
|
|
bool IsInStringInstanceTypeList(InstanceType instance_type) {
|
|
switch (instance_type) {
|
|
#define ASSERT_INSTANCE_TYPE(type, ...) \
|
|
STATIC_ASSERT(InstanceType::type < InstanceType::FIRST_NONSTRING_TYPE);
|
|
STRING_TYPE_LIST(ASSERT_INSTANCE_TYPE)
|
|
#undef ASSERT_INSTANCE_TYPE
|
|
#define TEST_INSTANCE_TYPE(type, ...) case InstanceType::type:
|
|
STRING_TYPE_LIST(TEST_INSTANCE_TYPE)
|
|
#undef TEST_INSTANCE_TYPE
|
|
return true;
|
|
default:
|
|
EXPECT_LE(InstanceType::FIRST_NONSTRING_TYPE, instance_type);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void CheckOneInstanceType(InstanceType instance_type) {
|
|
if (IsInStringInstanceTypeList(instance_type)) {
|
|
EXPECT_TRUE((instance_type & kIsNotStringMask) == kStringTag)
|
|
<< "Failing IsString mask check for " << instance_type;
|
|
} else {
|
|
EXPECT_FALSE((instance_type & kIsNotStringMask) == kStringTag)
|
|
<< "Failing !IsString mask check for " << instance_type;
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST(Object, InstanceTypeList) {
|
|
#define TEST_INSTANCE_TYPE(type) CheckOneInstanceType(InstanceType::type);
|
|
|
|
INSTANCE_TYPE_LIST(TEST_INSTANCE_TYPE)
|
|
#undef TEST_INSTANCE_TYPE
|
|
}
|
|
|
|
TEST(Object, InstanceTypeListOrder) {
|
|
int current = 0;
|
|
int last = -1;
|
|
InstanceType current_type = static_cast<InstanceType>(current);
|
|
EXPECT_EQ(current_type, InstanceType::FIRST_TYPE);
|
|
EXPECT_EQ(current_type, InstanceType::INTERNALIZED_STRING_TYPE);
|
|
#define TEST_INSTANCE_TYPE(type) \
|
|
current_type = InstanceType::type; \
|
|
current = static_cast<int>(current_type); \
|
|
if (current > static_cast<int>(LAST_NAME_TYPE)) { \
|
|
EXPECT_LE(last + 1, current); \
|
|
} \
|
|
EXPECT_LT(last, current) << " INSTANCE_TYPE_LIST is not ordered: " \
|
|
<< "last = " << static_cast<InstanceType>(last) \
|
|
<< " vs. current = " << current_type; \
|
|
last = current;
|
|
|
|
// Only test hand-written portion of instance type list. The generated portion
|
|
// doesn't run the same risk of getting out of order, and it does emit type
|
|
// names out of numerical order in one case: JS_OBJECT_TYPE is emitted before
|
|
// its subclass types, because types are emitted in depth-first pre-order
|
|
// traversal order, and some of its subclass types are numerically earlier.
|
|
INSTANCE_TYPE_LIST_BASE(TEST_INSTANCE_TYPE)
|
|
#undef TEST_INSTANCE_TYPE
|
|
}
|
|
|
|
TEST(Object, StructListOrder) {
|
|
int current = static_cast<int>(InstanceType::FIRST_STRUCT_TYPE);
|
|
int last = current - 1;
|
|
ASSERT_LT(0, last);
|
|
InstanceType current_type = static_cast<InstanceType>(current);
|
|
#define TEST_STRUCT(TYPE, class, name) \
|
|
current_type = InstanceType::TYPE; \
|
|
current = static_cast<int>(current_type); \
|
|
EXPECT_LE(last + 1, current) \
|
|
<< " STRUCT_LIST is not ordered: " \
|
|
<< " last = " << static_cast<InstanceType>(last) \
|
|
<< " vs. current = " << current_type; \
|
|
last = current;
|
|
|
|
// Only test the _BASE portion (the hand-coded part). Note that the values are
|
|
// not necessarily consecutive because some Structs that need special
|
|
// handling, such as those that have multiple Map instances associated, are
|
|
// omitted from this list.
|
|
STRUCT_LIST_GENERATOR_BASE(STRUCT_LIST_ADAPTER, TEST_STRUCT)
|
|
#undef TEST_STRUCT
|
|
}
|
|
|
|
using ObjectWithIsolate = TestWithIsolate;
|
|
|
|
TEST_F(ObjectWithIsolate, DictionaryGrowth) {
|
|
Handle<NumberDictionary> dict = NumberDictionary::New(isolate(), 1);
|
|
Handle<Object> value = isolate()->factory()->null_value();
|
|
PropertyDetails details = PropertyDetails::Empty();
|
|
|
|
// This test documents the expected growth behavior of a dictionary getting
|
|
// elements added to it one by one.
|
|
STATIC_ASSERT(HashTableBase::kMinCapacity == 4);
|
|
uint32_t i = 1;
|
|
// 3 elements fit into the initial capacity.
|
|
for (; i <= 3; i++) {
|
|
dict = NumberDictionary::Add(isolate(), dict, i, value, details);
|
|
CHECK_EQ(4, dict->Capacity());
|
|
}
|
|
// 4th element triggers growth.
|
|
DCHECK_EQ(4, i);
|
|
for (; i <= 5; i++) {
|
|
dict = NumberDictionary::Add(isolate(), dict, i, value, details);
|
|
CHECK_EQ(8, dict->Capacity());
|
|
}
|
|
// 6th element triggers growth.
|
|
DCHECK_EQ(6, i);
|
|
for (; i <= 11; i++) {
|
|
dict = NumberDictionary::Add(isolate(), dict, i, value, details);
|
|
CHECK_EQ(16, dict->Capacity());
|
|
}
|
|
// 12th element triggers growth.
|
|
DCHECK_EQ(12, i);
|
|
for (; i <= 21; i++) {
|
|
dict = NumberDictionary::Add(isolate(), dict, i, value, details);
|
|
CHECK_EQ(32, dict->Capacity());
|
|
}
|
|
// 22nd element triggers growth.
|
|
DCHECK_EQ(22, i);
|
|
for (; i <= 43; i++) {
|
|
dict = NumberDictionary::Add(isolate(), dict, i, value, details);
|
|
CHECK_EQ(64, dict->Capacity());
|
|
}
|
|
// 44th element triggers growth.
|
|
DCHECK_EQ(44, i);
|
|
for (; i <= 50; i++) {
|
|
dict = NumberDictionary::Add(isolate(), dict, i, value, details);
|
|
CHECK_EQ(128, dict->Capacity());
|
|
}
|
|
|
|
// If we grow by larger chunks, the next (sufficiently big) power of 2 is
|
|
// chosen as the capacity.
|
|
dict = NumberDictionary::New(isolate(), 1);
|
|
dict = NumberDictionary::EnsureCapacity(isolate(), dict, 65);
|
|
CHECK_EQ(128, dict->Capacity());
|
|
|
|
dict = NumberDictionary::New(isolate(), 1);
|
|
dict = NumberDictionary::EnsureCapacity(isolate(), dict, 30);
|
|
CHECK_EQ(64, dict->Capacity());
|
|
}
|
|
|
|
TEST_F(TestWithNativeContext, EmptyFunctionScopeInfo) {
|
|
// Check that the empty_function has a properly set up ScopeInfo.
|
|
Handle<JSFunction> function = RunJS<JSFunction>("(function(){})");
|
|
|
|
Handle<ScopeInfo> scope_info(function->shared().scope_info(),
|
|
function->GetIsolate());
|
|
Handle<ScopeInfo> empty_function_scope_info(
|
|
isolate()->empty_function()->shared().scope_info(),
|
|
function->GetIsolate());
|
|
|
|
EXPECT_EQ(scope_info->length(), empty_function_scope_info->length());
|
|
EXPECT_EQ(scope_info->Flags(), empty_function_scope_info->Flags());
|
|
EXPECT_EQ(scope_info->ParameterCount(),
|
|
empty_function_scope_info->ParameterCount());
|
|
EXPECT_EQ(scope_info->ContextLocalCount(),
|
|
empty_function_scope_info->ContextLocalCount());
|
|
}
|
|
|
|
TEST_F(TestWithNativeContext, RecreateScopeInfoWithLocalsBlocklistWorks) {
|
|
// Create a JSFunction to get a {ScopeInfo} we can use for the test.
|
|
Handle<JSFunction> function = RunJS<JSFunction>("(function foo() {})");
|
|
Handle<ScopeInfo> original_scope_info(function->shared().scope_info(),
|
|
isolate());
|
|
ASSERT_FALSE(original_scope_info->HasLocalsBlockList());
|
|
|
|
Handle<String> foo_string =
|
|
isolate()->factory()->NewStringFromStaticChars("foo");
|
|
Handle<String> bar_string =
|
|
isolate()->factory()->NewStringFromStaticChars("bar");
|
|
|
|
Handle<StringSet> blocklist = StringSet::New(isolate());
|
|
StringSet::Add(isolate(), blocklist, foo_string);
|
|
|
|
Handle<ScopeInfo> scope_info = ScopeInfo::RecreateWithBlockList(
|
|
isolate(), original_scope_info, blocklist);
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
EXPECT_TRUE(scope_info->HasLocalsBlockList());
|
|
EXPECT_TRUE(scope_info->LocalsBlockList().Has(isolate(), foo_string));
|
|
EXPECT_FALSE(scope_info->LocalsBlockList().Has(isolate(), bar_string));
|
|
|
|
EXPECT_EQ(original_scope_info->length() + 1, scope_info->length());
|
|
|
|
// Check that all variable fields *before* the blocklist stayed the same.
|
|
for (int i = ScopeInfo::kVariablePartIndex;
|
|
i < scope_info->LocalsBlockListIndex(); ++i) {
|
|
EXPECT_EQ(original_scope_info->get(i), scope_info->get(i));
|
|
}
|
|
|
|
// Check that all variable fields *after* the blocklist stayed the same.
|
|
for (int i = scope_info->LocalsBlockListIndex() + 1; i < scope_info->length();
|
|
++i) {
|
|
EXPECT_EQ(original_scope_info->get(i - 1), scope_info->get(i));
|
|
}
|
|
}
|
|
|
|
} // namespace internal
|
|
} // namespace v8
|