commit 905ed24b39020575a45bcd2e12ce5a99fe2f1b20
parent 3853c05fdf38876bc2259749552abd45a0de0c9a
Author: Tooru Fujisawa <arai_a@mac.com>
Date: Fri, 31 Oct 2025 05:22:08 +0000
Bug 1980154 - Part 2: Factor out the disk cache strategy into ScriptLoader::GetDiskCacheStrategy. r=nbp
Differential Revision: https://phabricator.services.mozilla.com/D270522
Diffstat:
2 files changed, 68 insertions(+), 53 deletions(-)
diff --git a/dom/script/ScriptLoader.cpp b/dom/script/ScriptLoader.cpp
@@ -2685,6 +2685,48 @@ nsresult ScriptLoader::FillCompileOptionsForRequest(
return NS_OK;
}
+/* static */
+ScriptLoader::DiskCacheStrategy ScriptLoader::GetDiskCacheStrategy() {
+ int32_t strategyPref =
+ StaticPrefs::dom_script_loader_bytecode_cache_strategy();
+ LOG(("Bytecode-cache: disk cache strategy = %d.", strategyPref));
+
+ DiskCacheStrategy strategy;
+ switch (strategyPref) {
+ case -2: {
+ strategy.mIsDisabled = true;
+ break;
+ }
+ case -1: {
+ // Eager mode, skip heuristics!
+ strategy.mHasSourceLengthMin = false;
+ strategy.mHasFetchCountMin = false;
+ break;
+ }
+ case 1: {
+ strategy.mHasSourceLengthMin = true;
+ strategy.mHasFetchCountMin = true;
+ strategy.mSourceLengthMin = 1024;
+ // fetchCountMin is optimized for speed in exchange for additional
+ // memory and cache use.
+ strategy.mFetchCountMin = 2;
+ break;
+ }
+ default:
+ case 0: {
+ strategy.mHasSourceLengthMin = true;
+ strategy.mHasFetchCountMin = true;
+ strategy.mSourceLengthMin = 1024;
+ // If we were to optimize only for speed, without considering the impact
+ // on memory, we should set this threshold to 2. (Bug 900784 comment 120)
+ strategy.mFetchCountMin = 4;
+ break;
+ }
+ }
+
+ return strategy;
+}
+
void ScriptLoader::CalculateCacheFlag(ScriptLoadRequest* aRequest) {
using mozilla::TimeDuration;
using mozilla::TimeStamp;
@@ -2762,71 +2804,32 @@ void ScriptLoader::CalculateCacheFlag(ScriptLoadRequest* aRequest) {
return;
}
- // Look at the preference to know which strategy (parameters) should be used
- // when the bytecode cache is enabled.
- int32_t strategy = StaticPrefs::dom_script_loader_bytecode_cache_strategy();
-
- // List of parameters used by the strategies.
- bool hasSourceLengthMin = false;
- bool hasFetchCountMin = false;
- size_t sourceLengthMin = 100;
- uint32_t fetchCountMin = 4;
+ auto strategy = GetDiskCacheStrategy();
- LOG(("ScriptLoadRequest (%p): Bytecode-cache: strategy = %d.", aRequest,
- strategy));
- switch (strategy) {
- case -2: {
- // Reader mode, keep requesting alternate data but no longer save it.
- LOG(
- ("ScriptLoadRequest (%p): Bytecode-cache: Skip disk: Disabled by "
- "pref.",
- aRequest));
- aRequest->MarkSkippedDiskCaching();
+ if (strategy.mIsDisabled) {
+ // Reader mode, keep requesting alternate data but no longer save it.
+ LOG(
+ ("ScriptLoadRequest (%p): Bytecode-cache: Skip disk: Disabled by "
+ "pref.",
+ aRequest));
+ aRequest->MarkSkippedDiskCaching();
- aRequest->getLoadedScript()->DropDiskCacheReferenceAndSRI();
- return;
- }
- case -1: {
- // Eager mode, skip heuristics!
- hasSourceLengthMin = false;
- hasFetchCountMin = false;
- break;
- }
- case 1: {
- hasSourceLengthMin = true;
- hasFetchCountMin = true;
- sourceLengthMin = 1024;
- // fetchCountMin is optimized for speed in exchange for additional
- // memory and cache use.
- fetchCountMin = 2;
- break;
- }
- default:
- case 0: {
- hasSourceLengthMin = true;
- hasFetchCountMin = true;
- sourceLengthMin = 1024;
- // If we were to optimize only for speed, without considering the impact
- // on memory, we should set this threshold to 2. (Bug 900784 comment 120)
- fetchCountMin = 4;
- break;
- }
+ aRequest->getLoadedScript()->DropDiskCacheReferenceAndSRI();
+ return;
}
// If the script is too small/large, do not attempt at creating a bytecode
// cache for this script, as the overhead of parsing it might not be worth the
// effort.
- if (hasSourceLengthMin) {
+ if (strategy.mHasSourceLengthMin) {
size_t sourceLength;
- size_t minLength;
if (aRequest->IsCachedStencil()) {
sourceLength = JS::GetScriptSourceLength(aRequest->GetStencil());
} else {
MOZ_ASSERT(aRequest->IsTextSource());
sourceLength = aRequest->ReceivedScriptTextLength();
}
- minLength = sourceLengthMin;
- if (sourceLength < minLength) {
+ if (sourceLength < strategy.mSourceLengthMin) {
LOG(
("ScriptLoadRequest (%p): Bytecode-cache: Skip disk: Script is too "
"small.",
@@ -2840,7 +2843,7 @@ void ScriptLoader::CalculateCacheFlag(ScriptLoadRequest* aRequest) {
// Check that we loaded the cache entry a few times before attempting any
// bytecode-cache optimization, such that we do not waste time on entry which
// are going to be dropped soon.
- if (hasFetchCountMin) {
+ if (strategy.mHasFetchCountMin) {
uint32_t fetchCount = 0;
if (aRequest->IsCachedStencil()) {
fetchCount = aRequest->mLoadedScript->mFetchCount;
@@ -2864,7 +2867,7 @@ void ScriptLoader::CalculateCacheFlag(ScriptLoadRequest* aRequest) {
}
LOG(("ScriptLoadRequest (%p): Bytecode-cache: fetchCount = %d.", aRequest,
fetchCount));
- if (fetchCount < fetchCountMin) {
+ if (fetchCount < strategy.mFetchCountMin) {
LOG(("ScriptLoadRequest (%p): Bytecode-cache: Skip disk: fetchCount",
aRequest));
aRequest->MarkSkippedDiskCaching();
diff --git a/dom/script/ScriptLoader.h b/dom/script/ScriptLoader.h
@@ -800,6 +800,18 @@ class ScriptLoader final : public JS::loader::ScriptLoaderInterface {
void MaybeMoveToLoadedList(ScriptLoadRequest* aRequest);
+ public:
+ struct DiskCacheStrategy {
+ bool mIsDisabled = false;
+ bool mHasSourceLengthMin = false;
+ bool mHasFetchCountMin = false;
+ uint8_t mFetchCountMin = 0;
+ size_t mSourceLengthMin = 0;
+ };
+
+ static DiskCacheStrategy GetDiskCacheStrategy();
+
+ private:
// Check whether the request should be saved to the following or not:
// * in-memory cache as Stencil
// * necko alternative stream as Stencil XDR