Bug 1776205 - Set the delazify option in all ScriptLoader code paths. r=arai

This patch move the ShouldApplyDelazificationStartegy as well as the
ApplyDelazificationStrategy under FillCompileOptionsForRequest.

As not all delazification strategies are capable of handling modules yet (Bug
1760334), and do not apply to cached-stencil, we have to add extra filters to
prevent ShouldApplyDelazificationStrategy to access custom delazification mode.

Differential Revision: https://phabricator.services.mozilla.com/D150118
This commit is contained in:
Nicolas B. Pierron 2022-07-08 13:19:30 +00:00
parent ff4d8a94ee
commit 49f29dc411
2 changed files with 55 additions and 16 deletions

View File

@ -1679,20 +1679,6 @@ nsresult ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest,
} else {
MOZ_ASSERT(aRequest->IsTextSource());
if (ShouldApplyDelazifyStrategy(aRequest)) {
ApplyDelazifyStrategy(&options);
mTotalFullParseSize +=
aRequest->ScriptTextLength() > 0
? static_cast<uint32_t>(aRequest->ScriptTextLength())
: 0;
LOG(
("ScriptLoadRequest (%p): non-on-demand-only Parsing Enabled for "
"url=%s mTotalFullParseSize=%u",
aRequest, aRequest->mURI->GetSpecOrDefault().get(),
mTotalFullParseSize));
}
MaybeSourceText maybeSource;
nsresult rv = aRequest->GetScriptSource(cx, &maybeSource);
NS_ENSURE_SUCCESS(rv, rv);
@ -2020,6 +2006,24 @@ nsresult ScriptLoader::FillCompileOptionsForRequest(
aOptions->allocateInstantiationStorage = true;
if (aOptions->forceFullParse()) {
// If code coverage is enabled, full-parsing is non revertable and asserts
// if any attempt to do so is made. Thus we should skip attempts to apply a
// different delazification strategy.
} else if (ShouldApplyDelazifyStrategy(aRequest)) {
ApplyDelazifyStrategy(aRequest, aOptions);
mTotalFullParseSize +=
aRequest->ScriptTextLength() > 0
? static_cast<uint32_t>(aRequest->ScriptTextLength())
: 0;
} else {
// If we cannot apply the delazification strategy set in the preference, due
// to failure of the preconditions, then we default to the strategy which
// minimize the memory impact at runtime.
aOptions->setEagerDelazificationStrategy(
JS::DelazificationOption::OnDemandOnly);
}
return NS_OK;
}
@ -3254,6 +3258,26 @@ static bool IsInternalURIScheme(nsIURI* uri) {
bool ScriptLoader::ShouldApplyDelazifyStrategy(ScriptLoadRequest* aRequest) {
// Full parse everything if negative.
if (!aRequest->IsTextSource()) {
// Delazification strategy is only used while processing source input, as
// the bytecode cache already contains delazified functions.
return false;
}
if (aRequest->IsModuleRequest()) {
// Module do not yet support concurrent off-thread delazification.
// (see Bug 1760334)
return false;
}
auto logEnabled = MakeScopeExit([&] {
LOG(
("ScriptLoadRequest (%p): non-on-demand-only Parsing Enabled for "
"url=%s mTotalFullParseSize=%u",
aRequest, aRequest->mURI->GetSpecOrDefault().get(),
mTotalFullParseSize));
});
if (StaticPrefs::dom_script_loader_delazification_max_size() < 0) {
return true;
}
@ -3261,6 +3285,7 @@ bool ScriptLoader::ShouldApplyDelazifyStrategy(ScriptLoadRequest* aRequest) {
// Be conservative on machines with 2GB or less of memory.
if (PhysicalSizeOfMemoryInGB() <=
StaticPrefs::dom_script_loader_delazification_min_mem()) {
logEnabled.release();
return false;
}
@ -3275,6 +3300,7 @@ bool ScriptLoader::ShouldApplyDelazifyStrategy(ScriptLoadRequest* aRequest) {
return true;
}
logEnabled.release();
if (LOG_ENABLED()) {
nsCString url = aRequest->mURI->GetSpecOrDefault();
LOG(
@ -3286,7 +3312,8 @@ bool ScriptLoader::ShouldApplyDelazifyStrategy(ScriptLoadRequest* aRequest) {
return false;
}
void ScriptLoader::ApplyDelazifyStrategy(JS::CompileOptions* aOptions) {
void ScriptLoader::ApplyDelazifyStrategy(ScriptLoadRequest* aRequest,
JS::CompileOptions* aOptions) {
JS::DelazificationOption strategy =
JS::DelazificationOption::ParseEverythingEagerly;
uint32_t strategyIndex =
@ -3315,6 +3342,17 @@ void ScriptLoader::ApplyDelazifyStrategy(JS::CompileOptions* aOptions) {
strategy = JS::DelazificationOption(uint8_t(strategyIndex));
}
// When using inline script, we want to minimize time spent parsing before
// running the script and thus forbid choosing the eager delazification
// strategy. It might still be selected by others means such as through code
// coverage.
if (aRequest->HasScriptLoadContext() &&
aRequest->GetScriptLoadContext()->mIsInline) {
if (strategy == JS::DelazificationOption::ParseEverythingEagerly) {
strategy = JS::DelazificationOption::OnDemandOnly;
}
}
aOptions->setEagerDelazificationStrategy(strategy);
}

View File

@ -655,7 +655,8 @@ class ScriptLoader final : public JS::loader::ScriptLoaderInterface {
bool MaybeRemovedDeferRequests();
bool ShouldApplyDelazifyStrategy(ScriptLoadRequest* aRequest);
void ApplyDelazifyStrategy(JS::CompileOptions* aOptions);
void ApplyDelazifyStrategy(ScriptLoadRequest* aRequest,
JS::CompileOptions* aOptions);
bool ShouldCompileOffThread(ScriptLoadRequest* aRequest);