Bug 1339251 - Make Equals/Subsumes faster when comparing same objects, r=bholley
--- a/caps/nsIPrincipal.idl
+++ b/caps/nsIPrincipal.idl
@@ -6,19 +6,39 @@
/* Defines the abstract interface for a principal. */
#include "nsISerializable.idl"
%{C++
struct JSPrincipals;
#include "nsCOMPtr.h"
#include "nsTArray.h"
+#include "mozilla/DebugOnly.h"
namespace mozilla {
class OriginAttributes;
}
+
+/**
+ * Some methods have a fast path for the case when we're comparing a principal
+ * to itself. The situation may happen for example with about:blank documents.
+ */
+
+#define DECL_FAST_INLINE_HELPER(method_) \
+ inline bool method_(nsIPrincipal* aOther) \
+ { \
+ mozilla::DebugOnly<bool> val = false; \
+ MOZ_ASSERT_IF(this == aOther, \
+ NS_SUCCEEDED(method_(aOther, &val)) && val); \
+ \
+ bool retVal = false; \
+ return \
+ this == aOther || \
+ (NS_SUCCEEDED(method_(aOther, &retVal)) && retVal); \
+ }
+
%}
interface nsIURI;
interface nsIContentSecurityPolicy;
interface nsIDOMDocument;
[ptr] native JSContext(JSContext);
[ptr] native JSPrincipals(JSPrincipals);
@@ -36,25 +56,18 @@ interface nsIPrincipal : nsISerializable
boolean equals(in nsIPrincipal other);
/**
* Like equals, but takes document.domain changes into account.
*/
boolean equalsConsideringDomain(in nsIPrincipal other);
%{C++
- inline bool Equals(nsIPrincipal* aOther) {
- bool equal = false;
- return NS_SUCCEEDED(Equals(aOther, &equal)) && equal;
- }
-
- inline bool EqualsConsideringDomain(nsIPrincipal* aOther) {
- bool equal = false;
- return NS_SUCCEEDED(EqualsConsideringDomain(aOther, &equal)) && equal;
- }
+ DECL_FAST_INLINE_HELPER(Equals)
+ DECL_FAST_INLINE_HELPER(EqualsConsideringDomain)
%}
/**
* Returns a hash value for the principal.
*/
[noscript] readonly attribute unsigned long hashValue;
/**
@@ -96,30 +109,20 @@ interface nsIPrincipal : nsISerializable
/**
* Same as the subsumesConsideringDomain(), but ignores the first party
* domain in its originAttributes.
*/
boolean subsumesConsideringDomainIgnoringFPD(in nsIPrincipal other);
%{C++
- inline bool Subsumes(nsIPrincipal* aOther) {
- bool subsumes = false;
- return NS_SUCCEEDED(Subsumes(aOther, &subsumes)) && subsumes;
- }
-
- inline bool SubsumesConsideringDomain(nsIPrincipal* aOther) {
- bool subsumes = false;
- return NS_SUCCEEDED(SubsumesConsideringDomain(aOther, &subsumes)) && subsumes;
- }
-
- inline bool SubsumesConsideringDomainIgnoringFPD(nsIPrincipal* aOther) {
- bool subsumes = false;
- return NS_SUCCEEDED(SubsumesConsideringDomainIgnoringFPD(aOther, &subsumes)) && subsumes;
- }
+ DECL_FAST_INLINE_HELPER(Subsumes)
+ DECL_FAST_INLINE_HELPER(SubsumesConsideringDomain)
+ DECL_FAST_INLINE_HELPER(SubsumesConsideringDomainIgnoringFPD)
+#undef DECL_FAST_INLINE_HELPER
%}
/**
* Checks whether this principal is allowed to load the network resource
* located at the given URI under the same-origin policy. This means that
* codebase principals are only allowed to load resources from the same
* domain, the system principal is allowed to load anything, and null
* principals can only load URIs where they are the principal. This is