Skip to content

Commit

Permalink
Bug 1865929: Apply delazify strategy to small scripts in ScriptLoader…
Browse files Browse the repository at this point in the history
…. r=nbp

Differential Revision: https://phabricator.services.mozilla.com/D194647
  • Loading branch information
dpalmeiro committed Nov 24, 2023
1 parent f6ab1b6 commit 596fcfc
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions dom/script/ScriptLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1713,6 +1713,14 @@ class OffThreadCompilationCompleteTask : public Task {

} /* anonymous namespace */

// TODO: This uses the same heuristics and the same threshold as the
// JS::CanCompileOffThread / JS::CanDecodeOffThread APIs, but the
// heuristics needs to be updated to reflect the change regarding the
// Stencil API, and also the thread management on the consumer side
// (bug 1846160).
static constexpr size_t OffThreadMinimumTextLength = 5 * 1000;
static constexpr size_t OffThreadMinimumBytecodeLength = 5 * 1000;

nsresult ScriptLoader::AttemptOffThreadScriptCompile(
ScriptLoadRequest* aRequest, bool* aCouldCompileOut) {
// If speculative parsing is enabled, the request may not be ready to run if
Expand Down Expand Up @@ -1749,14 +1757,6 @@ nsresult ScriptLoader::AttemptOffThreadScriptCompile(
return rv;
}

// TODO: This uses the same heuristics and the same threshold as the
// JS::CanCompileOffThread / JS::CanDecodeOffThread APIs, but the
// heuristics needs to be updated to reflect the change regarding the
// Stencil API, and also the thread management on the consumer side
// (bug 1846160).
static constexpr size_t OffThreadMinimumTextLength = 5 * 1000;
static constexpr size_t OffThreadMinimumBytecodeLength = 5 * 1000;

if (aRequest->IsTextSource()) {
if (!StaticPrefs::javascript_options_parallel_parsing() ||
aRequest->ScriptTextLength() < OffThreadMinimumTextLength) {
Expand Down Expand Up @@ -2048,8 +2048,8 @@ nsresult ScriptLoader::CreateOffThreadTask(
: 0;

LOG(
("ScriptLoadRequest (%p): non-on-demand-only Parsing Enabled for "
"url=%s mTotalFullParseSize=%u",
("ScriptLoadRequest (%p): non-on-demand-only (omt) Parsing Enabled "
"for url=%s mTotalFullParseSize=%u",
aRequest, aRequest->mURI->GetSpecOrDefault().get(),
mTotalFullParseSize));
}
Expand Down Expand Up @@ -2763,6 +2763,23 @@ nsresult ScriptLoader::EvaluateScript(nsIGlobalObject* aGlobalObject,
return rv;
}

// Apply the delazify strategy if the script is small.
if (aRequest->IsTextSource() &&
aRequest->ScriptTextLength() < OffThreadMinimumTextLength &&
ShouldApplyDelazifyStrategy(aRequest)) {
ApplyDelazifyStrategy(&options);
mTotalFullParseSize +=
aRequest->ScriptTextLength() > 0
? static_cast<uint32_t>(aRequest->ScriptTextLength())
: 0;

LOG(
("ScriptLoadRequest (%p): non-on-demand-only (non-omt) Parsing Enabled "
"for url=%s mTotalFullParseSize=%u",
aRequest, aRequest->mURI->GetSpecOrDefault().get(),
mTotalFullParseSize));
}

TRACE_FOR_TEST(aRequest->GetScriptLoadContext()->GetScriptElement(),
"scriptloader_execute");
JS::Rooted<JSObject*> global(cx, aGlobalObject->GetGlobalJSObject());
Expand Down

0 comments on commit 596fcfc

Please sign in to comment.