Skip to content

Commit 03ce7b0

Browse files
committed
Bug 1392185 - Remove dynamic HTML5 atoms. r=njn,hsivonen
This is a rebase + manual refcounting on some places, + cleanup of the original patch in the bug. Co-authored-by: Nicholas Nethercote <nnethercote@mozilla.com> Differential Revision: https://phabricator.services.mozilla.com/D11035
1 parent ed9deed commit 03ce7b0

16 files changed

+113
-210
lines changed

parser/html/nsHtml5AtomTable.cpp

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,6 @@
55
#include "nsHtml5AtomTable.h"
66
#include "nsThreadUtils.h"
77

8-
nsHtml5AtomEntry::nsHtml5AtomEntry(KeyTypePointer aStr)
9-
: nsStringHashKey(aStr)
10-
, mAtom(nsDynamicAtom::Create(*aStr))
11-
{
12-
}
13-
14-
nsHtml5AtomEntry::nsHtml5AtomEntry(nsHtml5AtomEntry&& aOther)
15-
: nsStringHashKey(std::move(aOther))
16-
, mAtom(nullptr)
17-
{
18-
MOZ_ASSERT_UNREACHABLE("nsHtml5AtomTable is broken; tried to copy an entry");
19-
}
20-
21-
nsHtml5AtomEntry::~nsHtml5AtomEntry()
22-
{
23-
nsDynamicAtom::Destroy(mAtom);
24-
}
25-
268
nsHtml5AtomTable::nsHtml5AtomTable()
279
: mRecentlyUsedParserAtoms{}
2810
{
@@ -37,27 +19,18 @@ nsAtom*
3719
nsHtml5AtomTable::GetAtom(const nsAString& aKey)
3820
{
3921
#ifdef DEBUG
40-
{
41-
MOZ_ASSERT(mPermittedLookupEventTarget->IsOnCurrentThread());
42-
}
22+
MOZ_ASSERT(mPermittedLookupEventTarget->IsOnCurrentThread());
4323
#endif
4424

4525
uint32_t index = mozilla::HashString(aKey) % RECENTLY_USED_PARSER_ATOMS_SIZE;
46-
nsAtom* cachedAtom = mRecentlyUsedParserAtoms[index];
47-
if (cachedAtom && cachedAtom->Equals(aKey)) {
48-
return cachedAtom;
49-
}
50-
51-
nsStaticAtom* atom = NS_GetStaticAtom(aKey);
52-
if (atom) {
53-
mRecentlyUsedParserAtoms[index] = atom;
54-
return atom;
55-
}
56-
nsHtml5AtomEntry* entry = mTable.PutEntry(aKey);
57-
if (!entry) {
58-
return nullptr;
26+
if (nsAtom* atom = mRecentlyUsedParserAtoms[index]) {
27+
if (atom->Equals(aKey)) {
28+
return atom;
29+
}
5930
}
6031

61-
mRecentlyUsedParserAtoms[index] = entry->GetAtom();
62-
return entry->GetAtom();
32+
RefPtr<nsAtom> atom = NS_Atomize(aKey);
33+
nsAtom* ret = atom.get();
34+
mRecentlyUsedParserAtoms[index] = atom.forget();
35+
return ret;
6336
}

parser/html/nsHtml5AtomTable.h

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,6 @@
1212

1313
#define RECENTLY_USED_PARSER_ATOMS_SIZE 31
1414

15-
class nsHtml5AtomEntry : public nsStringHashKey
16-
{
17-
public:
18-
explicit nsHtml5AtomEntry(KeyTypePointer aStr);
19-
nsHtml5AtomEntry(nsHtml5AtomEntry&& aOther);
20-
~nsHtml5AtomEntry();
21-
inline nsAtom* GetAtom() { return mAtom; }
22-
23-
private:
24-
nsDynamicAtom* mAtom;
25-
};
26-
2715
/**
2816
* nsHtml5AtomTable provides non-locking lookup and creation of atoms for
2917
* nsHtml5Parser or nsHtml5StreamParser.
@@ -74,9 +62,9 @@ class nsHtml5AtomTable
7462
nsHtml5AtomTable();
7563
~nsHtml5AtomTable();
7664

77-
/**
78-
* Obtains the atom for the given string in the scope of this atom table.
79-
*/
65+
// NOTE: We rely on mRecentlyUsedParserAtoms keeping alive the returned atom,
66+
// but the caller is responsible to take a reference before calling GetAtom
67+
// again.
8068
nsAtom* GetAtom(const nsAString& aKey);
8169

8270
/**
@@ -87,7 +75,6 @@ class nsHtml5AtomTable
8775
for (uint32_t i = 0; i < RECENTLY_USED_PARSER_ATOMS_SIZE; ++i) {
8876
mRecentlyUsedParserAtoms[i] = nullptr;
8977
}
90-
mTable.Clear();
9178
}
9279

9380
#ifdef DEBUG
@@ -98,8 +85,7 @@ class nsHtml5AtomTable
9885
#endif
9986

10087
private:
101-
nsTHashtable<nsHtml5AtomEntry> mTable;
102-
nsAtom* mRecentlyUsedParserAtoms[RECENTLY_USED_PARSER_ATOMS_SIZE];
88+
RefPtr<nsAtom> mRecentlyUsedParserAtoms[RECENTLY_USED_PARSER_ATOMS_SIZE];
10389
#ifdef DEBUG
10490
nsCOMPtr<nsISerialEventTarget> mPermittedLookupEventTarget;
10591
#endif

parser/html/nsHtml5AttributeEntry.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class nsHtml5AttributeEntry final
7171
if (!local->IsStatic()) {
7272
nsAutoString str;
7373
local->ToString(str);
74-
local = aInterner->GetAtom(str);
74+
nsAtom* local = aInterner->GetAtom(str);
7575
clone.mLocals[0] = local;
7676
clone.mLocals[1] = local;
7777
clone.mLocals[2] = local;
@@ -81,8 +81,8 @@ class nsHtml5AttributeEntry final
8181
}
8282

8383
private:
84-
nsAtom* mLocals[3];
85-
nsAtom* mPrefixes[3];
84+
RefPtr<nsAtom> mLocals[3];
85+
RefPtr<nsAtom> mPrefixes[3];
8686
int32_t mUris[3];
8787
int32_t mLine;
8888
nsHtml5String mValue;

parser/html/nsHtml5AttributeName.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,53 +58,53 @@ int32_t* nsHtml5AttributeName::ALL_NO_NS = 0;
5858
int32_t* nsHtml5AttributeName::XMLNS_NS = 0;
5959
int32_t* nsHtml5AttributeName::XML_NS = 0;
6060
int32_t* nsHtml5AttributeName::XLINK_NS = 0;
61-
nsAtom** nsHtml5AttributeName::ALL_NO_PREFIX = 0;
62-
nsAtom** nsHtml5AttributeName::XMLNS_PREFIX = 0;
63-
nsAtom** nsHtml5AttributeName::XLINK_PREFIX = 0;
64-
nsAtom** nsHtml5AttributeName::XML_PREFIX = 0;
65-
nsAtom**
61+
nsStaticAtom** nsHtml5AttributeName::ALL_NO_PREFIX = 0;
62+
nsStaticAtom** nsHtml5AttributeName::XMLNS_PREFIX = 0;
63+
nsStaticAtom** nsHtml5AttributeName::XLINK_PREFIX = 0;
64+
nsStaticAtom** nsHtml5AttributeName::XML_PREFIX = 0;
65+
RefPtr<nsAtom>*
6666
nsHtml5AttributeName::SVG_DIFFERENT(nsAtom* name, nsAtom* camel)
6767
{
68-
nsAtom** arr = new nsAtom*[4];
68+
RefPtr<nsAtom>* arr = new RefPtr<nsAtom>[4];
6969
arr[0] = name;
7070
arr[1] = name;
7171
arr[2] = camel;
7272
return arr;
7373
}
7474

75-
nsAtom**
75+
RefPtr<nsAtom>*
7676
nsHtml5AttributeName::MATH_DIFFERENT(nsAtom* name, nsAtom* camel)
7777
{
78-
nsAtom** arr = new nsAtom*[4];
78+
RefPtr<nsAtom>* arr = new RefPtr<nsAtom>[4];
7979
arr[0] = name;
8080
arr[1] = camel;
8181
arr[2] = name;
8282
return arr;
8383
}
8484

85-
nsAtom**
85+
RefPtr<nsAtom>*
8686
nsHtml5AttributeName::COLONIFIED_LOCAL(nsAtom* name, nsAtom* suffix)
8787
{
88-
nsAtom** arr = new nsAtom*[4];
88+
RefPtr<nsAtom>* arr = new RefPtr<nsAtom>[4];
8989
arr[0] = name;
9090
arr[1] = suffix;
9191
arr[2] = suffix;
9292
return arr;
9393
}
9494

95-
nsAtom**
95+
RefPtr<nsAtom>*
9696
nsHtml5AttributeName::SAME_LOCAL(nsAtom* name)
9797
{
98-
nsAtom** arr = new nsAtom*[4];
98+
RefPtr<nsAtom>* arr = new RefPtr<nsAtom>[4];
9999
arr[0] = name;
100100
arr[1] = name;
101101
arr[2] = name;
102102
return arr;
103103
}
104104

105105
nsHtml5AttributeName::nsHtml5AttributeName(int32_t* uri,
106-
nsAtom** local,
107-
nsAtom** prefix)
106+
RefPtr<nsAtom>* local,
107+
nsStaticAtom** prefix)
108108
: uri(uri)
109109
, local(local)
110110
, prefix(prefix)
@@ -148,7 +148,7 @@ nsHtml5AttributeName::getLocal(int32_t mode)
148148
return local[mode];
149149
}
150150

151-
nsAtom*
151+
nsStaticAtom*
152152
nsHtml5AttributeName::getPrefix(int32_t mode)
153153
{
154154
return prefix[mode];
@@ -767,19 +767,19 @@ nsHtml5AttributeName::initializeStatics()
767767
XLINK_NS[0] = kNameSpaceID_None;
768768
XLINK_NS[1] = kNameSpaceID_XLink;
769769
XLINK_NS[2] = kNameSpaceID_XLink;
770-
ALL_NO_PREFIX = new nsAtom*[3];
770+
ALL_NO_PREFIX = new nsStaticAtom*[3];
771771
ALL_NO_PREFIX[0] = nullptr;
772772
ALL_NO_PREFIX[1] = nullptr;
773773
ALL_NO_PREFIX[2] = nullptr;
774-
XMLNS_PREFIX = new nsAtom*[3];
774+
XMLNS_PREFIX = new nsStaticAtom*[3];
775775
XMLNS_PREFIX[0] = nullptr;
776776
XMLNS_PREFIX[1] = nsGkAtoms::xmlns;
777777
XMLNS_PREFIX[2] = nsGkAtoms::xmlns;
778-
XLINK_PREFIX = new nsAtom*[3];
778+
XLINK_PREFIX = new nsStaticAtom*[3];
779779
XLINK_PREFIX[0] = nullptr;
780780
XLINK_PREFIX[1] = nsGkAtoms::xlink;
781781
XLINK_PREFIX[2] = nsGkAtoms::xlink;
782-
XML_PREFIX = new nsAtom*[3];
782+
XML_PREFIX = new nsStaticAtom*[3];
783783
XML_PREFIX[0] = nullptr;
784784
XML_PREFIX[1] = nsGkAtoms::xml;
785785
XML_PREFIX[2] = nsGkAtoms::xml;

parser/html/nsHtml5AttributeName.h

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,18 @@ class nsHtml5AttributeName
6363
static int32_t* XML_NS;
6464
static int32_t* XLINK_NS;
6565
public:
66-
static nsAtom** ALL_NO_PREFIX;
66+
static nsStaticAtom** ALL_NO_PREFIX;
67+
6768
private:
68-
static nsAtom** XMLNS_PREFIX;
69-
static nsAtom** XLINK_PREFIX;
70-
static nsAtom** XML_PREFIX;
71-
static nsAtom** SVG_DIFFERENT(nsAtom* name, nsAtom* camel);
72-
static nsAtom** MATH_DIFFERENT(nsAtom* name, nsAtom* camel);
73-
static nsAtom** COLONIFIED_LOCAL(nsAtom* name, nsAtom* suffix);
69+
static nsStaticAtom** XMLNS_PREFIX;
70+
static nsStaticAtom** XLINK_PREFIX;
71+
static nsStaticAtom** XML_PREFIX;
72+
static RefPtr<nsAtom>* SVG_DIFFERENT(nsAtom* name, nsAtom* camel);
73+
static RefPtr<nsAtom>* MATH_DIFFERENT(nsAtom* name, nsAtom* camel);
74+
static RefPtr<nsAtom>* COLONIFIED_LOCAL(nsAtom* name, nsAtom* suffix);
75+
7476
public:
75-
static nsAtom** SAME_LOCAL(nsAtom* name);
77+
static RefPtr<nsAtom>* SAME_LOCAL(nsAtom* name);
7678
inline static int32_t levelOrderBinarySearch(jArray<int32_t, int32_t> data,
7779
int32_t key)
7880
{
@@ -154,10 +156,13 @@ class nsHtml5AttributeName
154156

155157
private:
156158
int32_t* uri;
157-
nsAtom** local;
158-
nsAtom** prefix;
159+
RefPtr<nsAtom>* local;
160+
nsStaticAtom** prefix;
159161
bool custom;
160-
nsHtml5AttributeName(int32_t* uri, nsAtom** local, nsAtom** prefix);
162+
nsHtml5AttributeName(int32_t* uri,
163+
RefPtr<nsAtom>* local,
164+
nsStaticAtom** prefix);
165+
161166
public:
162167
nsHtml5AttributeName();
163168
inline bool isInterned() { return !custom; }
@@ -174,7 +179,7 @@ class nsHtml5AttributeName
174179
~nsHtml5AttributeName();
175180
int32_t getUri(int32_t mode);
176181
nsAtom* getLocal(int32_t mode);
177-
nsAtom* getPrefix(int32_t mode);
182+
nsStaticAtom* getPrefix(int32_t mode);
178183
bool equalsAnother(nsHtml5AttributeName* another);
179184
static nsHtml5AttributeName* ATTR_ALT;
180185
static nsHtml5AttributeName* ATTR_DIR;

parser/html/nsHtml5ElementName.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ class nsHtml5ElementName
7676
static const int32_t OPTIONAL_END_TAG = (1 << 23);
7777

7878
private:
79-
nsAtom* name;
80-
nsAtom* camelCaseName;
79+
RefPtr<nsAtom> name;
80+
RefPtr<nsAtom> camelCaseName;
8181
mozilla::dom::HTMLContentCreatorFunction htmlCreator;
8282
mozilla::dom::SVGContentCreatorFunction svgCreator;
8383
public:

parser/html/nsHtml5Portability.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ nsHtml5Portability::newLocalNameFromBuffer(char16_t* buf,
1717
return interner->GetAtom(nsDependentSubstring(buf, buf + length));
1818
}
1919

20+
nsAtom*
21+
nsHtml5Portability::newLocalFromLocal(nsAtom* local,
22+
nsHtml5AtomTable* interner)
23+
{
24+
// FIXME(emilio): This function should be removed.
25+
return local;
26+
}
27+
2028
static bool
2129
ContainsWhiteSpace(mozilla::Span<char16_t> aSpan)
2230
{
@@ -86,19 +94,6 @@ nsHtml5Portability::newCharArrayFromString(nsHtml5String string)
8694
return arr;
8795
}
8896

89-
nsAtom*
90-
nsHtml5Portability::newLocalFromLocal(nsAtom* local, nsHtml5AtomTable* interner)
91-
{
92-
MOZ_ASSERT(local, "Atom was null.");
93-
MOZ_ASSERT(interner, "Atom table was null");
94-
if (!local->IsStatic()) {
95-
nsAutoString str;
96-
local->ToString(str);
97-
local = interner->GetAtom(str);
98-
}
99-
return local;
100-
}
101-
10297
bool
10398
nsHtml5Portability::localEqualsBuffer(nsAtom* local,
10499
char16_t* buf,

parser/html/nsHtml5StackNode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ class nsHtml5StackNode
6161
public:
6262
int32_t idxInTreeBuilder;
6363
int32_t flags;
64-
nsAtom* name;
65-
nsAtom* popName;
64+
RefPtr<nsAtom> name;
65+
RefPtr<nsAtom> popName;
6666
int32_t ns;
6767
nsIContentHandle* node;
6868
nsHtml5HtmlAttributes* attributes;

parser/html/nsHtml5Tokenizer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ class nsHtml5Tokenizer
282282
nsHtml5AttributeName* attributeName;
283283
private:
284284
nsHtml5AttributeName* nonInternedAttributeName;
285-
nsAtom* doctypeName;
285+
RefPtr<nsAtom> doctypeName;
286286
nsHtml5String publicIdentifier;
287287
nsHtml5String systemIdentifier;
288288
nsHtml5HtmlAttributes* attributes;

parser/html/nsHtml5TreeBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ class nsHtml5TreeBuilder : public nsAHtml5TreeBuilderState
293293
bool scriptingEnabled;
294294
bool needToDropLF;
295295
bool fragment;
296-
nsAtom* contextName;
296+
RefPtr<nsAtom> contextName;
297297
int32_t contextNamespace;
298298
nsIContentHandle* contextNode;
299299
autoJArray<int32_t, int32_t> templateModeStack;

0 commit comments

Comments
 (0)