Skip to content

Commit 7ad6cfa

Browse files
joyeecheungcodebytere
authored andcommitted
deps: V8: backport 22014de00115
Original commit message: Reland "[snapshot] rehash JSMap and JSSet during deserialization" This is a reland of 8374feed55a5b3010f2e9593560a2d84f9f6725f. Fixed rehashing of global proxy keys by creating its identity hash early, before the deserialization of the context snapshot. Original change's description: > [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} Bug: v8:9187, v8:10523 Change-Id: I7a0319b1d10ff07644de902fec43e7c2b1dd8da9 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2212085 Reviewed-by: Leszek Swirski <[email protected]> Reviewed-by: Camillo Bruni <[email protected]> Reviewed-by: Jakob Gruber <[email protected]> Commit-Queue: Joyee Cheung <[email protected]> Cr-Commit-Position: refs/heads/master@{#67999} Refs: v8/v8@22014de PR-URL: #33300 Refs: v8/v8@ea0719b Refs: v8/v8@bb9f0c2 Refs: #17058 Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 817befd commit 7ad6cfa

12 files changed

+104
-16
lines changed

common.gypi

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
# Reset this number to 0 on major V8 upgrades.
3838
# Increment by one for each non-official patch applied to deps/v8.
39-
'v8_embedder_string': '-node.19',
39+
'v8_embedder_string': '-node.20',
4040

4141
##### V8 defaults for Node.js #####
4242

deps/v8/src/heap/factory.cc

+5-1
Original file line numberDiff line numberDiff line change
@@ -2761,8 +2761,12 @@ Handle<JSGlobalProxy> Factory::NewUninitializedJSGlobalProxy(int size) {
27612761
map->set_is_access_check_needed(true);
27622762
map->set_may_have_interesting_symbols(true);
27632763
LOG(isolate(), MapDetails(*map));
2764-
return Handle<JSGlobalProxy>::cast(
2764+
Handle<JSGlobalProxy> proxy = Handle<JSGlobalProxy>::cast(
27652765
NewJSObjectFromMap(map, AllocationType::kYoung));
2766+
// Create identity hash early in case there is any JS collection containing
2767+
// a global proxy key and needs to be rehashed after deserialization.
2768+
proxy->GetOrCreateIdentityHash(isolate());
2769+
return proxy;
27662770
}
27672771

27682772
void Factory::ReinitializeJSGlobalProxy(Handle<JSGlobalProxy> object,

deps/v8/src/objects/heap-object.h

+1-1
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) \

deps/v8/src/objects/js-collection.h

+2
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)

deps/v8/src/objects/objects.cc

+34-4
Original file line numberDiff line numberDiff line change
@@ -2305,9 +2305,8 @@ bool HeapObject::NeedsRehashing() const {
23052305
case TRANSITION_ARRAY_TYPE:
23062306
return TransitionArray::cast(*this).number_of_entries() > 1;
23072307
case ORDERED_HASH_MAP_TYPE:
2308-
return OrderedHashMap::cast(*this).NumberOfElements() > 0;
23092308
case ORDERED_HASH_SET_TYPE:
2310-
return OrderedHashSet::cast(*this).NumberOfElements() > 0;
2309+
return false; // We'll rehash from the JSMap or JSSet referencing them.
23112310
case NAME_DICTIONARY_TYPE:
23122311
case GLOBAL_DICTIONARY_TYPE:
23132312
case NUMBER_DICTIONARY_TYPE:
@@ -2317,6 +2316,8 @@ bool HeapObject::NeedsRehashing() const {
23172316
case SMALL_ORDERED_HASH_MAP_TYPE:
23182317
case SMALL_ORDERED_HASH_SET_TYPE:
23192318
case SMALL_ORDERED_NAME_DICTIONARY_TYPE:
2319+
case JS_MAP_TYPE:
2320+
case JS_SET_TYPE:
23202321
return true;
23212322
default:
23222323
return false;
@@ -2326,10 +2327,13 @@ bool HeapObject::NeedsRehashing() const {
23262327
bool HeapObject::CanBeRehashed() const {
23272328
DCHECK(NeedsRehashing());
23282329
switch (map().instance_type()) {
2330+
case JS_MAP_TYPE:
2331+
case JS_SET_TYPE:
2332+
return true;
23292333
case ORDERED_HASH_MAP_TYPE:
23302334
case ORDERED_HASH_SET_TYPE:
2335+
UNREACHABLE(); // We'll rehash from the JSMap or JSSet referencing them.
23312336
case ORDERED_NAME_DICTIONARY_TYPE:
2332-
// TODO(yangguo): actually support rehashing OrderedHash{Map,Set}.
23332337
return false;
23342338
case NAME_DICTIONARY_TYPE:
23352339
case GLOBAL_DICTIONARY_TYPE:
@@ -2353,7 +2357,8 @@ bool HeapObject::CanBeRehashed() const {
23532357
return false;
23542358
}
23552359

2356-
void HeapObject::RehashBasedOnMap(ReadOnlyRoots roots) {
2360+
void HeapObject::RehashBasedOnMap(Isolate* isolate) {
2361+
ReadOnlyRoots roots = ReadOnlyRoots(isolate);
23572362
switch (map().instance_type()) {
23582363
case HASH_TABLE_TYPE:
23592364
UNREACHABLE();
@@ -2385,6 +2390,17 @@ void HeapObject::RehashBasedOnMap(ReadOnlyRoots roots) {
23852390
case SMALL_ORDERED_HASH_SET_TYPE:
23862391
DCHECK_EQ(0, SmallOrderedHashSet::cast(*this).NumberOfElements());
23872392
break;
2393+
case ORDERED_HASH_MAP_TYPE:
2394+
case ORDERED_HASH_SET_TYPE:
2395+
UNREACHABLE(); // We'll rehash from the JSMap or JSSet referencing them.
2396+
case JS_MAP_TYPE: {
2397+
JSMap::cast(*this).Rehash(isolate);
2398+
break;
2399+
}
2400+
case JS_SET_TYPE: {
2401+
JSSet::cast(*this).Rehash(isolate);
2402+
break;
2403+
}
23882404
case SMALL_ORDERED_NAME_DICTIONARY_TYPE:
23892405
DCHECK_EQ(0, SmallOrderedNameDictionary::cast(*this).NumberOfElements());
23902406
break;
@@ -7864,6 +7880,13 @@ void JSSet::Clear(Isolate* isolate, Handle<JSSet> set) {
78647880
set->set_table(*table);
78657881
}
78667882

7883+
void JSSet::Rehash(Isolate* isolate) {
7884+
Handle<OrderedHashSet> table_handle(OrderedHashSet::cast(table()), isolate);
7885+
Handle<OrderedHashSet> new_table =
7886+
OrderedHashSet::Rehash(isolate, table_handle).ToHandleChecked();
7887+
set_table(*new_table);
7888+
}
7889+
78677890
void JSMap::Initialize(Handle<JSMap> map, Isolate* isolate) {
78687891
Handle<OrderedHashMap> table = isolate->factory()->NewOrderedHashMap();
78697892
map->set_table(*table);
@@ -7875,6 +7898,13 @@ void JSMap::Clear(Isolate* isolate, Handle<JSMap> map) {
78757898
map->set_table(*table);
78767899
}
78777900

7901+
void JSMap::Rehash(Isolate* isolate) {
7902+
Handle<OrderedHashMap> table_handle(OrderedHashMap::cast(table()), isolate);
7903+
Handle<OrderedHashMap> new_table =
7904+
OrderedHashMap::Rehash(isolate, table_handle).ToHandleChecked();
7905+
set_table(*new_table);
7906+
}
7907+
78787908
void JSWeakCollection::Initialize(Handle<JSWeakCollection> weak_collection,
78797909
Isolate* isolate) {
78807910
Handle<EphemeronHashTable> table = EphemeronHashTable::New(isolate, 0);

deps/v8/src/objects/ordered-hash-table.cc

+21
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) {

deps/v8/src/objects/ordered-hash-table.h

+7
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

deps/v8/src/snapshot/deserializer.cc

+14-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void Deserializer::Initialize(Isolate* isolate) {
7070
void Deserializer::Rehash() {
7171
DCHECK(can_rehash() || deserializing_user_code());
7272
for (HeapObject item : to_rehash_) {
73-
item.RehashBasedOnMap(ReadOnlyRoots(isolate_));
73+
item.RehashBasedOnMap(isolate_);
7474
}
7575
}
7676

@@ -130,6 +130,14 @@ void Deserializer::DeserializeDeferredObjects() {
130130
}
131131
}
132132
}
133+
134+
// When the deserialization of maps are deferred, they will be created
135+
// as filler maps, and we postpone the post processing until the maps
136+
// are also deserialized.
137+
for (const auto& pair : fillers_to_post_process_) {
138+
DCHECK(!pair.first.IsFiller());
139+
PostProcessNewObject(pair.first, pair.second);
140+
}
133141
}
134142

135143
void Deserializer::LogNewObjectEvents() {
@@ -201,7 +209,11 @@ HeapObject Deserializer::PostProcessNewObject(HeapObject obj,
201209
DisallowHeapAllocation no_gc;
202210

203211
if ((FLAG_rehash_snapshot && can_rehash_) || deserializing_user_code()) {
204-
if (obj.IsString()) {
212+
if (obj.IsFiller()) {
213+
DCHECK_EQ(fillers_to_post_process_.find(obj),
214+
fillers_to_post_process_.end());
215+
fillers_to_post_process_.insert({obj, space});
216+
} else if (obj.IsString()) {
205217
// Uninitialize hash field as we need to recompute the hash.
206218
String string = String::cast(obj);
207219
string.set_hash_field(String::kEmptyHashField);

deps/v8/src/snapshot/deserializer.h

+5
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ class V8_EXPORT_PRIVATE Deserializer : public SerializerDeserializer {
194194
// TODO(6593): generalize rehashing, and remove this flag.
195195
bool can_rehash_;
196196
std::vector<HeapObject> to_rehash_;
197+
// Store the objects whose maps are deferred and thus initialized as filler
198+
// maps during deserialization, so that they can be processed later when the
199+
// maps become available.
200+
std::unordered_map<HeapObject, SnapshotSpace, Object::Hasher>
201+
fillers_to_post_process_;
197202

198203
#ifdef DEBUG
199204
uint32_t num_api_references_;

deps/v8/src/snapshot/object-deserializer.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ MaybeHandle<HeapObject> ObjectDeserializer::Deserialize(Isolate* isolate) {
4848
LinkAllocationSites();
4949
LogNewMapEvents();
5050
result = handle(HeapObject::cast(root), isolate);
51-
Rehash();
5251
allocator()->RegisterDeserializedObjectsForBlackAllocation();
5352
}
53+
Rehash();
5454
CommitPostProcessedObjects();
5555
return scope.CloseAndEscape(result);
5656
}

deps/v8/src/snapshot/partial-deserializer.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ MaybeHandle<Object> PartialDeserializer::Deserialize(
5757
// new code, which also has to be flushed from instruction cache.
5858
CHECK_EQ(start_address, code_space->top());
5959

60-
if (FLAG_rehash_snapshot && can_rehash()) Rehash();
6160
LogNewMapEvents();
6261

6362
result = handle(root, isolate);
6463
}
6564

65+
if (FLAG_rehash_snapshot && can_rehash()) Rehash();
6666
SetupOffHeapArrayBufferBackingStores();
6767

6868
return result;

deps/v8/test/cctest/test-serialize.cc

+12-5
Original file line numberDiff line numberDiff line change
@@ -3778,7 +3778,7 @@ UNINITIALIZED_TEST(SnapshotCreatorIncludeGlobalProxy) {
37783778
FreeCurrentEmbeddedBlob();
37793779
}
37803780

3781-
UNINITIALIZED_TEST(ReinitializeHashSeedNotRehashable) {
3781+
UNINITIALIZED_TEST(ReinitializeHashSeedJSCollectionRehashable) {
37823782
DisableAlwaysOpt();
37833783
i::FLAG_rehash_snapshot = true;
37843784
i::FLAG_hash_seed = 42;
@@ -3796,13 +3796,18 @@ UNINITIALIZED_TEST(ReinitializeHashSeedNotRehashable) {
37963796
CompileRun(
37973797
"var m = new Map();"
37983798
"m.set('a', 1);"
3799-
"m.set('b', 2);");
3799+
"m.set('b', 2);"
3800+
"var s = new Set();"
3801+
"s.add(1);"
3802+
"s.add(globalThis);");
38003803
ExpectInt32("m.get('b')", 2);
3804+
ExpectTrue("s.has(1)");
3805+
ExpectTrue("s.has(globalThis)");
38013806
creator.SetDefaultContext(context);
38023807
}
38033808
blob =
38043809
creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear);
3805-
CHECK(!blob.CanBeRehashed());
3810+
CHECK(blob.CanBeRehashed());
38063811
}
38073812

38083813
ReadOnlyHeap::ClearSharedHeapForTest();
@@ -3812,15 +3817,17 @@ UNINITIALIZED_TEST(ReinitializeHashSeedNotRehashable) {
38123817
create_params.snapshot_blob = &blob;
38133818
v8::Isolate* isolate = v8::Isolate::New(create_params);
38143819
{
3815-
// Check that no rehashing has been performed.
3816-
CHECK_EQ(static_cast<uint64_t>(42),
3820+
// Check that rehashing has been performed.
3821+
CHECK_EQ(static_cast<uint64_t>(1337),
38173822
HashSeed(reinterpret_cast<i::Isolate*>(isolate)));
38183823
v8::Isolate::Scope isolate_scope(isolate);
38193824
v8::HandleScope handle_scope(isolate);
38203825
v8::Local<v8::Context> context = v8::Context::New(isolate);
38213826
CHECK(!context.IsEmpty());
38223827
v8::Context::Scope context_scope(context);
38233828
ExpectInt32("m.get('b')", 2);
3829+
ExpectTrue("s.has(1)");
3830+
ExpectTrue("s.has(globalThis)");
38243831
}
38253832
isolate->Dispose();
38263833
delete[] blob.data;

0 commit comments

Comments
 (0)