Bug 1426494: Devirtualize StyleScope::AsNode. r=smaug
authorEmilio Cobos Álvarez <emilio@crisal.io>
Wed, 20 Dec 2017 06:44:24 +0100
changeset 449035 f28babf65d7277182a3ab72825e1a16641c0fee5
parent 449034 62aa4a2039b99cc7919fdbc594ce7785faf1015b
child 449036 abba86fa9a5b02a3a058f767efca7c90b3db4c0b
push id8527
push userCallek@gmail.com
push dateThu, 11 Jan 2018 21:05:50 +0000
treeherdermozilla-beta@95342d212a7a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1426494
milestone59.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 1426494: Devirtualize StyleScope::AsNode. r=smaug MozReview-Commit-ID: 2nDEI5aIu46
dom/base/ShadowRoot.cpp
dom/base/ShadowRoot.h
dom/base/StyleScope.cpp
dom/base/StyleScope.h
dom/base/nsDocument.cpp
dom/base/nsIDocument.h
--- a/dom/base/ShadowRoot.cpp
+++ b/dom/base/ShadowRoot.cpp
@@ -51,16 +51,17 @@ NS_INTERFACE_MAP_END_INHERITING(Document
 
 NS_IMPL_ADDREF_INHERITED(ShadowRoot, DocumentFragment)
 NS_IMPL_RELEASE_INHERITED(ShadowRoot, DocumentFragment)
 
 ShadowRoot::ShadowRoot(Element* aElement, bool aClosed,
                        already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
                        nsXBLPrototypeBinding* aProtoBinding)
   : DocumentFragment(aNodeInfo)
+  , StyleScope(*this)
   , mProtoBinding(aProtoBinding)
   , mInsertionPointChanged(false)
   , mIsComposedDocParticipant(false)
 {
   SetHost(aElement);
   mMode = aClosed ? ShadowRootMode::Closed : ShadowRootMode::Open;
 
   // Nodes in a shadow tree should never store a value
--- a/dom/base/ShadowRoot.h
+++ b/dom/base/ShadowRoot.h
@@ -51,22 +51,16 @@ public:
   {
     return mMode;
   }
   bool IsClosed() const
   {
     return mMode == ShadowRootMode::Closed;
   }
 
-  // StyleScope.
-  nsINode& AsNode() final
-  {
-    return *this;
-  }
-
   // [deprecated] Shadow DOM v0
   void AddToIdTable(Element* aElement, nsAtom* aId);
   void RemoveFromIdTable(Element* aElement, nsAtom* aId);
   void InsertSheet(StyleSheet* aSheet, nsIContent* aLinkingContent);
   void RemoveSheet(StyleSheet* aSheet);
   bool ApplyAuthorStyles();
   void SetApplyAuthorStyles(bool aApplyAuthorStyles);
   StyleSheetList* StyleSheets()
--- a/dom/base/StyleScope.cpp
+++ b/dom/base/StyleScope.cpp
@@ -5,16 +5,26 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "StyleScope.h"
 #include "mozilla/dom/StyleSheetList.h"
 
 namespace mozilla {
 namespace dom {
 
+StyleScope::StyleScope(mozilla::dom::ShadowRoot& aShadowRoot)
+  : mAsNode(aShadowRoot)
+  , mKind(Kind::ShadowRoot)
+{}
+
+StyleScope::StyleScope(nsIDocument& aDoc)
+  : mAsNode(aDoc)
+  , mKind(Kind::Document)
+{}
+
 StyleScope::~StyleScope()
 {
 }
 
 StyleSheetList&
 StyleScope::EnsureDOMStyleSheets()
 {
   if (!mDOMStyleSheets) {
--- a/dom/base/StyleScope.h
+++ b/dom/base/StyleScope.h
@@ -22,22 +22,33 @@ class StyleSheetList;
  * A class meant to be shared by ShadowRoot and Document, that holds a list of
  * stylesheets.
  *
  * TODO(emilio, bug 1418159): In the future this should hold most of the
  * relevant style state, this should allow us to fix bug 548397.
  */
 class StyleScope
 {
+  enum class Kind {
+    Document,
+    ShadowRoot,
+  };
+
 public:
-  virtual nsINode& AsNode() = 0;
+  explicit StyleScope(nsIDocument&);
+  explicit StyleScope(mozilla::dom::ShadowRoot&);
+
+  nsINode& AsNode()
+  {
+    return mAsNode;
+  }
 
   const nsINode& AsNode() const
   {
-    return const_cast<StyleScope&>(*this).AsNode();
+    return mAsNode;
   }
 
   StyleSheet* SheetAt(size_t aIndex) const
   {
     return mStyleSheets.SafeElementAt(aIndex);
   }
 
   size_t SheetCount() const
@@ -67,15 +78,18 @@ public:
 
   StyleSheetList& EnsureDOMStyleSheets();
 
   ~StyleScope();
 
 protected:
   nsTArray<RefPtr<mozilla::StyleSheet>> mStyleSheets;
   RefPtr<mozilla::dom::StyleSheetList> mDOMStyleSheets;
+
+  nsINode& mAsNode;
+  const Kind mKind;
 };
 
 }
 
 }
 
 #endif
--- a/dom/base/nsDocument.cpp
+++ b/dom/base/nsDocument.cpp
@@ -1399,16 +1399,17 @@ struct nsIDocument::FrameRequest
 
 static already_AddRefed<mozilla::dom::NodeInfo> nullNodeInfo;
 
 // ==================================================================
 // =
 // ==================================================================
 nsIDocument::nsIDocument()
   : nsINode(nullNodeInfo),
+    StyleScope(*this),
     mReferrerPolicySet(false),
     mReferrerPolicy(mozilla::net::RP_Unset),
     mBlockAllMixedContent(false),
     mBlockAllMixedContentPreloads(false),
     mUpgradeInsecureRequests(false),
     mUpgradeInsecurePreloads(false),
     mCharacterSet(WINDOWS_1252_ENCODING),
     mCharacterSetSource(0),
--- a/dom/base/nsIDocument.h
+++ b/dom/base/nsIDocument.h
@@ -1332,21 +1332,16 @@ public:
    * supplied by add-ons or by the app (Firefox OS or Firefox Mobile, for
    * example), since their sheets should override built-in sheets.
    *
    * TODO We can get rid of the whole concept of delayed loading if we fix
    * bug 77999.
    */
   virtual void EnsureOnDemandBuiltInUASheet(mozilla::StyleSheet* aSheet) = 0;
 
-  nsINode& AsNode() final
-  {
-    return *this;
-  }
-
   mozilla::dom::StyleSheetList* StyleSheets()
   {
     return &StyleScope::EnsureDOMStyleSheets();
   }
 
   /**
    * Insert a sheet at a particular spot in the stylesheet list (zero-based)
    * @param aSheet the sheet to insert