commit 14761bfc837dc973621780a099534f51116d2c44
parent 478fa236add460a7f3901a6072270743fe4d81e0
Author: Tooru Fujisawa <arai_a@mac.com>
Date: Thu, 16 Oct 2025 09:03:23 +0000
Bug 1991081 - Part 5: Use single field for SRI and optional bytecode, for bytecode case and for cached stencil case. r=nbp
Differential Revision: https://phabricator.services.mozilla.com/D266615
Diffstat:
4 files changed, 37 insertions(+), 68 deletions(-)
diff --git a/dom/script/ScriptLoader.cpp b/dom/script/ScriptLoader.cpp
@@ -1212,18 +1212,6 @@ void ScriptLoader::TryUseCache(ScriptLoadRequest* aRequest,
return;
}
-void ScriptLoader::StoreCacheInfo(LoadedScript* aLoadedScript,
- ScriptLoadRequest* aRequest) {
- MOZ_ASSERT(aRequest->getLoadedScript()->HasDiskCacheReference());
- MOZ_ASSERT(!aRequest->SRIAndBytecode().empty());
- MOZ_ASSERT(aRequest->SRIAndBytecode().length() == aRequest->GetSRILength());
- MOZ_ASSERT(aLoadedScript->mSRI.empty());
-
- if (!aLoadedScript->mSRI.appendAll(aRequest->SRIAndBytecode())) {
- return;
- }
-}
-
void ScriptLoader::EmulateNetworkEvents(ScriptLoadRequest* aRequest) {
MOZ_ASSERT(aRequest->IsCachedStencil());
MOZ_ASSERT(aRequest->mNetworkMetadata);
@@ -3307,9 +3295,6 @@ void ScriptLoader::TryCacheRequest(ScriptLoadRequest* aRequest) {
if (cacheBehavior == CacheBehavior::Insert) {
auto loadData = MakeRefPtr<ScriptLoadData>(this, aRequest);
- if (aRequest->getLoadedScript()->HasDiskCacheReference()) {
- StoreCacheInfo(aRequest->getLoadedScript(), aRequest);
- }
mCache->Insert(*loadData);
LOG(("ScriptLoader (%p): Inserting in-memory cache for %s.", this,
aRequest->mURI->GetSpecOrDefault().get()));
@@ -3664,38 +3649,25 @@ void ScriptLoader::UpdateCache() {
if (request->IsMarkedForDiskCache() &&
request->getLoadedScript()->HasDiskCacheReference()) {
- if (request->getLoadedScript()->mSRI.empty()) {
- // This is not a cached stencil.
- //
- // The nsICacheInfoChannel is stored when the this request
- // receives a source text (See ScriptLoadHandler::OnStreamComplete),
- // and also the SRI is calculated only in that case.
- MOZ_ASSERT(request->SRIAndBytecode().length() ==
- request->GetSRILength());
-
- EncodeBytecodeAndSave(aes.cx(), request,
- request->getLoadedScript()->mCacheInfo,
- BytecodeMimeTypeFor(request),
- request->SRIAndBytecode(), stencil);
-
- request->DropBytecode();
- } else {
- // This is cached stencil.
- //
- // The nsICacheInfoChannel is stored when the cached request
- // received a source text (See ScriptLoadHandler::OnStreamComplete),
- // and also the SRI is calculated and stored into the cache only in
- // that case.
- EncodeBytecodeAndSave(aes.cx(), request,
- request->getLoadedScript()->mCacheInfo,
- BytecodeMimeTypeFor(request),
- request->getLoadedScript()->mSRI, stencil);
-
- // The cached nsICacheInfoChannel can be used only once.
- // We don't overwrite the bytecode cache.
- request->getLoadedScript()->DropDiskCacheReference();
- request->getLoadedScript()->mSRI.clear();
- }
+ // If this is not a cached stencil, the nsICacheInfoChannel is stored
+ // when the this request receives a source text
+ // (See ScriptLoadHandler::OnStreamComplete),
+ // and also the SRI is calculated only in that case.
+ //
+ // If this is cached stencil, The nsICacheInfoChannel is stored when
+ // the cached request received a source text
+ // (See ScriptLoadHandler::OnStreamComplete),
+ // and also the SRI is calculated and stored into the cache only in
+ // that case.
+ EncodeBytecodeAndSave(
+ aes.cx(), request, request->getLoadedScript()->mCacheInfo,
+ BytecodeMimeTypeFor(request), request->SRIAndBytecode(), stencil);
+
+ // Even if this is cached stencil, the cached nsICacheInfoChannel can
+ // be used only once. We don't overwrite the bytecode cache.
+ request->getLoadedScript()->DropDiskCacheReference();
+
+ request->DropBytecode();
}
}
request->getLoadedScript()->DropDiskCacheReference();
diff --git a/dom/script/ScriptLoader.h b/dom/script/ScriptLoader.h
@@ -784,9 +784,6 @@ class ScriptLoader final : public JS::loader::ScriptLoaderInterface {
const JS::TranscodeBuffer& aSRI,
JS::Stencil* aStencil);
- void StoreCacheInfo(JS::loader::LoadedScript* aLoadedScript,
- ScriptLoadRequest* aRequest);
-
/**
* Stop collecting delazifications for all requests.
* This should be used when the ScriptLoader is getting destroyed, or
diff --git a/js/loader/LoadedScript.cpp b/js/loader/LoadedScript.cpp
@@ -65,7 +65,7 @@ LoadedScript::LoadedScript(const LoadedScript& aOther)
MOZ_DIAGNOSTIC_ASSERT(aOther.mDataType == DataType::eCachedStencil);
MOZ_DIAGNOSTIC_ASSERT(mStencil);
MOZ_ASSERT(!mScriptData);
- MOZ_ASSERT(mScriptBytecode.empty());
+ MOZ_ASSERT(mSRIAndBytecode.empty());
}
LoadedScript::~LoadedScript() {
@@ -118,7 +118,7 @@ size_t LoadedScript::SizeOfIncludingThis(
}
}
- bytes += mScriptBytecode.sizeOfExcludingThis(aMallocSizeOf);
+ bytes += mSRIAndBytecode.sizeOfExcludingThis(aMallocSizeOf);
// NOTE: Stencil is reported by SpiderMonkey.
return bytes;
diff --git a/js/loader/LoadedScript.h b/js/loader/LoadedScript.h
@@ -124,17 +124,17 @@ class LoadedScript : public nsIMemoryReporter {
// This script is received as a plain text from the channel.
// mScriptData holds the text source, and mStencil holds the compiled
// stencil.
- // mScriptBytecode holds the SRI.
+ // mSRIAndBytecode holds the SRI.
eTextSource,
// This script is received as a bytecode from the channel.
- // mScriptBytecode holds the SRI and the bytecode, and mStencil holds the
+ // mSRIAndBytecode holds the SRI and the bytecode, and mStencil holds the
// decoded stencil.
eBytecode,
// This script is cached from the previous load.
- // mStencil holds the cached stencil, and mSRI holds the SRI.
- // mScriptData and mScriptBytecode are unused.
+ // mStencil holds the cached stencil, and mSRIAndBytecode holds the SRI.
+ // mScriptData is unused.
eCachedStencil
};
@@ -231,11 +231,11 @@ class LoadedScript : public nsIMemoryReporter {
// as we want to be able to save the bytecode content when we are loading
// from source.
MOZ_ASSERT(CanHaveBytecode());
- return mScriptBytecode;
+ return mSRIAndBytecode;
}
TranscodeRange Bytecode() const {
MOZ_ASSERT(IsBytecode());
- const auto& bytecode = mScriptBytecode;
+ const auto& bytecode = mSRIAndBytecode;
auto offset = mBytecodeOffset;
return TranscodeRange(bytecode.begin() + offset,
bytecode.length() - offset);
@@ -252,7 +252,7 @@ class LoadedScript : public nsIMemoryReporter {
void DropBytecode() {
MOZ_ASSERT(CanHaveBytecode());
- mScriptBytecode.clearAndFree();
+ mSRIAndBytecode.clearAndFree();
}
bool HasStencil() const { return mStencil; }
@@ -281,7 +281,7 @@ class LoadedScript : public nsIMemoryReporter {
public:
// Fields.
- // Determine whether the mScriptData or mScriptBytecode is used.
+ // Determine whether the mScriptData or mSRIAndBytecode is used.
// See DataType description for more info.
DataType mDataType;
@@ -298,7 +298,7 @@ class LoadedScript : public nsIMemoryReporter {
mozilla::dom::ReferrerPolicy mReferrerPolicy;
public:
- // Offset of the bytecode in mScriptBytecode.
+ // Offset of the bytecode in mSRIAndBytecode.
uint32_t mBytecodeOffset;
private:
@@ -316,10 +316,13 @@ class LoadedScript : public nsIMemoryReporter {
// since mScriptData is cleared when the source is passed to the JS engine.
size_t mReceivedScriptTextLength;
- // Holds the SRI serialized hash and the script bytecode for non-inline
- // scripts. The data is laid out according to ScriptBytecodeDataLayout
- // or, if compression is enabled, ScriptBytecodeCompressedDataLayout.
- TranscodeBuffer mScriptBytecode;
+ // Holds either of the following for non-inline scripts:
+ // * The SRI serialized hash and the paddings, which is calculated when
+ // receiving the source text
+ // * The SRI, padding, and the script bytecode, which is received
+ // from necko. The data is laid out according to ScriptBytecodeDataLayout
+ // or, if compression is enabled, ScriptBytecodeCompressedDataLayout.
+ TranscodeBuffer mSRIAndBytecode;
// Holds the stencil for the script. This field is used in all DataType.
RefPtr<Stencil> mStencil;
@@ -330,9 +333,6 @@ class LoadedScript : public nsIMemoryReporter {
// IsTextSource() or IsCachedStencil(), and it's cleared after saving the
// bytecode (Thus, used only once).
nsCOMPtr<nsICacheInfoChannel> mCacheInfo;
-
- // The SRI data and the padding, used when saving the bytecode.
- JS::TranscodeBuffer mSRI;
};
// Provide accessors for any classes `Derived` which is providing the