Bug 953005 - Part 1: [B2G] Enhance the initialization of MobileConnectionArray to improve performance. r=khuey, a=1.3+
authorHsin-Yi Tsai <htsai@mozilla.com>
Tue, 14 Jan 2014 14:17:35 +0800
changeset 174969 d17316ce716eef5aeadaf4e6e3203cb62f2bd843
parent 174968 3fe07c50c854223ecb154810f5046310b3e15827
child 174970 0c0e7c3fd0efc4684db0d62d996c2e40bdfd9027
push id3224
push userlsblakk@mozilla.com
push dateTue, 04 Feb 2014 01:06:49 +0000
treeherdermozilla-beta@60c04d0987f1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskhuey, 1
bugs953005
milestone28.0a2
Bug 953005 - Part 1: [B2G] Enhance the initialization of MobileConnectionArray to improve performance. r=khuey, a=1.3+
dom/network/src/MobileConnectionArray.cpp
dom/network/src/MobileConnectionArray.h
--- a/dom/network/src/MobileConnectionArray.cpp
+++ b/dom/network/src/MobileConnectionArray.cpp
@@ -28,41 +28,53 @@ NS_IMPL_CYCLE_COLLECTING_ADDREF(MobileCo
 NS_IMPL_CYCLE_COLLECTING_RELEASE(MobileConnectionArray)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MobileConnectionArray)
   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
   NS_INTERFACE_MAP_ENTRY(nsISupports)
 NS_INTERFACE_MAP_END
 
 MobileConnectionArray::MobileConnectionArray(nsPIDOMWindow* aWindow)
-: mWindow(aWindow)
+: mWindow(aWindow), mInitialized(false)
 {
-  int32_t numRil = mozilla::Preferences::GetInt("ril.numRadioInterfaces", 1);
+  uint32_t numRil = mozilla::Preferences::GetUint("ril.numRadioInterfaces", 1);
   MOZ_ASSERT(numRil > 0);
 
-  for (int32_t id = 0; id < numRil; id++) {
-    nsRefPtr<MobileConnection> mobileConnection = new MobileConnection(id);
-    mobileConnection->Init(aWindow);
-    mMobileConnections.AppendElement(mobileConnection);
-  }
+  bool ret = mMobileConnections.SetLength(numRil);
+  MOZ_ASSERT(ret);
 
   SetIsDOMBinding();
 }
 
 MobileConnectionArray::~MobileConnectionArray()
 {
   DropConnections();
 }
 
 void
+MobileConnectionArray::Init()
+{
+  mInitialized = true;
+
+  for (uint32_t id = 0; id < mMobileConnections.Length(); id++) {
+    nsRefPtr<MobileConnection> mobileConnection = new MobileConnection(id);
+    mobileConnection->Init(mWindow);
+    mMobileConnections[id] = mobileConnection;
+  }
+}
+
+void
 MobileConnectionArray::DropConnections()
 {
-  for (uint32_t i = 0; i < mMobileConnections.Length(); i++) {
-    mMobileConnections[i]->Shutdown();
+  if (mInitialized) {
+    for (uint32_t i = 0; i < mMobileConnections.Length(); i++) {
+      mMobileConnections[i]->Shutdown();
+    }
   }
+
   mMobileConnections.Clear();
 }
 
 nsPIDOMWindow*
 MobileConnectionArray::GetParentObject() const
 {
   MOZ_ASSERT(mWindow);
   return mWindow;
@@ -70,28 +82,32 @@ MobileConnectionArray::GetParentObject()
 
 JSObject*
 MobileConnectionArray::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
 {
   return MozMobileConnectionArrayBinding::Wrap(aCx, aScope, this);
 }
 
 nsIDOMMozMobileConnection*
-MobileConnectionArray::Item(uint32_t aIndex) const
+MobileConnectionArray::Item(uint32_t aIndex)
 {
   bool unused;
   return IndexedGetter(aIndex, unused);
 }
 
 uint32_t
 MobileConnectionArray::Length() const
 {
   return mMobileConnections.Length();
 }
 
 nsIDOMMozMobileConnection*
-MobileConnectionArray::IndexedGetter(uint32_t aIndex, bool& aFound) const
+MobileConnectionArray::IndexedGetter(uint32_t aIndex, bool& aFound)
 {
+  if (!mInitialized) {
+    Init();
+  }
+
   aFound = false;
   aFound = aIndex < mMobileConnections.Length();
 
   return aFound ? mMobileConnections[aIndex] : nullptr;
 }
--- a/dom/network/src/MobileConnectionArray.h
+++ b/dom/network/src/MobileConnectionArray.h
@@ -29,28 +29,34 @@ public:
   GetParentObject() const;
 
   // WrapperCache
   virtual JSObject*
   WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
 
   //  WebIDL
   nsIDOMMozMobileConnection*
-  Item(uint32_t aIndex) const;
+  Item(uint32_t aIndex);
 
   uint32_t
   Length() const;
 
   nsIDOMMozMobileConnection*
-  IndexedGetter(uint32_t aIndex, bool& aFound) const;
+  IndexedGetter(uint32_t aIndex, bool& aFound);
 
 private:
   ~MobileConnectionArray();
 
-  void DropConnections();
+  void
+  Init();
+
+  void
+  DropConnections();
+
+  bool mInitialized;
 
   nsCOMPtr<nsPIDOMWindow> mWindow;
   nsTArray<nsRefPtr<MobileConnection>> mMobileConnections;
 };
 
 } // namespace network
 } // namespace dom
 } // namespace mozilla