Bug 1203140 - Add a pref cache for the touch events pref, as it can get called a lot. r=smaug, a=sylvestre
authorKartikaya Gupta <kgupta@mozilla.com>
Tue, 24 May 2016 11:24:30 -0400
changeset 333384 7366f3c669b387034cdb723d7ca8cc63afc1a287
parent 333383 810fa61b0d7347ab50e7dc930d3f0d95a8f8bda4
child 333385 b142c2e3bf73c86c64ef9ec503dbb0dff08d85d0
push id6092
push usercbook@mozilla.com
push dateThu, 23 Jun 2016 13:55:42 +0000
treeherdermozilla-beta@b142c2e3bf73 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug, sylvestre
bugs1203140
milestone48.0
Bug 1203140 - Add a pref cache for the touch events pref, as it can get called a lot. r=smaug, a=sylvestre MozReview-Commit-ID: EQD3oxFaInO
dom/events/TouchEvent.cpp
--- a/dom/events/TouchEvent.cpp
+++ b/dom/events/TouchEvent.cpp
@@ -172,38 +172,43 @@ TouchEvent::ChangedTouches()
   }
   return mChangedTouches;
 }
 
 // static
 bool
 TouchEvent::PrefEnabled(JSContext* aCx, JSObject* aGlobal)
 {
+  static bool sPrefCached = false;
+  static int32_t sPrefCacheValue = 0;
+
+  if (!sPrefCached) {
+    sPrefCached = true;
+    Preferences::AddIntVarCache(&sPrefCacheValue, "dom.w3c_touch_events.enabled");
+  }
+
   bool prefValue = false;
-  int32_t flag = 0;
-  if (NS_SUCCEEDED(Preferences::GetInt("dom.w3c_touch_events.enabled", &flag))) {
-    if (flag == 2) {
+  if (sPrefCacheValue == 2) {
 #if defined(MOZ_B2G) || defined(MOZ_WIDGET_ANDROID)
-      // Touch support is always enabled on B2G and android.
-      prefValue = true;
+    // Touch support is always enabled on B2G and android.
+    prefValue = true;
 #elif defined(XP_WIN) || MOZ_WIDGET_GTK == 3
-      static bool sDidCheckTouchDeviceSupport = false;
-      static bool sIsTouchDeviceSupportPresent = false;
-      // On Windows and GTK3 we auto-detect based on device support.
-      if (!sDidCheckTouchDeviceSupport) {
-        sDidCheckTouchDeviceSupport = true;
-        sIsTouchDeviceSupportPresent = WidgetUtils::IsTouchDeviceSupportPresent();
-      }
-      prefValue = sIsTouchDeviceSupportPresent;
+    static bool sDidCheckTouchDeviceSupport = false;
+    static bool sIsTouchDeviceSupportPresent = false;
+    // On Windows and GTK3 we auto-detect based on device support.
+    if (!sDidCheckTouchDeviceSupport) {
+      sDidCheckTouchDeviceSupport = true;
+      sIsTouchDeviceSupportPresent = WidgetUtils::IsTouchDeviceSupportPresent();
+    }
+    prefValue = sIsTouchDeviceSupportPresent;
 #else
-      prefValue = false;
+    prefValue = false;
 #endif
-    } else {
-      prefValue = !!flag;
-    }
+  } else {
+    prefValue = !!sPrefCacheValue;
   }
   if (prefValue) {
     nsContentUtils::InitializeTouchEventTable();
   }
   return prefValue;
 }
 
 // static