Bug 1554183: Save font data to startup cache immediately r=jfkthame
authorRandell Jesup <rjesup@wgate.com>
Fri, 24 May 2019 21:38:47 -0400
changeset 476538 798777922fe7065cb8cd7653c10e348e1e9ac887
parent 476537 bbcfdcc12774c1b8d78c6420614141382fed3d40
child 476539 04b0e8a22ee2e0bb67c6cff81b7a9cf312df9e2f
push id36098
push userdluca@mozilla.com
push dateSun, 02 Jun 2019 09:44:49 +0000
treeherdermozilla-central@afb588ff0931 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjfkthame
bugs1554183
milestone69.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 1554183: Save font data to startup cache immediately r=jfkthame Differential Revision: https://phabricator.services.mozilla.com/D33245
gfx/thebes/gfxFT2FontList.cpp
gfx/thebes/gfxFT2FontList.h
--- a/gfx/thebes/gfxFT2FontList.cpp
+++ b/gfx/thebes/gfxFT2FontList.cpp
@@ -702,21 +702,24 @@ class FontNameCache {
     mOps = (PLDHashTableOps){StringHash, HashMatchEntry, MoveEntry,
                              PLDHashTable::ClearEntryStub, nullptr};
 
     MOZ_ASSERT(XRE_IsParentProcess(),
                "FontNameCache should only be used in chrome process");
     mCache = mozilla::scache::StartupCache::GetSingleton();
   }
 
-  ~FontNameCache() {
+  ~FontNameCache() { WriteCache(); }
+
+  void WriteCache() {
     if (!mWriteNeeded || !mCache) {
       return;
     }
 
+    LOG(("Writing FontNameCache:"));
     nsAutoCString buf;
     for (auto iter = mMap.Iter(); !iter.Done(); iter.Next()) {
       auto entry = static_cast<FNCMapEntry*>(iter.Get());
       if (!entry->mFileExists) {
         // skip writing entries for files that are no longer present
         continue;
       }
       buf.Append(entry->mFilename);
@@ -724,33 +727,37 @@ class FontNameCache {
       buf.Append(entry->mFaces);
       buf.Append(';');
       buf.AppendInt(entry->mTimestamp);
       buf.Append(';');
       buf.AppendInt(entry->mFilesize);
       buf.Append(';');
     }
 
+    LOG(("putting FontNameCache to " CACHE_KEY ", length %u",
+         buf.Length() + 1));
     mCache->PutBuffer(CACHE_KEY, UniquePtr<char[]>(ToNewCString(buf)),
                       buf.Length() + 1);
+    mWriteNeeded = false;
   }
 
   // This may be called more than once (if we re-load the font list).
   void Init() {
     if (!mCache) {
       return;
     }
 
     uint32_t size;
     UniquePtr<char[]> buf;
     if (NS_FAILED(mCache->GetBuffer(CACHE_KEY, &buf, &size))) {
+      LOG(("no cache of " CACHE_KEY));
       return;
     }
 
-    LOG(("got: %s from the cache", nsDependentCString(buf.get(), size).get()));
+    LOG(("got: %u bytes from the cache " CACHE_KEY, size));
 
     mMap.Clear();
     mWriteNeeded = false;
 
     const char* beginning = buf.get();
     const char* end = strchr(beginning, ';');
     while (end) {
       nsCString filename(beginning, end - beginning);
@@ -1333,16 +1340,35 @@ void gfxFT2FontList::FindFonts() {
   // Finalize the families by sorting faces into standard order
   // and marking "simple" families.
   // Passing non-null userData here says that we want faces to be sorted.
   for (auto iter = mFontFamilies.Iter(); !iter.Done(); iter.Next()) {
     nsCStringHashKey::KeyType key = iter.Key();
     RefPtr<gfxFontFamily>& family = iter.Data();
     FinalizeFamilyMemberList(key, family, /* aSortFaces */ true);
   }
+  // Write out FontCache data if needed
+  WriteCache();
+}
+
+void gfxFT2FontList::WriteCache() {
+  if (mFontNameCache) {
+    mFontNameCache->WriteCache();
+  }
+  mozilla::scache::StartupCache* cache =
+      mozilla::scache::StartupCache::GetSingleton();
+  if (cache && mJarModifiedTime > 0) {
+    const size_t bufSize = sizeof(mJarModifiedTime);
+    auto buf = MakeUnique<char[]>(bufSize);
+    memcpy(buf.get(), &mJarModifiedTime, bufSize);
+
+    LOG(("WriteCache: putting Jar, length %zu", bufSize));
+    cache->PutBuffer(JAR_LAST_MODIFED_TIME, std::move(buf), bufSize);
+  }
+  LOG(("Done with writecache"));
 }
 
 void gfxFT2FontList::FindFontsInDir(const nsCString& aDir,
                                     FontNameCache* aFNC) {
   static const char* sStandardFonts[] = {"DroidSans.ttf",
                                          "DroidSans-Bold.ttf",
                                          "DroidSerif-Regular.ttf",
                                          "DroidSerif-Bold.ttf",
@@ -1543,19 +1569,12 @@ void gfxFT2FontList::GetFontFamilyList(
   }
 }
 
 gfxFontFamily* gfxFT2FontList::CreateFontFamily(const nsACString& aName) const {
   return new FT2FontFamily(aName);
 }
 
 void gfxFT2FontList::WillShutdown() {
-  mozilla::scache::StartupCache* cache =
-      mozilla::scache::StartupCache::GetSingleton();
-  if (cache && mJarModifiedTime > 0) {
-    const size_t bufSize = sizeof(mJarModifiedTime);
-    auto buf = MakeUnique<char[]>(bufSize);
-    memcpy(buf.get(), &mJarModifiedTime, bufSize);
-
-    cache->PutBuffer(JAR_LAST_MODIFED_TIME, std::move(buf), bufSize);
-  }
+  LOG(("WillShutdown"));
+  WriteCache();
   mFontNameCache = nullptr;
 }
--- a/gfx/thebes/gfxFT2FontList.h
+++ b/gfx/thebes/gfxFT2FontList.h
@@ -127,16 +127,18 @@ class gfxFT2FontList : public gfxPlatfor
 
   gfxFontEntry* MakePlatformFont(const nsACString& aFontName,
                                  WeightRange aWeightForEntry,
                                  StretchRange aStretchForEntry,
                                  SlantStyleRange aStyleForEntry,
                                  const uint8_t* aFontData,
                                  uint32_t aLength) override;
 
+  void WriteCache();
+
   void GetSystemFontList(InfallibleTArray<FontListEntry>* retValue);
 
   static gfxFT2FontList* PlatformFontList() {
     return static_cast<gfxFT2FontList*>(
         gfxPlatformFontList::PlatformFontList());
   }
 
   void GetFontFamilyList(