Bug 1173051 - Minor optimizations for device storage enumerate on parent and child. r=dhylands
authorAndrew Osmond <aosmond@mozilla.com>
Wed, 10 Jun 2015 10:23:43 -0400
changeset 248101 62896b7f8e2fea708328a6a2656721d5907e0240
parent 248100 22e7e595584ace1e26b5eff3b58c7adfe902b8ce
child 248102 fa0c8750017791389dc04aa809308857d72227b7
push id60888
push userkwierso@gmail.com
push dateThu, 11 Jun 2015 01:38:38 +0000
treeherdermozilla-inbound@39e638ed06bf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdhylands
bugs1173051
milestone41.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 1173051 - Minor optimizations for device storage enumerate on parent and child. r=dhylands
dom/devicestorage/DeviceStorageRequestChild.cpp
dom/devicestorage/nsDeviceStorage.cpp
dom/devicestorage/nsDeviceStorage.h
--- a/dom/devicestorage/DeviceStorageRequestChild.cpp
+++ b/dom/devicestorage/DeviceStorageRequestChild.cpp
@@ -188,21 +188,22 @@ DeviceStorageRequestChild::
 
     case DeviceStorageResponseValue::TEnumerationResponse:
     {
       EnumerationResponse r = aValue;
       nsDOMDeviceStorageCursor* cursor
         = static_cast<nsDOMDeviceStorageCursor*>(mRequest.get());
 
       uint32_t count = r.paths().Length();
+      cursor->mFiles.SetCapacity(count);
       for (uint32_t i = 0; i < count; i++) {
         nsRefPtr<DeviceStorageFile> dsf
           = new DeviceStorageFile(r.type(), r.paths()[i].storageName(),
                                   r.rootdir(), r.paths()[i].name());
-        cursor->mFiles.AppendElement(dsf);
+        cursor->mFiles.AppendElement(dsf.forget());
       }
 
       nsRefPtr<ContinueCursorEvent> event = new ContinueCursorEvent(cursor);
       event->Continue();
       break;
     }
 
     default:
--- a/dom/devicestorage/nsDeviceStorage.cpp
+++ b/dom/devicestorage/nsDeviceStorage.cpp
@@ -1456,16 +1456,20 @@ DeviceStorageFile::collectFilesInternal(
 
   nsCOMPtr<nsISimpleEnumerator> e;
   mFile->GetDirectoryEntries(getter_AddRefs(e));
 
   if (!e) {
     return;
   }
 
+  DeviceStorageTypeChecker* typeChecker
+    = DeviceStorageTypeChecker::CreateOrGet();
+  MOZ_ASSERT(typeChecker);
+
   nsCOMPtr<nsIDirectoryEnumerator> files = do_QueryInterface(e);
   nsCOMPtr<nsIFile> f;
 
   while (NS_SUCCEEDED(files->GetNextFile(getter_AddRefs(f))) && f) {
 
     bool isFile;
     f->IsFile(&isFile);
 
@@ -1482,16 +1486,20 @@ DeviceStorageFile::collectFilesInternal(
     f->IsDirectory(&isDir);
 
     nsString fullpath;
     nsresult rv = f->GetPath(fullpath);
     if (NS_FAILED(rv)) {
       continue;
     }
 
+    if (isFile && !typeChecker->Check(mStorageType, fullpath)) {
+      continue;
+    }
+
     if (!StringBeginsWith(fullpath, aRootPath)) {
       NS_ERROR("collectFiles returned a path that does not belong!");
       continue;
     }
 
     nsAString::size_type len = aRootPath.Length() + 1; // +1 for the trailing /
     nsDependentSubstring newPath = Substring(fullpath, len);
 
@@ -2028,31 +2036,23 @@ ContinueCursorEvent::GetNextFile()
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   nsDOMDeviceStorageCursor* cursor
     = static_cast<nsDOMDeviceStorageCursor*>(mRequest.get());
   nsString cursorStorageType;
   cursor->GetStorageType(cursorStorageType);
 
-  DeviceStorageTypeChecker* typeChecker
-    = DeviceStorageTypeChecker::CreateOrGet();
-  if (!typeChecker) {
-    return nullptr;
-  }
-
-  while (cursor->mFiles.Length() > 0) {
-    nsRefPtr<DeviceStorageFile> file = cursor->mFiles[0];
-    cursor->mFiles.RemoveElementAt(0);
-    if (!typeChecker->Check(cursorStorageType, file->mFile)) {
-      continue;
+  while (cursor->mIndex < cursor->mFiles.Length()) {
+    nsRefPtr<DeviceStorageFile> file = cursor->mFiles[cursor->mIndex].forget();
+    ++cursor->mIndex;
+    if (file) {
+      file->CalculateMimeType();
+      return file.forget();
     }
-
-    file->CalculateMimeType();
-    return file.forget();
   }
 
   return nullptr;
 }
 
 ContinueCursorEvent::~ContinueCursorEvent() {}
 
 void
@@ -2167,16 +2167,17 @@ NS_IMPL_RELEASE_INHERITED(nsDOMDeviceSto
 
 nsDOMDeviceStorageCursor::nsDOMDeviceStorageCursor(nsPIDOMWindow* aWindow,
                                                    nsIPrincipal* aPrincipal,
                                                    DeviceStorageFile* aFile,
                                                    PRTime aSince)
   : DOMCursor(aWindow, nullptr)
   , mOkToCallContinue(false)
   , mSince(aSince)
+  , mIndex(0)
   , mFile(aFile)
   , mPrincipal(aPrincipal)
   , mRequester(new nsContentPermissionRequester(GetOwner()))
 {
 }
 
 nsDOMDeviceStorageCursor::~nsDOMDeviceStorageCursor()
 {
--- a/dom/devicestorage/nsDeviceStorage.h
+++ b/dom/devicestorage/nsDeviceStorage.h
@@ -211,16 +211,17 @@ public:
                            nsIPrincipal* aPrincipal,
                            DeviceStorageFile* aFile,
                            PRTime aSince);
 
 
   nsTArray<nsRefPtr<DeviceStorageFile> > mFiles;
   bool mOkToCallContinue;
   PRTime mSince;
+  size_t mIndex;
 
   void GetStorageType(nsAString & aType);
 
   void RequestComplete() override;
 
 private:
   ~nsDOMDeviceStorageCursor();