Skip to content

Commit 28f5f79

Browse files
committed
[libSyntax] Don't reference count RawSyntax
Instead, only reference count the SyntaxArena that the RawSyntax nodes live in. The user of RawSyntax nodes must guarantee that the SyntaxArena stays alive as long as the RawSyntax nodes are being accessed. During parse time, the SyntaxTreeCreator holds on to the SyntaxArena in which it creates RawSyntax nodes. When inspecting a syntax tree, the root SyntaxData node keeps the SyntaxArena alive. The change should be mostly invisible to the users of the public libSyntax API. This change significantly decreases the overall reference-counting overhead. Since we were not able to free individual RawSyntax nodes anyway, performing the reference-counting on the level of the SyntaxArena feels natural.
1 parent 00d09f7 commit 28f5f79

25 files changed

+390
-325
lines changed

include/swift/Parse/Parser.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,10 +1817,12 @@ bool isKeywordPossibleDeclStart(const Token &Tok);
18171817

18181818
/// Lex and return a vector of `TokenSyntax` tokens, which include
18191819
/// leading and trailing trivia.
1820-
std::vector<std::pair<RC<syntax::RawSyntax>, syntax::AbsoluteOffsetPosition>>
1820+
std::vector<
1821+
std::pair<const syntax::RawSyntax *, syntax::AbsoluteOffsetPosition>>
18211822
tokenizeWithTrivia(const LangOptions &LangOpts, const SourceManager &SM,
1822-
unsigned BufferID, unsigned Offset = 0,
1823-
unsigned EndOffset = 0, DiagnosticEngine *Diags = nullptr);
1823+
unsigned BufferID, const RC<SyntaxArena> &Arena,
1824+
unsigned Offset = 0, unsigned EndOffset = 0,
1825+
DiagnosticEngine *Diags = nullptr);
18241826
} // end namespace swift
18251827

18261828
#endif

include/swift/Syntax/AbsoluteRawSyntax.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ class SyntaxIndexInTree {
3232

3333
/// Assuming that this index points to the start of \p Raw, advance it so that
3434
/// it points to the next sibling of \p Raw.
35-
SyntaxIndexInTree advancedBy(const RC<RawSyntax> &Raw) const;
35+
SyntaxIndexInTree advancedBy(const RawSyntax *Raw) const;
3636

3737
/// Assuming that this index points to the next sibling of \p Raw, reverse it
3838
/// so that it points to the start of \p Raw.
39-
SyntaxIndexInTree reversedBy(const RC<RawSyntax> &Raw) const;
39+
SyntaxIndexInTree reversedBy(const RawSyntax *Raw) const;
4040

4141
/// Advance this index to point to its first immediate child.
4242
SyntaxIndexInTree advancedToFirstChild() const;
@@ -84,14 +84,14 @@ class SyntaxIdentifier {
8484

8585
/// Assuming that this identifier points to the start of \p Raw, advance it so
8686
/// that it points to the next sibling of \p Raw.
87-
SyntaxIdentifier advancedBy(const RC<RawSyntax> &Raw) const {
87+
SyntaxIdentifier advancedBy(const RawSyntax *Raw) const {
8888
auto NewIndexInTree = IndexInTree.advancedBy(Raw);
8989
return SyntaxIdentifier(RootId, NewIndexInTree);
9090
}
9191

9292
/// Assuming that this identifier points to the next sibling of \p Raw,
9393
/// reverse it so that it points to the start of \p Raw.
94-
SyntaxIdentifier reversedBy(const RC<RawSyntax> &Raw) const {
94+
SyntaxIdentifier reversedBy(const RawSyntax *Raw) const {
9595
auto NewIndexInTree = IndexInTree.reversedBy(Raw);
9696
return SyntaxIdentifier(RootId, NewIndexInTree);
9797
}
@@ -138,11 +138,11 @@ class AbsoluteSyntaxPosition {
138138

139139
/// Assuming that this position points to the start of \p Raw, advance it so
140140
/// that it points to the next sibling of \p Raw.
141-
AbsoluteSyntaxPosition advancedBy(const RC<RawSyntax> &Raw) const;
141+
AbsoluteSyntaxPosition advancedBy(const RawSyntax *Raw) const;
142142

143143
/// Assuming that this position points to the next sibling of \p Raw, reverse
144144
/// it so that it points to the start of \p Raw.
145-
AbsoluteSyntaxPosition reversedBy(const RC<RawSyntax> &Raw) const;
145+
AbsoluteSyntaxPosition reversedBy(const RawSyntax *Raw) const;
146146

147147
/// Get the position of the node's first immediate child.
148148
AbsoluteSyntaxPosition advancedToFirstChild() const {
@@ -189,15 +189,15 @@ class AbsoluteSyntaxInfo {
189189

190190
/// Assuming that this info points to the start of \p Raw, advance it so
191191
/// that it points to the next sibling of \p Raw.
192-
AbsoluteSyntaxInfo advancedBy(const RC<RawSyntax> &Raw) const {
192+
AbsoluteSyntaxInfo advancedBy(const RawSyntax *Raw) const {
193193
auto NewNodeId = NodeId.advancedBy(Raw);
194194
auto NewPosition = Position.advancedBy(Raw);
195195
return AbsoluteSyntaxInfo(NewPosition, NewNodeId);
196196
}
197197

198198
/// Assuming that this info points to the next sibling of \p Raw, reverse
199199
/// it so that it points to the start of \p Raw.
200-
AbsoluteSyntaxInfo reversedBy(const RC<RawSyntax> &Raw) const {
200+
AbsoluteSyntaxInfo reversedBy(const RawSyntax *Raw) const {
201201
auto NewNodeId = NodeId.reversedBy(Raw);
202202
auto NewPosition = Position.reversedBy(Raw);
203203
return AbsoluteSyntaxInfo(NewPosition, NewNodeId);
@@ -214,20 +214,20 @@ class AbsoluteSyntaxInfo {
214214
/// A \c RawSyntax node that is enrichted with information of its position
215215
/// within the syntax tree it lives in.
216216
struct AbsoluteRawSyntax {
217-
const RC<RawSyntax> Raw;
217+
const RawSyntax *Raw;
218218
const AbsoluteSyntaxInfo Info;
219219

220220
public:
221-
AbsoluteRawSyntax(const RC<RawSyntax> &Raw, AbsoluteSyntaxInfo Info)
221+
AbsoluteRawSyntax(const RawSyntax *Raw, AbsoluteSyntaxInfo Info)
222222
: Raw(Raw), Info(Info) {}
223223

224224
/// Construct a \c AbsoluteRawSyntax for a \c RawSyntax node that represents
225225
/// the syntax tree's root.
226-
static AbsoluteRawSyntax forRoot(const RC<RawSyntax> &Raw) {
226+
static AbsoluteRawSyntax forRoot(const RawSyntax *Raw) {
227227
return AbsoluteRawSyntax(Raw, AbsoluteSyntaxInfo::forRoot());
228228
}
229229

230-
const RC<RawSyntax> &getRaw() const { return Raw; }
230+
const RawSyntax *getRaw() const { return Raw; }
231231

232232
AbsoluteSyntaxInfo getInfo() const { return Info; }
233233

@@ -245,7 +245,7 @@ struct AbsoluteRawSyntax {
245245
/// - the \p NewRaw as the backing storage
246246
/// - the \p NewRootId as the RootId
247247
AbsoluteRawSyntax
248-
replacingSelf(const RC<RawSyntax> &NewRaw,
248+
replacingSelf(const RawSyntax *NewRaw,
249249
SyntaxIdentifier::RootIdType NewRootId) const {
250250
SyntaxIdentifier NewNodeId(NewRootId, Info.getNodeId().getIndexInTree());
251251
AbsoluteSyntaxInfo NewInfo(Info.getPosition(), NewNodeId);

0 commit comments

Comments
 (0)