Bug 1445079 - Remove nsHTMLTags::sTagAtomTable[]. r=froydnj
authorNicholas Nethercote <nnethercote@mozilla.com>
Tue, 13 Mar 2018 10:14:43 +1100
changeset 766513 837b409fa5b4dfc230641376f53cdf7e18d66448
parent 766512 e08d2fc661202c8d30ba649df5d314b67882c951
child 766587 4a2985fcb2ac2c0a567d827c1ab3b53df0dda562
push id102337
push usernnethercote@mozilla.com
push dateMon, 12 Mar 2018 23:15:09 +0000
reviewersfroydnj
bugs1445079
milestone60.0a1
Bug 1445079 - Remove nsHTMLTags::sTagAtomTable[]. r=froydnj The atoms in nsHTMLTags are a subset of nsGkAtoms, which means that sTagAtomTable[] currently ends up holding duplicate pointers to the same static atoms. This patch removes sTagAtomTable[]. The only place that used sTagAtomTable[] was nsHTMLTags::AddRefTable(). It now instead calls NS_GetStaticAtom() to get the static atoms registered by nsGkAtoms. The patch also moves some checking of sTagNames from RegisterAtoms() (which is removed) to AddRefTable(). All this reduces the number of duplicate static atoms from 148 to 12. MozReview-Commit-ID: 14qXYeoorFr
layout/build/nsLayoutStatics.cpp
parser/htmlparser/nsHTMLTags.cpp
parser/htmlparser/nsHTMLTags.h
--- a/layout/build/nsLayoutStatics.cpp
+++ b/layout/build/nsLayoutStatics.cpp
@@ -150,17 +150,16 @@ nsLayoutStatics::Initialize()
   // Register all of our atoms once
   nsCSSAnonBoxes::AddRefAtoms();
   nsCSSPseudoClasses::AddRefAtoms();
   nsCSSPseudoElements::AddRefAtoms();
   nsCSSKeywords::AddRefTable();
   nsCSSProps::AddRefTable();
   nsColorNames::AddRefTable();
   nsGkAtoms::AddRefAtoms();
-  nsHTMLTags::RegisterAtoms();
   nsRDFAtoms::RegisterAtoms();
 
   NS_SetStaticAtomsDone();
 
   StartupJSEnvironment();
 
   nsGlobalWindowInner::Init();
   nsGlobalWindowOuter::Init();
--- a/parser/htmlparser/nsHTMLTags.cpp
+++ b/parser/htmlparser/nsHTMLTags.cpp
@@ -25,96 +25,63 @@ const char16_t* const nsHTMLTags::sTagNa
 #undef HTML_OTHER
 
 int32_t nsHTMLTags::gTableRefCount;
 nsHTMLTags::TagStringHash* nsHTMLTags::gTagTable;
 nsHTMLTags::TagAtomHash* nsHTMLTags::gTagAtomTable;
 
 #define NS_HTMLTAG_NAME_MAX_LENGTH 10
 
-// This would use NS_STATIC_ATOM_DEFN if it wasn't an array.
-nsStaticAtom* nsHTMLTags::sTagAtomTable[eHTMLTag_userdefined - 1];
-
-#define HTML_TAG(_tag, _classname, _interfacename) \
-  NS_STATIC_ATOM_BUFFER(_tag, #_tag)
-#define HTML_OTHER(_tag)
-#include "nsHTMLTagList.h"
-#undef HTML_TAG
-#undef HTML_OTHER
-
-/* static */ void
-nsHTMLTags::RegisterAtoms(void)
-{
-  // This would use NS_STATIC_ATOM_SETUP if it wasn't an array.
-  static const nsStaticAtomSetup sTagAtomSetup[] = {
-    #define HTML_TAG(_tag, _classname, _interfacename) \
-      { _tag##_buffer, &nsHTMLTags::sTagAtomTable[eHTMLTag_##_tag - 1] },
-    #define HTML_OTHER(_tag)
-    #include "nsHTMLTagList.h"
-    #undef HTML_TAG
-    #undef HTML_OTHER
-  };
-
-  NS_RegisterStaticAtoms(sTagAtomSetup);
-
-#if defined(DEBUG)
-  {
-    // let's verify that all names in the the table are lowercase...
-    for (int32_t i = 0; i < NS_HTML_TAG_MAX; ++i) {
-      nsAutoString temp1((char16_t*)sTagAtomSetup[i].mString);
-      nsAutoString temp2((char16_t*)sTagAtomSetup[i].mString);
-      ToLowerCase(temp1);
-      NS_ASSERTION(temp1.Equals(temp2), "upper case char in table");
-    }
-
-    // let's verify that all names in the unicode strings above are
-    // correct.
-    for (int32_t i = 0; i < NS_HTML_TAG_MAX; ++i) {
-      nsAutoString temp1(sTagNames[i]);
-      nsAutoString temp2((char16_t*)sTagAtomSetup[i].mString);
-      NS_ASSERTION(temp1.Equals(temp2), "Bad unicode tag name!");
-    }
-
-    // let's verify that NS_HTMLTAG_NAME_MAX_LENGTH is correct
-    uint32_t maxTagNameLength = 0;
-    for (int32_t i = 0; i < NS_HTML_TAG_MAX; ++i) {
-      uint32_t len = NS_strlen(sTagNames[i]);
-      maxTagNameLength = std::max(len, maxTagNameLength);
-    }
-    NS_ASSERTION(maxTagNameLength == NS_HTMLTAG_NAME_MAX_LENGTH,
-                 "NS_HTMLTAG_NAME_MAX_LENGTH not set correctly!");
-  }
-#endif
-}
-
 // static
 nsresult
 nsHTMLTags::AddRefTable(void)
 {
   if (gTableRefCount++ == 0) {
     NS_ASSERTION(!gTagTable && !gTagAtomTable, "pre existing hash!");
 
     gTagTable = new TagStringHash(64);
     gTagAtomTable = new TagAtomHash(64);
 
     // Fill in gTagTable with the above static char16_t strings as
     // keys and the value of the corresponding enum as the value in
     // the table.
 
     int32_t i;
+#ifdef DEBUG
+    uint32_t maxTagNameLength = 0;
+#endif
     for (i = 0; i < NS_HTML_TAG_MAX; ++i) {
       const char16_t* tagName = sTagNames[i];
       const nsHTMLTag tagValue = static_cast<nsHTMLTag>(i + 1);
       // We use AssignLiteral here to avoid a string copy. This is okay
       // because this is truly static data.
       nsString tmp;
       tmp.AssignLiteral(tagName, nsString::char_traits::length(tagName));
       gTagTable->Put(tmp, tagValue);
-      gTagAtomTable->Put(sTagAtomTable[i], tagValue);
+
+      // All the HTML tag names are static atoms within nsGkAtoms, and they are
+      // registered before this code is reached.
+      nsStaticAtom* atom = NS_GetStaticAtom(tmp);
+      MOZ_ASSERT(atom);
+      gTagAtomTable->Put(atom, tagValue);
+
+#ifdef DEBUG
+      // Verify that all tagNames are lowercase.
+      nsAutoString tmp2(tagName);
+      ToLowerCase(tmp2);
+      MOZ_ASSERT(tmp.Equals(tmp2));
+
+      maxTagNameLength = std::max(NS_strlen(tagName), maxTagNameLength);
+#endif
     }
+
+#ifdef DEBUG
+    // Verify that NS_HTMLTAG_NAME_MAX_LENGTH is correct.
+    MOZ_ASSERT(maxTagNameLength == NS_HTMLTAG_NAME_MAX_LENGTH);
+#endif
   }
 
   return NS_OK;
 }
 
 // static
 void
 nsHTMLTags::ReleaseTable(void)
--- a/parser/htmlparser/nsHTMLTags.h
+++ b/parser/htmlparser/nsHTMLTags.h
@@ -39,17 +39,16 @@ enum nsHTMLTag {
 // All tags before eHTMLTag_text are HTML tags
 #define NS_HTML_TAG_MAX int32_t(eHTMLTag_text - 1)
 
 class nsHTMLTags {
 public:
   using TagStringHash = nsDataHashtable<nsStringHashKey, nsHTMLTag>;
   using TagAtomHash = nsDataHashtable<nsPtrHashKey<nsAtom>, nsHTMLTag>;
 
-  static void RegisterAtoms(void);
   static nsresult AddRefTable(void);
   static void ReleaseTable(void);
 
   // Functions for converting string or atom to id
   static nsHTMLTag StringTagToId(const nsAString& aTagName);
   static nsHTMLTag AtomTagToId(nsAtom* aTagName)
   {
     return StringTagToId(nsDependentAtomString(aTagName));
@@ -71,18 +70,16 @@ public:
     return tag ? *tag : eHTMLTag_userdefined;
   }
 
 #ifdef DEBUG
   static void TestTagTable();
 #endif
 
 private:
-  // This would use NS_STATIC_ATOM_DECL if it wasn't an array.
-  static nsStaticAtom* sTagAtomTable[eHTMLTag_userdefined - 1];
   static const char16_t* const sTagNames[];
 
   static int32_t gTableRefCount;
   static TagStringHash* gTagTable;
   static TagAtomHash* gTagAtomTable;
 };
 
 #endif /* nsHTMLTags_h___ */