Bug 1526542 - Improve the efficiency of CreatePermissionKey(); r=baku
authorEhsan Akhgari <ehsan@mozilla.com>
Mon, 11 Feb 2019 12:55:52 +0000
changeset 458532 f40bd9a6c21d9b839a6598522e44075f55d36daf
parent 458531 5d2cf729433068ce47e213d03717e52c5f8b5ef7
child 458533 2a84cc180ca99c24e5f49cec209f1149852c7105
push id111855
push userbtara@mozilla.com
push dateMon, 11 Feb 2019 22:01:49 +0000
treeherdermozilla-inbound@42a097167d36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1526542
milestone67.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 1526542 - Improve the efficiency of CreatePermissionKey(); r=baku There are two changes being made here: * In call sites where we know both origin arguments are equal, we will call a specialized version of the function which avoids the origin equality check. * We avoid calling into the generic printf structure and build the result string manually. Differential Revision: https://phabricator.services.mozilla.com/D19245
toolkit/components/antitracking/AntiTrackingCommon.cpp
--- a/toolkit/components/antitracking/AntiTrackingCommon.cpp
+++ b/toolkit/components/antitracking/AntiTrackingCommon.cpp
@@ -117,26 +117,45 @@ bool GetParentPrincipalAndTrackingOrigin
   }
   if (aTrackingPrincipal) {
     trackingPrincipal.forget(aTrackingPrincipal);
   }
   return true;
 };
 
 void CreatePermissionKey(const nsCString& aTrackingOrigin,
+                         nsACString& aPermissionKey) {
+  MOZ_ASSERT(aPermissionKey.IsEmpty());
+
+  static const nsLiteralCString prefix =
+      NS_LITERAL_CSTRING(ANTITRACKING_PERM_KEY "^");
+
+  aPermissionKey.SetCapacity(prefix.Length() + aTrackingOrigin.Length());
+  aPermissionKey.Append(prefix);
+  aPermissionKey.Append(aTrackingOrigin);
+}
+
+void CreatePermissionKey(const nsCString& aTrackingOrigin,
                          const nsCString& aGrantedOrigin,
                          nsACString& aPermissionKey) {
+  MOZ_ASSERT(aPermissionKey.IsEmpty());
+
   if (aTrackingOrigin == aGrantedOrigin) {
-    aPermissionKey =
-        nsPrintfCString(ANTITRACKING_PERM_KEY "^%s", aTrackingOrigin.get());
+    CreatePermissionKey(aTrackingOrigin, aPermissionKey);
     return;
   }
 
-  aPermissionKey = nsPrintfCString(ANTITRACKING_PERM_KEY "^%s^%s",
-                                   aTrackingOrigin.get(), aGrantedOrigin.get());
+  static const nsLiteralCString prefix =
+      NS_LITERAL_CSTRING(ANTITRACKING_PERM_KEY "^");
+
+  aPermissionKey.SetCapacity(prefix.Length() + 1 + aTrackingOrigin.Length());
+  aPermissionKey.Append(prefix);
+  aPermissionKey.Append(aTrackingOrigin);
+  aPermissionKey.AppendLiteral("^");
+  aPermissionKey.Append(aGrantedOrigin);
 }
 
 // This internal method returns ACCESS_DENY if the access is denied,
 // ACCESS_DEFAULT if unknown, some other access code if granted.
 nsCookieAccess CheckCookiePermissionForPrincipal(nsIPrincipal* aPrincipal) {
   nsCookieAccess access = nsICookiePermission::ACCESS_DEFAULT;
   if (!aPrincipal->GetIsCodebasePrincipal()) {
     return access;
@@ -1016,17 +1035,17 @@ bool AntiTrackingCommon::IsStorageAccess
   // open another origin where that second origin would be the granted origin.)
   // But even in the second case, the type of the permission would still be
   // formed by concatenating the granted origin to the end of the type name
   // (see CreatePermissionKey).  Therefore, we pass in the same argument to
   // both tracking origin and granted origin here in order to compute the
   // shorter permission key and will then do a prefix match on the type of the
   // input permission to see if it is a storage access permission or not.
   nsAutoCString permissionKey;
-  CreatePermissionKey(origin, origin, permissionKey);
+  CreatePermissionKey(origin, permissionKey);
 
   nsAutoCString type;
   rv = aPermission->GetType(type);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return false;
   }
 
   return StringBeginsWith(type, permissionKey);
@@ -1540,17 +1559,17 @@ bool AntiTrackingCommon::IsFirstPartySto
   nsAutoCString origin;
   nsresult rv = nsContentUtils::GetASCIIOrigin(aURI, origin);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     LOG_SPEC(("Failed to compute the origin from %s", _spec), aURI);
     return false;
   }
 
   nsAutoCString type;
-  CreatePermissionKey(origin, origin, type);
+  CreatePermissionKey(origin, type);
 
   nsPermissionManager* permManager = nsPermissionManager::GetInstance();
   if (NS_WARN_IF(!permManager)) {
     LOG(("Failed to obtain the permission manager"));
     return false;
   }
 
   uint32_t result = 0;