Bug 1369604: Force lazily-loaded metadata to load on background thread. r=liuche
authorMichael Comella <michael.l.comella@gmail.com>
Fri, 28 Jul 2017 15:02:57 -0700
changeset 420720 dd051eb3bbf65a6276b374ce70a523831c1c4628
parent 420719 e755f4ab80fdc2f6f9571854502034b1cb930ff1
child 420721 95ca4a6f89572763b25a41403861871104af6f44
push id7566
push usermtabara@mozilla.com
push dateWed, 02 Aug 2017 08:25:16 +0000
treeherdermozilla-beta@86913f512c3c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersliuche
bugs1369604
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 1369604: Force lazily-loaded metadata to load on background thread. r=liuche This undoes a caveat created from the last changeset; I did not profile this change. MozReview-Commit-ID: 6jpXyt0GRUj
mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/HighlightsLoader.java
--- a/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/HighlightsLoader.java
+++ b/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/HighlightsLoader.java
@@ -4,23 +4,25 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.activitystream.homepanel;
 
 import android.content.Context;
 import android.database.ContentObserver;
 import android.database.Cursor;
 import android.os.SystemClock;
+import android.support.annotation.WorkerThread;
 import android.support.v4.content.AsyncTaskLoader;
 
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.activitystream.ranking.HighlightsRanking;
 import org.mozilla.gecko.db.BrowserContract;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.activitystream.homepanel.model.Highlight;
+import org.mozilla.gecko.util.ThreadUtils;
 
 import java.util.Collections;
 import java.util.List;
 
 /**
  * Loader implementation for loading a list of ranked highlights from the database.
  */
 /* package-private */ class HighlightsLoader extends AsyncTaskLoader<List<Highlight>> {
@@ -57,25 +59,38 @@ import java.util.List;
             return Collections.emptyList();
         }
 
         try {
             // From now on get notified about content updates and reload data - until loader is reset.
             enableContentUpdates();
 
             final List<Highlight> highlights = HighlightsRanking.rank(candidatesCursor, highlightsLimit);
+            forceLoadHighlightMetadata(highlights); // force load now that we have a short list of the data.
 
             addToPerformanceHistogram(startTime);
 
             return highlights;
         } finally {
             candidatesCursor.close();
         }
     }
 
+    /**
+     * Optimization: we force the lazily-loaded metadata to load while we're running on this background thread
+     * rather than letting it block the UI thread when this data is being used in the UI.
+     */
+    @WorkerThread
+    private static void forceLoadHighlightMetadata(final List<Highlight> highlights) {
+        ThreadUtils.assertNotOnUiThread();
+        for (final Highlight highlight : highlights) {
+            highlight.getMetadataSlow();
+        }
+    }
+
     private void addToPerformanceHistogram(long startTime) {
         final long took = SystemClock.uptimeMillis() - startTime;
 
         Telemetry.addToHistogram(TELEMETRY_HISTOGRAM_ACTIVITY_STREAM_HIGHLIGHTS, (int) Math.min(took, Integer.MAX_VALUE));
     }
 
     @Override
     protected void onReset() {