Repository navigation
Expand file tree
/
Copy pathCodeCacheQueue.cpp
More file actions
173 lines (153 loc) · 5.12 KB
/
Copy pathCodeCacheQueue.cpp
File metadata and controls
173 lines (153 loc) · 5.12 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "ns/loader/CodeCacheQueue.h"
#include <chrono>
#include <cstdint>
#include <cstring>
#include <deque>
#include <memory>
#include <utility>
#include "ns/IsolateState.h"
#include "ns/Loop.h"
namespace nativescript::common::loader {
namespace {
using Clock = std::chrono::steady_clock;
// After the last queued cache, so a burst of loads (boot, a lazy route)
// finishes before any creation competes with it.
constexpr std::chrono::duration<double, std::milli> kQuietPeriod{500};
// Between two creations, so each is a short task of its own and the loop runs
// whatever else is due in between.
constexpr double kCreationGapMs = 16;
struct PendingCodeCache {
// Exactly one of the two is set.
v8::Global<v8::UnboundModuleScript> module;
v8::Global<v8::UnboundScript> script;
CodeCacheEntry entry;
};
// Isolate thread only.
struct CodeCacheQueueState {
std::deque<PendingCodeCache> pending;
Clock::time_point lastQueuedAt;
// At most one creation task is posted at a time.
bool taskPosted = false;
};
void CreateAndPostWrite(v8::Isolate* isolate, PendingCodeCache& item) {
v8::HandleScope handleScope(isolate);
v8::ScriptCompiler::CachedData* cache = nullptr;
if (!item.module.IsEmpty()) {
cache = v8::ScriptCompiler::CreateCodeCache(item.module.Get(isolate));
} else if (!item.script.IsEmpty()) {
cache = v8::ScriptCompiler::CreateCodeCache(item.script.Get(isolate));
}
item.module.Reset();
item.script.Reset();
// Null for a script V8 declines to serialize.
if (cache == nullptr) {
return;
}
const size_t length =
cache->length > 0 ? static_cast<size_t>(cache->length) : 0;
std::unique_ptr<uint8_t[]> payload;
if (length > 0 &&
cache->buffer_policy == v8::ScriptCompiler::CachedData::BufferOwned) {
// An owned buffer was allocated with new[]; the writer takes it over.
payload.reset(const_cast<uint8_t*>(cache->data));
cache->buffer_policy = v8::ScriptCompiler::CachedData::BufferNotOwned;
} else if (length > 0) {
payload.reset(new uint8_t[length]);
std::memcpy(payload.get(), cache->data, length);
}
delete cache;
if (payload != nullptr) {
PostCodeCacheWrite(std::move(item.entry), std::move(payload), length);
}
}
void PostCreationTask(v8::Isolate* isolate, double delayMs);
// Creates one cache once loading has been quiet for kQuietPeriod, then posts
// the next.
void RunCreationTask(v8::Isolate* isolate) {
auto* queue = IsolateState::ForExisting<CodeCacheQueueState>(isolate);
if (queue == nullptr) {
return;
}
queue->taskPosted = false;
if (queue->pending.empty()) {
return;
}
const std::chrono::duration<double, std::milli> quietLeft =
kQuietPeriod - (Clock::now() - queue->lastQueuedAt);
if (quietLeft.count() > 0) {
PostCreationTask(isolate, quietLeft.count());
return;
}
PendingCodeCache item = std::move(queue->pending.front());
queue->pending.pop_front();
CreateAndPostWrite(isolate, item);
if (!queue->pending.empty()) {
PostCreationTask(isolate, kCreationGapMs);
}
}
void PostCreationTask(v8::Isolate* isolate, double delayMs) {
auto* queue = IsolateState::ForExisting<CodeCacheQueueState>(isolate);
if (queue == nullptr || queue->taskPosted) {
return;
}
std::shared_ptr<Loop> loop = platform::LoopFor(isolate);
if (loop == nullptr) {
return;
}
queue->taskPosted = loop->PostInternalDelayed(
[isolate]() { RunCreationTask(isolate); }, delayMs);
}
void Enqueue(v8::Isolate* isolate, PendingCodeCache item) {
auto* queue = IsolateState::For<CodeCacheQueueState>(isolate);
if (queue == nullptr) {
return;
}
queue->pending.push_back(std::move(item));
queue->lastQueuedAt = Clock::now();
PostCreationTask(isolate, kQuietPeriod.count());
}
void FlushCodeCaches(v8::Isolate* isolate) {
auto* queue = IsolateState::ForExisting<CodeCacheQueueState>(isolate);
if (queue == nullptr) {
return;
}
while (!queue->pending.empty()) {
PendingCodeCache item = std::move(queue->pending.front());
queue->pending.pop_front();
CreateAndPostWrite(isolate, item);
}
}
} // namespace
void QueueCodeCache(v8::Isolate* isolate,
v8::Local<v8::UnboundModuleScript> script,
CodeCacheEntry entry) {
PendingCodeCache item;
item.module.Reset(isolate, script);
item.entry = std::move(entry);
Enqueue(isolate, std::move(item));
}
void QueueCodeCache(v8::Isolate* isolate, v8::Local<v8::UnboundScript> script,
CodeCacheEntry entry) {
PendingCodeCache item;
item.script.Reset(isolate, script);
item.entry = std::move(entry);
Enqueue(isolate, std::move(item));
}
void NotifyAppBackgrounded(const std::shared_ptr<RuntimeLiveness>& liveness) {
if (liveness == nullptr) {
return;
}
v8::Isolate* isolate = liveness->isolate();
liveness->Post([isolate] {
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
FlushCodeCaches(isolate);
});
}
void NotifyAppBackgrounded(v8::Isolate* isolate) {
std::shared_ptr<Loop> loop = platform::LoopFor(isolate);
if (loop != nullptr) {
loop->PostInternal([isolate]() { FlushCodeCaches(isolate); });
}
}
} // namespace nativescript::common::loader