Skip to content

Commit 8374fee

Browse files
joyeecheungCommit Bot
authored andcommitted
[snapshot] rehash JSMap and JSSet during deserialization
To rehash JSMap and JSSet, we simply replace the backing store with a new one created with the new hash. Bug: v8:9187 Change-Id: I90c25b18b33b7bc2b6ffe1b89fe17aa5f978b517 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2143983 Commit-Queue: Joyee Cheung <[email protected]> Reviewed-by: Jakob Gruber <[email protected]> Reviewed-by: Camillo Bruni <[email protected]> Cr-Commit-Position: refs/heads/master@{#67663}
1 parent 23dace8 commit 8374fee

10 files changed

Lines changed: 96 additions & 14 deletions

src/objects/heap-object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class HeapObject : public Object {
191191
bool CanBeRehashed() const;
192192

193193
// Rehash the object based on the layout inferred from its map.
194-
void RehashBasedOnMap(ReadOnlyRoots root);
194+
void RehashBasedOnMap(Isolate* isolate);
195195

196196
// Layout description.
197197
#define HEAP_OBJECT_FIELDS(V) \

src/objects/js-collection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class JSSet : public TorqueGeneratedJSSet<JSSet, JSCollection> {
3030
public:
3131
static void Initialize(Handle<JSSet> set, Isolate* isolate);
3232
static void Clear(Isolate* isolate, Handle<JSSet> set);
33+
void Rehash(Isolate* isolate);
3334

3435
// Dispatched behavior.
3536
DECL_PRINTER(JSSet)
@@ -56,6 +57,7 @@ class JSMap : public TorqueGeneratedJSMap<JSMap, JSCollection> {
5657
public:
5758
static void Initialize(Handle<JSMap> map, Isolate* isolate);
5859
static void Clear(Isolate* isolate, Handle<JSMap> map);
60+
void Rehash(Isolate* isolate);
5961

6062
// Dispatched behavior.
6163
DECL_PRINTER(JSMap)

src/objects/objects.cc

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,9 +2303,8 @@ bool HeapObject::NeedsRehashing() const {
23032303
case TRANSITION_ARRAY_TYPE:
23042304
return TransitionArray::cast(*this).number_of_entries() > 1;
23052305
case ORDERED_HASH_MAP_TYPE:
2306-
return OrderedHashMap::cast(*this).NumberOfElements() > 0;
23072306
case ORDERED_HASH_SET_TYPE:
2308-
return OrderedHashSet::cast(*this).NumberOfElements() > 0;
2307+
return false; // We'll rehash from the JSMap or JSSet referencing them.
23092308
case NAME_DICTIONARY_TYPE:
23102309
case GLOBAL_DICTIONARY_TYPE:
23112310
case NUMBER_DICTIONARY_TYPE:
@@ -2315,6 +2314,8 @@ bool HeapObject::NeedsRehashing() const {
23152314
case SMALL_ORDERED_HASH_MAP_TYPE:
23162315
case SMALL_ORDERED_HASH_SET_TYPE:
23172316
case SMALL_ORDERED_NAME_DICTIONARY_TYPE:
2317+
case JS_MAP_TYPE:
2318+
case JS_SET_TYPE:
23182319
return true;
23192320
default:
23202321
return false;
@@ -2324,10 +2325,13 @@ bool HeapObject::NeedsRehashing() const {
23242325
bool HeapObject::CanBeRehashed() const {
23252326
DCHECK(NeedsRehashing());
23262327
switch (map().instance_type()) {
2328+
case JS_MAP_TYPE:
2329+
case JS_SET_TYPE:
2330+
return true;
23272331
case ORDERED_HASH_MAP_TYPE:
23282332
case ORDERED_HASH_SET_TYPE:
2333+
UNREACHABLE(); // We'll rehash from the JSMap or JSSet referencing them.
23292334
case ORDERED_NAME_DICTIONARY_TYPE:
2330-
// TODO(yangguo): actually support rehashing OrderedHash{Map,Set}.
23312335
return false;
23322336
case NAME_DICTIONARY_TYPE:
23332337
case GLOBAL_DICTIONARY_TYPE:
@@ -2351,7 +2355,8 @@ bool HeapObject::CanBeRehashed() const {
23512355
return false;
23522356
}
23532357

2354-
void HeapObject::RehashBasedOnMap(ReadOnlyRoots roots) {
2358+
void HeapObject::RehashBasedOnMap(Isolate* isolate) {
2359+
ReadOnlyRoots roots = ReadOnlyRoots(isolate);
23552360
switch (map().instance_type()) {
23562361
case HASH_TABLE_TYPE:
23572362
UNREACHABLE();
@@ -2383,6 +2388,17 @@ void HeapObject::RehashBasedOnMap(ReadOnlyRoots roots) {
23832388
case SMALL_ORDERED_HASH_SET_TYPE:
23842389
DCHECK_EQ(0, SmallOrderedHashSet::cast(*this).NumberOfElements());
23852390
break;
2391+
case ORDERED_HASH_MAP_TYPE:
2392+
case ORDERED_HASH_SET_TYPE:
2393+
UNREACHABLE(); // We'll rehash from the JSMap or JSSet referencing them.
2394+
case JS_MAP_TYPE: {
2395+
JSMap::cast(*this).Rehash(isolate);
2396+
break;
2397+
}
2398+
case JS_SET_TYPE: {
2399+
JSSet::cast(*this).Rehash(isolate);
2400+
break;
2401+
}
23862402
case SMALL_ORDERED_NAME_DICTIONARY_TYPE:
23872403
DCHECK_EQ(0, SmallOrderedNameDictionary::cast(*this).NumberOfElements());
23882404
break;
@@ -7849,6 +7865,13 @@ void JSSet::Clear(Isolate* isolate, Handle<JSSet> set) {
78497865
set->set_table(*table);
78507866
}
78517867

7868+
void JSSet::Rehash(Isolate* isolate) {
7869+
Handle<OrderedHashSet> table_handle(OrderedHashSet::cast(table()), isolate);
7870+
Handle<OrderedHashSet> new_table =
7871+
OrderedHashSet::Rehash(isolate, table_handle).ToHandleChecked();
7872+
set_table(*new_table);
7873+
}
7874+
78527875
void JSMap::Initialize(Handle<JSMap> map, Isolate* isolate) {
78537876
Handle<OrderedHashMap> table = isolate->factory()->NewOrderedHashMap();
78547877
map->set_table(*table);
@@ -7860,6 +7883,13 @@ void JSMap::Clear(Isolate* isolate, Handle<JSMap> map) {
78607883
map->set_table(*table);
78617884
}
78627885

7886+
void JSMap::Rehash(Isolate* isolate) {
7887+
Handle<OrderedHashMap> table_handle(OrderedHashMap::cast(table()), isolate);
7888+
Handle<OrderedHashMap> new_table =
7889+
OrderedHashMap::Rehash(isolate, table_handle).ToHandleChecked();
7890+
set_table(*new_table);
7891+
}
7892+
78637893
void JSWeakCollection::Initialize(Handle<JSWeakCollection> weak_collection,
78647894
Isolate* isolate) {
78657895
Handle<EphemeronHashTable> table = EphemeronHashTable::New(isolate, 0);

src/objects/ordered-hash-table.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ HeapObject OrderedHashMap::GetEmpty(ReadOnlyRoots ro_roots) {
194194
return ro_roots.empty_ordered_hash_map();
195195
}
196196

197+
template <class Derived, int entrysize>
198+
MaybeHandle<Derived> OrderedHashTable<Derived, entrysize>::Rehash(
199+
Isolate* isolate, Handle<Derived> table) {
200+
return OrderedHashTable<Derived, entrysize>::Rehash(isolate, table,
201+
table->Capacity());
202+
}
203+
197204
template <class Derived, int entrysize>
198205
MaybeHandle<Derived> OrderedHashTable<Derived, entrysize>::Rehash(
199206
Isolate* isolate, Handle<Derived> table, int new_capacity) {
@@ -250,6 +257,20 @@ MaybeHandle<OrderedHashSet> OrderedHashSet::Rehash(Isolate* isolate,
250257
new_capacity);
251258
}
252259

260+
MaybeHandle<OrderedHashSet> OrderedHashSet::Rehash(
261+
Isolate* isolate, Handle<OrderedHashSet> table) {
262+
return OrderedHashTable<
263+
OrderedHashSet, OrderedHashSet::kEntrySizeWithoutChain>::Rehash(isolate,
264+
table);
265+
}
266+
267+
MaybeHandle<OrderedHashMap> OrderedHashMap::Rehash(
268+
Isolate* isolate, Handle<OrderedHashMap> table) {
269+
return OrderedHashTable<
270+
OrderedHashMap, OrderedHashMap::kEntrySizeWithoutChain>::Rehash(isolate,
271+
table);
272+
}
273+
253274
MaybeHandle<OrderedHashMap> OrderedHashMap::Rehash(Isolate* isolate,
254275
Handle<OrderedHashMap> table,
255276
int new_capacity) {

src/objects/ordered-hash-table.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class OrderedHashTable : public FixedArray {
138138

139139
// The extra +1 is for linking the bucket chains together.
140140
static const int kEntrySize = entrysize + 1;
141+
static const int kEntrySizeWithoutChain = entrysize;
141142
static const int kChainOffset = entrysize;
142143

143144
static const int kNotFound = -1;
@@ -200,6 +201,8 @@ class OrderedHashTable : public FixedArray {
200201
static MaybeHandle<Derived> Allocate(
201202
Isolate* isolate, int capacity,
202203
AllocationType allocation = AllocationType::kYoung);
204+
205+
static MaybeHandle<Derived> Rehash(Isolate* isolate, Handle<Derived> table);
203206
static MaybeHandle<Derived> Rehash(Isolate* isolate, Handle<Derived> table,
204207
int new_capacity);
205208

@@ -244,6 +247,8 @@ class V8_EXPORT_PRIVATE OrderedHashSet
244247
static MaybeHandle<OrderedHashSet> Rehash(Isolate* isolate,
245248
Handle<OrderedHashSet> table,
246249
int new_capacity);
250+
static MaybeHandle<OrderedHashSet> Rehash(Isolate* isolate,
251+
Handle<OrderedHashSet> table);
247252
static MaybeHandle<OrderedHashSet> Allocate(
248253
Isolate* isolate, int capacity,
249254
AllocationType allocation = AllocationType::kYoung);
@@ -273,6 +278,8 @@ class V8_EXPORT_PRIVATE OrderedHashMap
273278
static MaybeHandle<OrderedHashMap> Rehash(Isolate* isolate,
274279
Handle<OrderedHashMap> table,
275280
int new_capacity);
281+
static MaybeHandle<OrderedHashMap> Rehash(Isolate* isolate,
282+
Handle<OrderedHashMap> table);
276283
Object ValueAt(int entry);
277284

278285
// This takes and returns raw Address values containing tagged Object

src/snapshot/context-deserializer.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ MaybeHandle<Object> ContextDeserializer::Deserialize(
5959
// new code, which also has to be flushed from instruction cache.
6060
CHECK_EQ(start_address, code_space->top());
6161

62-
if (FLAG_rehash_snapshot && can_rehash()) Rehash();
6362
LogNewMapEvents();
6463

6564
result = handle(root, isolate);
6665
}
6766

67+
if (FLAG_rehash_snapshot && can_rehash()) Rehash();
6868
SetupOffHeapArrayBufferBackingStores();
6969

7070
return result;

src/snapshot/deserializer.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void Deserializer::Initialize(Isolate* isolate) {
8282
void Deserializer::Rehash() {
8383
DCHECK(can_rehash() || deserializing_user_code());
8484
for (HeapObject item : to_rehash_) {
85-
item.RehashBasedOnMap(ReadOnlyRoots(isolate_));
85+
item.RehashBasedOnMap(isolate_);
8686
}
8787
}
8888

@@ -142,6 +142,14 @@ void Deserializer::DeserializeDeferredObjects() {
142142
}
143143
}
144144
}
145+
146+
// When the deserialization of maps are deferred, they will be created
147+
// as filler maps, and we postpone the post processing until the maps
148+
// are also deserialized.
149+
for (const auto& pair : fillers_to_post_process_) {
150+
DCHECK(!pair.first.IsFiller());
151+
PostProcessNewObject(pair.first, pair.second);
152+
}
145153
}
146154

147155
void Deserializer::LogNewObjectEvents() {
@@ -213,7 +221,11 @@ HeapObject Deserializer::PostProcessNewObject(HeapObject obj,
213221
DisallowHeapAllocation no_gc;
214222

215223
if ((FLAG_rehash_snapshot && can_rehash_) || deserializing_user_code()) {
216-
if (obj.IsString()) {
224+
if (obj.IsFiller()) {
225+
DCHECK_EQ(fillers_to_post_process_.find(obj),
226+
fillers_to_post_process_.end());
227+
fillers_to_post_process_.insert({obj, space});
228+
} else if (obj.IsString()) {
217229
// Uninitialize hash field as we need to recompute the hash.
218230
String string = String::cast(obj);
219231
string.set_hash_field(String::kEmptyHashField);

src/snapshot/deserializer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ class V8_EXPORT_PRIVATE Deserializer : public SerializerDeserializer {
197197
// TODO(6593): generalize rehashing, and remove this flag.
198198
bool can_rehash_;
199199
std::vector<HeapObject> to_rehash_;
200+
// Store the objects whose maps are deferred and thus initialized as filler
201+
// maps during deserialization, so that they can be processed later when the
202+
// maps become available.
203+
std::unordered_map<HeapObject, SnapshotSpace, Object::Hasher>
204+
fillers_to_post_process_;
200205

201206
#ifdef DEBUG
202207
uint32_t num_api_references_;

src/snapshot/object-deserializer.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ MaybeHandle<HeapObject> ObjectDeserializer::Deserialize(Isolate* isolate) {
4747
LinkAllocationSites();
4848
LogNewMapEvents();
4949
result = handle(HeapObject::cast(root), isolate);
50-
Rehash();
5150
allocator()->RegisterDeserializedObjectsForBlackAllocation();
5251
}
52+
53+
Rehash();
5354
CommitPostProcessedObjects();
5455
return scope.CloseAndEscape(result);
5556
}

test/cctest/test-serialize.cc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3755,7 +3755,7 @@ UNINITIALIZED_TEST(SnapshotCreatorIncludeGlobalProxy) {
37553755
FreeCurrentEmbeddedBlob();
37563756
}
37573757

3758-
UNINITIALIZED_TEST(ReinitializeHashSeedNotRehashable) {
3758+
UNINITIALIZED_TEST(ReinitializeHashSeedJSCollectionRehashable) {
37593759
DisableAlwaysOpt();
37603760
i::FLAG_rehash_snapshot = true;
37613761
i::FLAG_hash_seed = 42;
@@ -3773,13 +3773,16 @@ UNINITIALIZED_TEST(ReinitializeHashSeedNotRehashable) {
37733773
CompileRun(
37743774
"var m = new Map();"
37753775
"m.set('a', 1);"
3776-
"m.set('b', 2);");
3776+
"m.set('b', 2);"
3777+
"var s = new Set();"
3778+
"s.add(1)");
37773779
ExpectInt32("m.get('b')", 2);
3780+
ExpectTrue("s.has(1)");
37783781
creator.SetDefaultContext(context);
37793782
}
37803783
blob =
37813784
creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear);
3782-
CHECK(!blob.CanBeRehashed());
3785+
CHECK(blob.CanBeRehashed());
37833786
}
37843787

37853788
i::FLAG_hash_seed = 1337;
@@ -3788,15 +3791,16 @@ UNINITIALIZED_TEST(ReinitializeHashSeedNotRehashable) {
37883791
create_params.snapshot_blob = &blob;
37893792
v8::Isolate* isolate = v8::Isolate::New(create_params);
37903793
{
3791-
// Check that no rehashing has been performed.
3792-
CHECK_EQ(static_cast<uint64_t>(42),
3794+
// Check that rehashing has been performed.
3795+
CHECK_EQ(static_cast<uint64_t>(1337),
37933796
HashSeed(reinterpret_cast<i::Isolate*>(isolate)));
37943797
v8::Isolate::Scope isolate_scope(isolate);
37953798
v8::HandleScope handle_scope(isolate);
37963799
v8::Local<v8::Context> context = v8::Context::New(isolate);
37973800
CHECK(!context.IsEmpty());
37983801
v8::Context::Scope context_scope(context);
37993802
ExpectInt32("m.get('b')", 2);
3803+
ExpectTrue("s.has(1)");
38003804
}
38013805
isolate->Dispose();
38023806
delete[] blob.data;

0 commit comments

Comments
 (0)