Bug 1655067 - Use range for for some loops; r=dom-workers-and-storage-reviewers,sg
authorTom Tung <ttung@mozilla.com>
Fri, 11 Sep 2020 07:58:06 +0000
changeset 548281 ca25b9db4e2c45dd215ff55f49a61965c29aa4a9
parent 548280 9e0aeb1b5202cb3d86b71e63281ecc0928dda51e
child 548282 cd73a90d2fceeec7270b6c4a3ac90a1779c28d94
push id37776
push userbtara@mozilla.com
push dateFri, 11 Sep 2020 15:10:42 +0000
treeherdermozilla-central@b133e2d673e8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdom-workers-and-storage-reviewers, sg
bugs1655067
milestone82.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 1655067 - Use range for for some loops; r=dom-workers-and-storage-reviewers,sg Differential Revision: https://phabricator.services.mozilla.com/D86209
dom/cache/DBSchema.cpp
--- a/dom/cache/DBSchema.cpp
+++ b/dom/cache/DBSchema.cpp
@@ -897,19 +897,19 @@ nsresult CacheMatchAll(mozIStorageConnec
   } else {
     rv = QueryCache(aConn, aCacheId, aMaybeRequest.ref(), aParams, matches);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
   }
 
   // TODO: replace this with a bulk load using SQL IN clause (bug 1110458)
-  for (uint32_t i = 0; i < matches.Length(); ++i) {
+  for (const auto match : matches) {
     SavedResponse savedResponse;
-    rv = ReadResponse(aConn, matches[i], &savedResponse);
+    rv = ReadResponse(aConn, match, &savedResponse);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
     savedResponse.mCacheId = aCacheId;
     aSavedResponsesOut.AppendElement(savedResponse);
   }
 
   return rv;
@@ -1013,19 +1013,19 @@ nsresult CacheKeys(mozIStorageConnection
   } else {
     rv = QueryCache(aConn, aCacheId, aMaybeRequest.ref(), aParams, matches);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
   }
 
   // TODO: replace this with a bulk load using SQL IN clause (bug 1110458)
-  for (uint32_t i = 0; i < matches.Length(); ++i) {
+  for (const auto match : matches) {
     SavedRequest savedRequest;
-    rv = ReadRequest(aConn, matches[i], &savedRequest);
+    rv = ReadRequest(aConn, match, &savedRequest);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
     savedRequest.mCacheId = aCacheId;
     aSavedRequestsOut.AppendElement(savedRequest);
   }
 
   return rv;
@@ -1091,25 +1091,25 @@ nsresult StorageMatch(mozIStorageConnect
     rv = state->GetInt64(0, &cacheId);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
     cacheIdList.AppendElement(cacheId);
   }
 
   // Now try to find a match in each cache in order
-  for (uint32_t i = 0; i < cacheIdList.Length(); ++i) {
-    rv = CacheMatch(aConn, cacheIdList[i], aRequest, aParams, aFoundResponseOut,
+  for (const auto cacheId : cacheIdList) {
+    rv = CacheMatch(aConn, cacheId, aRequest, aParams, aFoundResponseOut,
                     aSavedResponseOut);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
 
     if (*aFoundResponseOut) {
-      aSavedResponseOut->mCacheId = cacheIdList[i];
+      aSavedResponseOut->mCacheId = cacheId;
       return rv;
     }
   }
 
   return NS_OK;
 }
 
 nsresult StorageGetCacheId(mozIStorageConnection& aConn, Namespace aNamespace,
@@ -1495,19 +1495,18 @@ nsresult MatchByVaryHeader(mozIStorageCo
   }
 
   RefPtr<InternalHeaders> queryHeaders =
       TypeUtils::ToInternalHeaders(aRequest.headers());
 
   // Assume the vary headers match until we find a conflict
   bool varyHeadersMatch = true;
 
-  for (uint32_t i = 0; i < varyValues.Length(); ++i) {
+  for (auto& varyValue : varyValues) {
     // Extract the header names inside the Vary header value.
-    nsAutoCString varyValue(varyValues[i]);
     char* rawBuffer = varyValue.BeginWriting();
     char* token = nsCRT::strtok(rawBuffer, NS_HTTP_HEADER_SEPS, &rawBuffer);
     bool bailOut = false;
     for (; token;
          token = nsCRT::strtok(rawBuffer, NS_HTTP_HEADER_SEPS, &rawBuffer)) {
       nsDependentCString header(token);
       MOZ_DIAGNOSTIC_ASSERT(!header.EqualsLiteral("*"),
                             "We should have already caught this in "
@@ -1643,30 +1642,30 @@ nsresult DeleteEntries(mozIStorageConnec
 
     if (!isNull) {
       int32_t securityId = -1;
       rv = state->GetInt32(2, &securityId);
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
 
-      // First try to increment the count for this ID if we're already
-      // seen it
-      bool found = false;
-      for (uint32_t i = 0; i < aDeletedSecurityIdListOut.Length(); ++i) {
-        if (aDeletedSecurityIdListOut[i].mId == securityId) {
-          found = true;
-          aDeletedSecurityIdListOut[i].mCount += 1;
-          break;
-        }
-      }
-
-      // Otherwise add a new entry for this ID with a count of 1
-      if (!found) {
+      // XXXtt: Consider using map for aDeletedSecuityIdListOut.
+      auto foundIt = std::find_if(aDeletedSecurityIdListOut.begin(),
+                                  aDeletedSecurityIdListOut.end(),
+                                  [securityId](const auto& deletedSecurityId) {
+                                    return deletedSecurityId.mId == securityId;
+                                  });
+
+      if (foundIt == aDeletedSecurityIdListOut.end()) {
+        // Add a new entry for this ID with a count of 1, if it's not in the
+        // list
         aDeletedSecurityIdListOut.AppendElement(IdCount(securityId));
+      } else {
+        // Otherwise, increment the count for this ID
+        foundIt->mCount += 1;
       }
     }
 
     // It's possible to have null padding size for non-opaque response
     rv = state->GetIsNull(3, &isNull);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
@@ -1927,19 +1926,19 @@ nsresult DeleteSecurityInfo(mozIStorageC
   }
 
   return NS_OK;
 }
 
 nsresult DeleteSecurityInfoList(
     mozIStorageConnection& aConn,
     const nsTArray<IdCount>& aDeletedStorageIdList) {
-  for (uint32_t i = 0; i < aDeletedStorageIdList.Length(); ++i) {
-    nsresult rv = DeleteSecurityInfo(aConn, aDeletedStorageIdList[i].mId,
-                                     aDeletedStorageIdList[i].mCount);
+  for (const auto& deletedStorageId : aDeletedStorageIdList) {
+    nsresult rv = DeleteSecurityInfo(aConn, deletedStorageId.mId,
+                                     deletedStorageId.mCount);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
   }
 
   return NS_OK;
 }
 
@@ -2227,24 +2226,23 @@ nsresult InsertEntry(mozIStorageConnecti
                        "value, "
                        "entry_id "
                        ") VALUES (:name, :value, :entry_id)"),
       getter_AddRefs(state));
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
-  const nsTArray<HeadersEntry>& requestHeaders = aRequest.headers();
-  for (uint32_t i = 0; i < requestHeaders.Length(); ++i) {
-    rv = state->BindUTF8StringByName("name"_ns, requestHeaders[i].name());
+  for (const auto& requestHeader : aRequest.headers()) {
+    rv = state->BindUTF8StringByName("name"_ns, requestHeader.name());
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
 
-    rv = state->BindUTF8StringByName("value"_ns, requestHeaders[i].value());
+    rv = state->BindUTF8StringByName("value"_ns, requestHeader.value());
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
 
     rv = state->BindInt32ByName("entry_id"_ns, entryId);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
@@ -2261,24 +2259,23 @@ nsresult InsertEntry(mozIStorageConnecti
                        "value, "
                        "entry_id "
                        ") VALUES (:name, :value, :entry_id)"),
       getter_AddRefs(state));
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
-  const nsTArray<HeadersEntry>& responseHeaders = aResponse.headers();
-  for (uint32_t i = 0; i < responseHeaders.Length(); ++i) {
-    rv = state->BindUTF8StringByName("name"_ns, responseHeaders[i].name());
+  for (const auto& responseHeader : aResponse.headers()) {
+    rv = state->BindUTF8StringByName("name"_ns, responseHeader.name());
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
 
-    rv = state->BindUTF8StringByName("value"_ns, responseHeaders[i].value());
+    rv = state->BindUTF8StringByName("value"_ns, responseHeader.value());
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
 
     rv = state->BindInt32ByName("entry_id"_ns, entryId);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
@@ -2293,19 +2290,18 @@ nsresult InsertEntry(mozIStorageConnecti
                                               "url, "
                                               "entry_id "
                                               ") VALUES (:url, :entry_id)"),
                              getter_AddRefs(state));
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
-  const nsTArray<nsCString>& responseUrlList = aResponse.urlList();
-  for (uint32_t i = 0; i < responseUrlList.Length(); ++i) {
-    rv = state->BindUTF8StringByName("url"_ns, responseUrlList[i]);
+  for (const auto& responseUrl : aResponse.urlList()) {
+    rv = state->BindUTF8StringByName("url"_ns, responseUrl);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
 
     rv = state->BindInt32ByName("entry_id"_ns, entryId);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
@@ -2959,31 +2955,30 @@ nsresult Validate(mozIStorageConnection&
 
   if (NS_WARN_IF(schemaVersion != kLatestSchemaVersion)) {
     return NS_ERROR_FAILURE;
   }
 
 #ifdef DEBUG
   // This is the schema we expect the database at the latest version to
   // contain.  Update this list if you add a new table or index.
-  Expect expect[] = {
+  Expect expects[] = {
       Expect("caches", "table", kTableCaches),
       Expect("sqlite_sequence", "table"),  // auto-gen by sqlite
       Expect("security_info", "table", kTableSecurityInfo),
       Expect("security_info_hash_index", "index", kIndexSecurityInfoHash),
       Expect("entries", "table", kTableEntries),
       Expect("entries_request_match_index", "index", kIndexEntriesRequest),
       Expect("request_headers", "table", kTableRequestHeaders),
       Expect("response_headers", "table", kTableResponseHeaders),
       Expect("response_headers_name_index", "index", kIndexResponseHeadersName),
       Expect("response_url_list", "table", kTableResponseUrlList),
       Expect("storage", "table", kTableStorage),
       Expect("sqlite_autoindex_storage_1", "index"),  // auto-gen by sqlite
   };
-  const uint32_t expectLength = sizeof(expect) / sizeof(Expect);
 
   // Read the schema from the sqlite_master table and compare.
   nsCOMPtr<mozIStorageStatement> state;
   rv = aConn.CreateStatement("SELECT name, type, sql FROM sqlite_master;"_ns,
                              getter_AddRefs(state));
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
@@ -3004,27 +2999,27 @@ nsresult Validate(mozIStorageConnection&
 
     nsAutoCString sql;
     rv = state->GetUTF8String(2, sql);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
 
     bool foundMatch = false;
-    for (uint32_t i = 0; i < expectLength; ++i) {
-      if (name == expect[i].mName) {
-        if (type != expect[i].mType) {
+    for (const auto& expect : expects) {
+      if (name == expect.mName) {
+        if (type != expect.mType) {
           NS_WARNING(
               nsPrintfCString("Unexpected type for Cache schema entry %s",
                               name.get())
                   .get());
           return NS_ERROR_FAILURE;
         }
 
-        if (!expect[i].mIgnoreSql && sql != expect[i].mSql) {
+        if (!expect.mIgnoreSql && sql != expect.mSql) {
           NS_WARNING(nsPrintfCString("Unexpected SQL for Cache schema entry %s",
                                      name.get())
                          .get());
           return NS_ERROR_FAILURE;
         }
 
         foundMatch = true;
         break;
@@ -3073,17 +3068,16 @@ nsresult MigrateFrom26To27(mozIStorageCo
 Migration sMigrationList[] = {
     Migration(15, MigrateFrom15To16), Migration(16, MigrateFrom16To17),
     Migration(17, MigrateFrom17To18), Migration(18, MigrateFrom18To19),
     Migration(19, MigrateFrom19To20), Migration(20, MigrateFrom20To21),
     Migration(21, MigrateFrom21To22), Migration(22, MigrateFrom22To23),
     Migration(23, MigrateFrom23To24), Migration(24, MigrateFrom24To25),
     Migration(25, MigrateFrom25To26), Migration(26, MigrateFrom26To27),
 };
-uint32_t sMigrationListLength = sizeof(sMigrationList) / sizeof(Migration);
 nsresult RewriteEntriesSchema(mozIStorageConnection& aConn) {
   nsresult rv = aConn.ExecuteSimpleSQL("PRAGMA writable_schema = ON"_ns);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   nsCOMPtr<mozIStorageStatement> state;
   rv = aConn.CreateStatement(
@@ -3124,20 +3118,20 @@ nsresult Migrate(mozIStorageConnection& 
   bool rewriteSchema = false;
 
   while (currentVersion < kLatestSchemaVersion) {
     // Wiping old databases is handled in DBAction because it requires
     // making a whole new mozIStorageConnection.  Make sure we don't
     // accidentally get here for one of those old databases.
     MOZ_DIAGNOSTIC_ASSERT(currentVersion >= kFirstShippedSchemaVersion);
 
-    for (uint32_t i = 0; i < sMigrationListLength; ++i) {
-      if (sMigrationList[i].mFromVersion == currentVersion) {
+    for (const auto& migration : sMigrationList) {
+      if (migration.mFromVersion == currentVersion) {
         bool shouldRewrite = false;
-        rv = sMigrationList[i].mFunc(aConn, shouldRewrite);
+        rv = migration.mFunc(aConn, shouldRewrite);
         if (NS_WARN_IF(NS_FAILED(rv))) {
           return rv;
         }
         if (shouldRewrite) {
           rewriteSchema = true;
         }
         break;
       }