V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
read-only-serializer.cc
1 // Copyright 2018 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/snapshot/read-only-serializer.h"
6 
7 #include "src/api.h"
8 #include "src/code-tracer.h"
9 #include "src/global-handles.h"
10 #include "src/objects-inl.h"
11 #include "src/objects/slots.h"
12 #include "src/snapshot/startup-serializer.h"
13 #include "src/v8threads.h"
14 
15 namespace v8 {
16 namespace internal {
17 
18 ReadOnlySerializer::ReadOnlySerializer(Isolate* isolate)
19  : RootsSerializer(isolate, RootIndex::kFirstReadOnlyRoot) {
20  STATIC_ASSERT(RootIndex::kFirstReadOnlyRoot == RootIndex::kFirstRoot);
21 }
22 
23 ReadOnlySerializer::~ReadOnlySerializer() {
24  OutputStatistics("ReadOnlySerializer");
25 }
26 
27 void ReadOnlySerializer::SerializeObject(HeapObject* obj, HowToCode how_to_code,
28  WhereToPoint where_to_point,
29  int skip) {
30  CHECK(isolate()->heap()->read_only_space()->Contains(obj));
31 
32  if (SerializeHotObject(obj, how_to_code, where_to_point, skip)) return;
33  if (IsRootAndHasBeenSerialized(obj) &&
34  SerializeRoot(obj, how_to_code, where_to_point, skip)) {
35  return;
36  }
37  if (SerializeBackReference(obj, how_to_code, where_to_point, skip)) return;
38 
39  FlushSkip(skip);
40 
41  CheckRehashability(obj);
42 
43  // Object has not yet been serialized. Serialize it here.
44  ObjectSerializer object_serializer(this, obj, &sink_, how_to_code,
45  where_to_point);
46  object_serializer.Serialize();
47 }
48 
49 void ReadOnlySerializer::SerializeReadOnlyRoots() {
50  // No active threads.
51  CHECK_NULL(isolate()->thread_manager()->FirstThreadStateInUse());
52  // No active or weak handles.
53  CHECK(isolate()->handle_scope_implementer()->blocks()->empty());
54 
55  ReadOnlyRoots(isolate()).Iterate(this);
56 }
57 
58 void ReadOnlySerializer::FinalizeSerialization() {
59  // This comes right after serialization of the other snapshots, where we
60  // add entries to the read-only object cache. Add one entry with 'undefined'
61  // to terminate the read-only object cache.
62  Object* undefined = ReadOnlyRoots(isolate()).undefined_value();
63  VisitRootPointer(Root::kReadOnlyObjectCache, nullptr, ObjectSlot(&undefined));
64  SerializeDeferredObjects();
65  Pad();
66 }
67 
68 bool ReadOnlySerializer::MustBeDeferred(HeapObject* object) {
69  if (root_has_been_serialized(RootIndex::kFreeSpaceMap) &&
70  root_has_been_serialized(RootIndex::kOnePointerFillerMap) &&
71  root_has_been_serialized(RootIndex::kTwoPointerFillerMap)) {
72  // All required root objects are serialized, so any aligned objects can
73  // be saved without problems.
74  return false;
75  }
76  // Just defer everything except for Map objects until all required roots are
77  // serialized. Some objects may have special alignment requirements, that may
78  // not be fulfilled during deserialization until few first root objects are
79  // serialized. But we must serialize Map objects since deserializer checks
80  // that these root objects are indeed Maps.
81  return !object->IsMap();
82 }
83 
84 bool ReadOnlySerializer::SerializeUsingReadOnlyObjectCache(
85  SnapshotByteSink* sink, HeapObject* obj, HowToCode how_to_code,
86  WhereToPoint where_to_point, int skip) {
87  if (!isolate()->heap()->read_only_space()->Contains(obj)) return false;
88 
89  // Get the cache index and serialize it into the read-only snapshot if
90  // necessary.
91  int cache_index = SerializeInObjectCache(obj);
92 
93  // Writing out the cache entry into the calling serializer's sink.
94  FlushSkip(sink, skip);
95  sink->Put(kReadOnlyObjectCache + how_to_code + where_to_point,
96  "ReadOnlyObjectCache");
97  sink->PutInt(cache_index, "read_only_object_cache_index");
98 
99  return true;
100 }
101 
102 } // namespace internal
103 } // namespace v8
Definition: libplatform.h:13