-
Notifications
You must be signed in to change notification settings - Fork 54
/
OffThreadScriptCompilation.cpp
211 lines (180 loc) · 8.46 KB
/
OffThreadScriptCompilation.cpp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "js/OffThreadScriptCompilation.h"
#include "mozilla/Assertions.h" // MOZ_ASSERT
#include "mozilla/Range.h" // mozilla::Range
#include "mozilla/Utf8.h" // mozilla::Utf8Unit
#include "mozilla/Vector.h" // mozilla::Vector
#include <stddef.h> // size_t
#include "jspubtd.h" // js::CurrentThreadCanAccessRuntime
#include "jstypes.h" // JS_PUBLIC_API
#include "js/CompileOptions.h" // JS::ReadOnlyCompileOptions
#include "vm/HelperThreads.h" // js::OffThreadParsingMustWaitForGC, js::StartOffThreadParseScript
#include "vm/JSContext.h" // JSContext
#include "vm/Runtime.h" // js::CanUseExtraThreads
using namespace js;
using mozilla::Utf8Unit;
using JS::ReadOnlyCompileOptions;
enum class OffThread { Compile, Decode, DecodeBinAST };
static bool CanDoOffThread(JSContext* cx, const ReadOnlyCompileOptions& options,
size_t length, OffThread what) {
static const size_t TINY_LENGTH = 5 * 1000;
static const size_t HUGE_SRC_LENGTH = 100 * 1000;
static const size_t HUGE_BC_LENGTH = 367 * 1000;
static const size_t HUGE_BINAST_LENGTH = 70 * 1000;
// These are heuristics which the caller may choose to ignore (e.g., for
// testing purposes).
if (!options.forceAsync) {
// Compiling off the main thread inolves creating a new Zone and other
// significant overheads. Don't bother if the script is tiny.
if (length < TINY_LENGTH) {
return false;
}
// If the parsing task would have to wait for GC to complete, it'll probably
// be faster to just start it synchronously on the main thread unless the
// script is huge.
if (OffThreadParsingMustWaitForGC(cx->runtime())) {
if (what == OffThread::Compile && length < HUGE_SRC_LENGTH) {
return false;
}
if (what == OffThread::Decode && length < HUGE_BC_LENGTH) {
return false;
}
if (what == OffThread::DecodeBinAST && length < HUGE_BINAST_LENGTH) {
return false;
}
}
}
return cx->runtime()->canUseParallelParsing() && CanUseExtraThreads() &&
!mozilla::recordreplay::IsRecordingOrReplaying();
}
JS_PUBLIC_API bool JS::CanCompileOffThread(
JSContext* cx, const ReadOnlyCompileOptions& options, size_t length) {
return CanDoOffThread(cx, options, length, OffThread::Compile);
}
JS_PUBLIC_API bool JS::CanDecodeOffThread(JSContext* cx,
const ReadOnlyCompileOptions& options,
size_t length) {
return CanDoOffThread(cx, options, length, OffThread::Decode);
}
#ifdef JS_BUILD_BINAST
JS_PUBLIC_API bool JS::CanDecodeBinASTOffThread(
JSContext* cx, const ReadOnlyCompileOptions& options, size_t length) {
return CanDoOffThread(cx, options, length, OffThread::DecodeBinAST);
}
#endif
JS_PUBLIC_API bool JS::CompileOffThread(JSContext* cx,
const ReadOnlyCompileOptions& options,
JS::SourceText<char16_t>& srcBuf,
OffThreadCompileCallback callback,
void* callbackData) {
MOZ_ASSERT(CanCompileOffThread(cx, options, srcBuf.length()));
return StartOffThreadParseScript(cx, options, srcBuf, callback, callbackData);
}
JS_PUBLIC_API bool JS::CompileOffThread(JSContext* cx,
const ReadOnlyCompileOptions& options,
JS::SourceText<Utf8Unit>& srcBuf,
OffThreadCompileCallback callback,
void* callbackData) {
MOZ_ASSERT(CanCompileOffThread(cx, options, srcBuf.length()));
return StartOffThreadParseScript(cx, options, srcBuf, callback, callbackData);
}
JS_PUBLIC_API JSScript* JS::FinishOffThreadScript(JSContext* cx,
JS::OffThreadToken* token) {
MOZ_ASSERT(cx);
MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime()));
return HelperThreadState().finishScriptParseTask(cx, token);
}
JS_PUBLIC_API void JS::CancelOffThreadScript(JSContext* cx,
JS::OffThreadToken* token) {
MOZ_ASSERT(cx);
MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime()));
HelperThreadState().cancelParseTask(cx->runtime(), ParseTaskKind::Script,
token);
}
JS_PUBLIC_API bool JS::CompileOffThreadModule(
JSContext* cx, const ReadOnlyCompileOptions& options,
JS::SourceText<char16_t>& srcBuf, OffThreadCompileCallback callback,
void* callbackData) {
MOZ_ASSERT(CanCompileOffThread(cx, options, srcBuf.length()));
return StartOffThreadParseModule(cx, options, srcBuf, callback, callbackData);
}
JS_PUBLIC_API bool JS::CompileOffThreadModule(
JSContext* cx, const ReadOnlyCompileOptions& options,
JS::SourceText<Utf8Unit>& srcBuf, OffThreadCompileCallback callback,
void* callbackData) {
MOZ_ASSERT(CanCompileOffThread(cx, options, srcBuf.length()));
return StartOffThreadParseModule(cx, options, srcBuf, callback, callbackData);
}
JS_PUBLIC_API JSObject* JS::FinishOffThreadModule(JSContext* cx,
JS::OffThreadToken* token) {
MOZ_ASSERT(cx);
MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime()));
return HelperThreadState().finishModuleParseTask(cx, token);
}
JS_PUBLIC_API void JS::CancelOffThreadModule(JSContext* cx,
JS::OffThreadToken* token) {
MOZ_ASSERT(cx);
MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime()));
HelperThreadState().cancelParseTask(cx->runtime(), ParseTaskKind::Module,
token);
}
JS_PUBLIC_API bool JS::DecodeOffThreadScript(
JSContext* cx, const ReadOnlyCompileOptions& options,
mozilla::Vector<uint8_t>& buffer /* TranscodeBuffer& */, size_t cursor,
OffThreadCompileCallback callback, void* callbackData) {
JS::TranscodeRange range(buffer.begin() + cursor, buffer.length() - cursor);
MOZ_ASSERT(CanDecodeOffThread(cx, options, range.length()));
return StartOffThreadDecodeScript(cx, options, range, callback, callbackData);
}
JS_PUBLIC_API bool JS::DecodeOffThreadScript(
JSContext* cx, const ReadOnlyCompileOptions& options,
const mozilla::Range<uint8_t>& range /* TranscodeRange& */,
OffThreadCompileCallback callback, void* callbackData) {
MOZ_ASSERT(CanDecodeOffThread(cx, options, range.length()));
return StartOffThreadDecodeScript(cx, options, range, callback, callbackData);
}
JS_PUBLIC_API bool JS::DecodeMultiOffThreadScripts(
JSContext* cx, const ReadOnlyCompileOptions& options,
TranscodeSources& sources, OffThreadCompileCallback callback,
void* callbackData) {
#ifdef DEBUG
size_t length = 0;
for (auto& source : sources) {
length += source.range.length();
}
MOZ_ASSERT(CanCompileOffThread(cx, options, length));
#endif
return StartOffThreadDecodeMultiScripts(cx, options, sources, callback,
callbackData);
}
JS_PUBLIC_API JSScript* JS::FinishOffThreadScriptDecoder(
JSContext* cx, JS::OffThreadToken* token) {
MOZ_ASSERT(cx);
MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime()));
return HelperThreadState().finishScriptDecodeTask(cx, token);
}
JS_PUBLIC_API void JS::CancelOffThreadScriptDecoder(JSContext* cx,
JS::OffThreadToken* token) {
MOZ_ASSERT(cx);
MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime()));
HelperThreadState().cancelParseTask(cx->runtime(),
ParseTaskKind::ScriptDecode, token);
}
JS_PUBLIC_API bool JS::FinishMultiOffThreadScriptsDecoder(
JSContext* cx, JS::OffThreadToken* token,
MutableHandle<ScriptVector> scripts) {
MOZ_ASSERT(cx);
MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime()));
return HelperThreadState().finishMultiScriptsDecodeTask(cx, token, scripts);
}
JS_PUBLIC_API void JS::CancelMultiOffThreadScriptsDecoder(
JSContext* cx, JS::OffThreadToken* token) {
MOZ_ASSERT(cx);
MOZ_ASSERT(CurrentThreadCanAccessRuntime(cx->runtime()));
HelperThreadState().cancelParseTask(cx->runtime(),
ParseTaskKind::MultiScriptsDecode, token);
}