shared-heap-deserializer.cc 2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
// Copyright 2021 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/snapshot/shared-heap-deserializer.h"

#include "src/heap/heap-inl.h"
#include "src/snapshot/shared-heap-serializer.h"

namespace v8 {
namespace internal {

void SharedHeapDeserializer::DeserializeIntoIsolate() {
  // Don't deserialize into client Isolates. If there are client Isolates, the
  // shared heap object cache should already be populated.
  DCHECK_IMPLIES(isolate()->shared_isolate() != nullptr,
                 !isolate()->shared_heap_object_cache()->empty());
  if (isolate()->shared_isolate() != nullptr) return;
  DCHECK(isolate()->shared_heap_object_cache()->empty());
  HandleScope scope(isolate());

  IterateSharedHeapObjectCache(isolate(), this);
  DeserializeStringTable();
  DeserializeDeferredObjects();

  if (FLAG_rehash_snapshot && can_rehash()) {
    // Hash seed was initialized in ReadOnlyDeserializer.
    Rehash();
  }
}

void SharedHeapDeserializer::DeserializeStringTable() {
  // See SharedHeapSerializer::SerializeStringTable.

  DCHECK(isolate()->OwnsStringTable());

  // Get the string table size.
  int string_table_size = source()->GetInt();

  // Add each string to the Isolate's string table.
  // TODO(leszeks): Consider pre-sizing the string table.
  for (int i = 0; i < string_table_size; ++i) {
    Handle<String> string = Handle<String>::cast(ReadObject());
    StringTableInsertionKey key(
        isolate(), string,
        DeserializingUserCodeOption::kNotDeserializingUserCode);
    Handle<String> result =
        isolate()->string_table()->LookupKey(isolate(), &key);

    // Since this is startup, there should be no duplicate entries in the
    // string table, and the lookup should unconditionally add the given
    // string.
    DCHECK_EQ(*result, *string);
    USE(result);
  }

  DCHECK_EQ(string_table_size, isolate()->string_table()->NumberOfElements());
}

}  // namespace internal
}  // namespace v8