Bug 962968 - Replace HashMap with SparseArray in PrefsHelper (r=rnewman)
authorLucas Rocha <lucasr@mozilla.com>
Thu, 23 Jan 2014 19:00:06 +0000
changeset 164998 533642b8d6df94e2ad974220d8e6edd5e252db7f
parent 164997 f8cb6d26dd7d2317f51a018fad892f48ada4e0cc
child 164999 5d550701bd491693707fece10426595e971a9efb
push id38871
push usercbook@mozilla.com
push dateFri, 24 Jan 2014 08:28:29 +0000
treeherdermozilla-inbound@8be30d3b0b80 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman
bugs962968
milestone29.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 962968 - Replace HashMap with SparseArray in PrefsHelper (r=rnewman)
mobile/android/base/PrefsHelper.java
--- a/mobile/android/base/PrefsHelper.java
+++ b/mobile/android/base/PrefsHelper.java
@@ -7,29 +7,28 @@ package org.mozilla.gecko;
 
 import org.mozilla.gecko.util.GeckoEventListener;
 
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
 import android.util.Log;
+import android.util.SparseArray;
 
 import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Map;
 
 /**
  * Helper class to get/set gecko prefs.
  */
 public final class PrefsHelper {
     private static final String LOGTAG = "GeckoPrefsHelper";
 
     private static boolean sRegistered = false;
-    private static final Map<Integer, PrefHandler> sCallbacks = new HashMap<Integer, PrefHandler>();
+    private static final SparseArray<PrefHandler> sCallbacks = new SparseArray<PrefHandler>();
     private static int sUniqueRequestId = 1;
 
     public static int getPref(String prefName, PrefHandler callback) {
         return getPrefsInternal(new String[] { prefName }, callback);
     }
 
     public static int getPrefs(String[] prefNames, PrefHandler callback) {
         return getPrefsInternal(prefNames, callback);
@@ -68,17 +67,17 @@ public final class PrefsHelper {
             @Override public void handleMessage(String event, JSONObject message) {
                 try {
                     PrefHandler callback;
                     synchronized (PrefsHelper.class) {
                         try {
                             int requestId = message.getInt("requestId");
                             callback = sCallbacks.get(requestId);
                             if (callback != null && !callback.isObserver()) {
-                                sCallbacks.remove(requestId);
+                                sCallbacks.delete(requestId);
                             }
                         } catch (Exception e) {
                             callback = null;
                         }
                     }
                     if (callback == null) {
                         Log.d(LOGTAG, "Preferences:Data message had an unknown requestId; ignoring");
                         return;
@@ -139,17 +138,19 @@ public final class PrefsHelper {
     }
 
     public static void removeObserver(int requestId) {
         if (requestId < 0) {
             throw new IllegalArgumentException("Invalid request ID");
         }
 
         synchronized (PrefsHelper.class) {
-            PrefHandler callback = sCallbacks.remove(requestId);
+            PrefHandler callback = sCallbacks.get(requestId);
+            sCallbacks.delete(requestId);
+
             if (callback == null) {
                 Log.e(LOGTAG, "Unknown request ID " + requestId);
                 return;
             }
         }
 
         GeckoEvent event = GeckoEvent.createBroadcastEvent("Preferences:RemoveObserver",
                                                            Integer.toString(requestId));