Bug 1382329: Part 2 - Eagerly encode scripts for the startup cache. r=erahm,nbp
authorKris Maglione <maglione.k@gmail.com>
Wed, 19 Jul 2017 12:44:17 -0700
changeset 422125 e7e1331a26642f19832142a29eae4d2bb052dd75
parent 422124 d46324978a6db2a584a17c2ddfaba0caba8dec3b
child 422126 e1c94b661c79eee24f65a1fc0c122287ac527b3b
push id1517
push userjlorenzo@mozilla.com
push dateThu, 14 Sep 2017 16:50:54 +0000
treeherdermozilla-release@3b41fd564418 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerserahm, nbp
bugs1382329
milestone56.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1382329: Part 2 - Eagerly encode scripts for the startup cache. r=erahm,nbp MozReview-Commit-ID: JqCqQZ2rO2z
js/xpconnect/loader/ScriptPreloader.cpp
--- a/js/xpconnect/loader/ScriptPreloader.cpp
+++ b/js/xpconnect/loader/ScriptPreloader.cpp
@@ -543,18 +543,16 @@ ScriptPreloader::PrepareCacheWriteIntern
 
         if (!(script->mProcessTypes == script->mOriginalProcessTypes)) {
             // Note: EnumSet doesn't support operator!=, hence the weird form above.
             found = true;
         }
 
         if (!script->mSize && !script->XDREncode(jsapi.cx())) {
             script.Remove();
-        } else {
-            script->mSize = script->Range().length();
         }
     }
 
     if (!found) {
         mSaveComplete = true;
         return;
     }
 
@@ -709,16 +707,31 @@ ScriptPreloader::NoteScript(const nsCStr
     auto script = mScripts.LookupOrAdd(cachePath, *this, url, cachePath, jsscript);
 
     if (!script->mScript) {
         MOZ_ASSERT(jsscript);
         script->mScript = jsscript;
         script->mReadyToExecute = true;
     }
 
+    // If we don't already have bytecode for this script, and it doesn't already
+    // exist in the child cache, encode it now, before it's ever executed.
+    //
+    // Ideally, we would like to do the encoding lazily, during idle slices.
+    // There are subtle issues with encoding scripts which have already been
+    // executed, though, which makes that somewhat risky. So until that
+    // situation is improved, and thoroughly tested, we need to encode eagerly.
+    //
+    // (See also the TranscodeResult_Failure_RunOnceNotSupported failure case in
+    // js::XDRScript)
+    if (!script->mSize && !(mChildCache && mChildCache->mScripts.Get(cachePath))) {
+        AutoSafeJSAPI jsapi;
+        Unused << script->XDREncode(jsapi.cx());
+    }
+
     script->UpdateLoadTime(TimeStamp::Now());
     script->mProcessTypes += CurrentProcessType();
 }
 
 void
 ScriptPreloader::NoteScript(const nsCString& url, const nsCString& cachePath,
                             ProcessType processType, nsTArray<uint8_t>&& xdrData,
                             TimeStamp loadTime)
@@ -986,18 +999,20 @@ ScriptPreloader::CachedScript::XDREncode
     JSAutoCompartment ac(cx, mScript);
     JS::RootedScript jsscript(cx, mScript);
 
     mXDRData.construct<JS::TranscodeBuffer>();
 
     JS::TranscodeResult code = JS::EncodeScript(cx, Buffer(), jsscript);
     if (code == JS::TranscodeResult_Ok) {
         mXDRRange.emplace(Buffer().begin(), Buffer().length());
+        mSize = Range().length();
         return true;
     }
+    mXDRData.destroy();
     JS_ClearPendingException(cx);
     return false;
 }
 
 JSScript*
 ScriptPreloader::CachedScript::GetJSScript(JSContext* cx)
 {
     MOZ_ASSERT(mReadyToExecute);