Skip to content

Commit 2949775

Browse files
committed
[libSyntax] Remove incremental JSON transfer option
We were only keeping track of `RawSyntax` node IDs to incrementally transfer a syntax tree via JSON. However, AFAICT the incremental JSON transfer option has been superceeded by `SyntaxParseActions`, which are more efficient. So, let’s clean up and remove the `RawSyntax` node ID and JSON incremental transfer option. In places that still need a notion of `RawSyntax` identity (like determining the reused syntax regions), use the `RawSyntax`’s pointer instead of the manually created ID. In `incr_transfer_round_trip.py` always use the code path that uses the `SyntaxParseActions` and remove the transitional code that was still using the incremental JSON transfer but was never called.
1 parent fd8e349 commit 2949775

26 files changed

+75
-889
lines changed

include/swift/Parse/SyntaxParsingCache.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class SyntaxParsingCache {
6060
/// the source file that is now parsed incrementally
6161
llvm::SmallVector<SourceEdit, 4> Edits;
6262

63-
/// The IDs of all syntax nodes that got reused are collected in this vector.
64-
std::unordered_set<SyntaxNodeId> ReusedNodeIds;
63+
/// The \c RawSyntax nodes that got reused are collected in this vector.
64+
std::unordered_set<const RawSyntax *> ReusedNodes;
6565

6666
public:
6767
SyntaxParsingCache(SourceFileSyntax OldSyntaxTree)
@@ -83,8 +83,8 @@ class SyntaxParsingCache {
8383
/// reused for a new syntax tree.
8484
llvm::Optional<Syntax> lookUp(size_t NewPosition, SyntaxKind Kind);
8585

86-
const std::unordered_set<SyntaxNodeId> &getReusedNodeIds() const {
87-
return ReusedNodeIds;
86+
const std::unordered_set<const RawSyntax *> &getReusedNodes() const {
87+
return ReusedNodes;
8888
}
8989

9090
/// Get the source regions of the new source file, represented by

include/swift/Syntax/RawSyntax.h

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -151,25 +151,16 @@ struct SyntaxPrintOptions {
151151
bool PrintTrivialNodeKind = false;
152152
};
153153

154-
typedef unsigned SyntaxNodeId;
155-
156154
/// RawSyntax - the strictly immutable, shared backing nodes for all syntax.
157155
///
158156
/// This is implementation detail - do not expose it in public API.
159157
class RawSyntax final
160158
: private llvm::TrailingObjects<RawSyntax, const RawSyntax *> {
161159
friend TrailingObjects;
162160

163-
/// The ID that shall be used for the next node that is created and does not
164-
/// have a manually specified id
165-
static SyntaxNodeId NextFreeNodeId;
166-
167161
/// The \c SyntaxArena in which this node was allocated.
168162
SyntaxArena *Arena;
169163

170-
/// An ID of this node that is stable across incremental parses
171-
SyntaxNodeId NodeId;
172-
173164
/// Number of bytes this node takes up spelled out in the source code.
174165
/// Always 0 if the node is missing.
175166
uint32_t TextLength;
@@ -227,7 +218,7 @@ class RawSyntax final
227218
template <typename ChildrenIteratorType>
228219
RawSyntax(SyntaxKind Kind, ChildrenIteratorType ChildrenIt,
229220
uint32_t NumChildren, SourcePresence Presence,
230-
const RC<SyntaxArena> &Arena, llvm::Optional<SyntaxNodeId> NodeId)
221+
const RC<SyntaxArena> &Arena)
231222
: Arena(Arena.get()), TextLength(0 /*computed in body*/),
232223
Presence(Presence), IsToken(false),
233224
Bits(LayoutData{NumChildren,
@@ -254,13 +245,6 @@ class RawSyntax final
254245

255246
*TrailingChildren = Child;
256247
}
257-
258-
if (NodeId.hasValue()) {
259-
this->NodeId = NodeId.getValue();
260-
NextFreeNodeId = std::max(this->NodeId + 1, NextFreeNodeId);
261-
} else {
262-
this->NodeId = NextFreeNodeId++;
263-
}
264248
}
265249

266250
/// Constructor for creating token nodes
@@ -270,8 +254,7 @@ class RawSyntax final
270254
/// the caller needs to assure that the NodeId has not been used yet.
271255
RawSyntax(tok TokKind, StringRef Text, size_t TextLength,
272256
StringRef LeadingTrivia, StringRef TrailingTrivia,
273-
SourcePresence Presence, const RC<SyntaxArena> &Arena,
274-
llvm::Optional<SyntaxNodeId> NodeId)
257+
SourcePresence Presence, const RC<SyntaxArena> &Arena)
275258
: Arena(Arena.get()), TextLength(uint32_t(TextLength)),
276259
Presence(Presence), IsToken(true),
277260
Bits(TokenData{LeadingTrivia.data(), Text.data(), TrailingTrivia.data(),
@@ -285,13 +268,6 @@ class RawSyntax final
285268
assert(TextLength ==
286269
LeadingTrivia.size() + Text.size() + TrailingTrivia.size());
287270
}
288-
289-
if (NodeId.hasValue()) {
290-
this->NodeId = NodeId.getValue();
291-
NextFreeNodeId = std::max(this->NodeId + 1, NextFreeNodeId);
292-
} else {
293-
this->NodeId = NextFreeNodeId++;
294-
}
295271
Arena->copyStringToArenaIfNecessary(Bits.Token.LeadingTrivia,
296272
Bits.Token.LeadingTriviaLength);
297273
Arena->copyStringToArenaIfNecessary(Bits.Token.TokenText,
@@ -320,52 +296,47 @@ class RawSyntax final
320296
template <typename ChildrenIteratorType>
321297
static const RawSyntax *
322298
make(SyntaxKind Kind, ChildrenIteratorType ChildrenIt, size_t NumChildren,
323-
SourcePresence Presence, const RC<SyntaxArena> &Arena,
324-
llvm::Optional<SyntaxNodeId> NodeId = llvm::None) {
299+
SourcePresence Presence, const RC<SyntaxArena> &Arena) {
325300
assert(Arena && "RawSyntax nodes must always be allocated in an arena");
326301
auto size = totalSizeToAlloc<const RawSyntax *>(NumChildren);
327302
void *data = Arena->Allocate(size, alignof(RawSyntax));
328303
return new (data)
329-
RawSyntax(Kind, ChildrenIt, NumChildren, Presence, Arena, NodeId);
304+
RawSyntax(Kind, ChildrenIt, NumChildren, Presence, Arena);
330305
}
331306

332307
/// Convenience constructor to create a raw "layout" syntax node from an
333308
/// \c llvm::ArrayRef containing the children.
334309
static const RawSyntax *
335310
make(SyntaxKind Kind, llvm::ArrayRef<const RawSyntax *> Children,
336-
SourcePresence Presence, const RC<SyntaxArena> &Arena,
337-
llvm::Optional<SyntaxNodeId> NodeId = llvm::None) {
338-
return make(Kind, Children.begin(), Children.size(), Presence, Arena,
339-
NodeId);
311+
SourcePresence Presence, const RC<SyntaxArena> &Arena) {
312+
return make(Kind, Children.begin(), Children.size(), Presence, Arena);
340313
}
341314

342315
/// Make a raw "token" syntax node.
343316
static const RawSyntax *
344317
make(tok TokKind, StringRef Text, size_t TextLength, StringRef LeadingTrivia,
345318
StringRef TrailingTrivia, SourcePresence Presence,
346-
const RC<SyntaxArena> &Arena,
347-
llvm::Optional<SyntaxNodeId> NodeId = llvm::None) {
319+
const RC<SyntaxArena> &Arena) {
348320
assert(Arena && "RawSyntax nodes must always be allocated in an arena");
349321
auto size = totalSizeToAlloc<const RawSyntax *>(0);
350322
void *data = Arena->Allocate(size, alignof(RawSyntax));
351323
return new (data) RawSyntax(TokKind, Text, TextLength, LeadingTrivia,
352-
TrailingTrivia, Presence, Arena, NodeId);
324+
TrailingTrivia, Presence, Arena);
353325
}
354326

355327
/// Make a raw "token" syntax node that was allocated in \p Arena.
356328
static const RawSyntax *
357329
makeAndCalcLength(tok TokKind, StringRef Text, StringRef LeadingTrivia,
358330
StringRef TrailingTrivia, SourcePresence Presence,
359-
const RC<SyntaxArena> &Arena,
360-
llvm::Optional<SyntaxNodeId> NodeId = llvm::None) {
331+
const RC<SyntaxArena> &Arena) {
361332
size_t TextLength = 0;
362333
if (Presence != SourcePresence::Missing) {
363334
TextLength += LeadingTrivia.size();
364335
TextLength += Text.size();
365336
TextLength += TrailingTrivia.size();
366337
}
367338
return make(TokKind, Text, TextLength, LeadingTrivia, TrailingTrivia,
368-
Presence, Arena, NodeId);
339+
Presence, Arena);
369340
}
370341

371342
/// Make a missing raw "layout" syntax node.
@@ -413,9 +384,6 @@ class RawSyntax final
413384
}
414385
}
415386

416-
/// Get an ID for this node that is stable across incremental parses
417-
SyntaxNodeId getId() const { return NodeId; }
418-
419387
/// Returns true if the node is "missing" in the source (i.e. it was
420388
/// expected (or optional) but not written.
421389
bool isMissing() const { return getPresence() == SourcePresence::Missing; }

include/swift/Syntax/Serialization/SyntaxDeserialization.h

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -177,30 +177,18 @@ template <> struct MappingTraits<const swift::RawSyntax *> {
177177
in.mapRequired("trailingTrivia", trailingTrivia);
178178
swift::SourcePresence presence;
179179
in.mapRequired("presence", presence);
180-
/// FIXME: This is a workaround for existing bug from llvm yaml parser
181-
/// which would raise error when deserializing number with trailing
182-
/// character like "1\n". See https://bugs.llvm.org/show_bug.cgi?id=15505
183-
StringRef nodeIdString;
184-
in.mapRequired("id", nodeIdString);
185-
unsigned nodeId = std::atoi(nodeIdString.data());
186180
value = swift::RawSyntax::makeAndCalcLength(
187181
tokenKind, text, leadingTrivia, trailingTrivia, presence,
188-
input->Arena, nodeId);
182+
input->Arena);
189183
} else {
190184
swift::SyntaxKind kind;
191185
in.mapRequired("kind", kind);
192186
std::vector<const swift::RawSyntax *> layout;
193187
in.mapRequired("layout", layout);
194188
swift::SourcePresence presence;
195189
in.mapRequired("presence", presence);
196-
/// FIXME: This is a workaround for existing bug from llvm yaml parser
197-
/// which would raise error when deserializing number with trailing
198-
/// character like "1\n". See https://bugs.llvm.org/show_bug.cgi?id=15505
199-
StringRef nodeIdString;
200-
in.mapRequired("id", nodeIdString);
201-
unsigned nodeId = std::atoi(nodeIdString.data());
202190
value =
203-
swift::RawSyntax::make(kind, layout, presence, input->Arena, nodeId);
191+
swift::RawSyntax::make(kind, layout, presence, input->Arena);
204192
}
205193
}
206194
};

include/swift/Syntax/Serialization/SyntaxSerialization.h

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@
2828
namespace swift {
2929
namespace json {
3030

31-
/// The associated value will be interpreted as \c bool. If \c true the node IDs
32-
/// will not be included in the serialized JSON.
33-
static void *DontSerializeNodeIdsUserInfoKey = &DontSerializeNodeIdsUserInfoKey;
34-
35-
/// The user info key pointing to a std::unordered_set of IDs of nodes that
36-
/// shall be omitted when the tree gets serialized
37-
static void *OmitNodesUserInfoKey = &OmitNodesUserInfoKey;
38-
3931
/// Serialization traits for SourcePresence.
4032
template <>
4133
struct ScalarReferenceTraits<syntax::SourcePresence> {
@@ -150,22 +142,6 @@ struct ObjectTraits<TokenDescription> {
150142
template <>
151143
struct ObjectTraits<const syntax::RawSyntax> {
152144
static void mapping(Output &out, const syntax::RawSyntax &value) {
153-
bool dontSerializeIds =
154-
(bool)out.getUserInfo()[DontSerializeNodeIdsUserInfoKey];
155-
if (!dontSerializeIds) {
156-
auto nodeId = value.getId();
157-
out.mapRequired("id", nodeId);
158-
}
159-
160-
auto omitNodes =
161-
(std::unordered_set<unsigned> *)out.getUserInfo()[OmitNodesUserInfoKey];
162-
163-
if (omitNodes && omitNodes->count(value.getId()) > 0) {
164-
bool omitted = true;
165-
out.mapRequired("omitted", omitted);
166-
return;
167-
}
168-
169145
if (value.isToken()) {
170146
auto tokenKind = value.getTokenKind();
171147
auto text = value.getTokenText();

include/swift/Syntax/Syntax.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,6 @@ class Syntax {
201201
/// Get the shared raw syntax.
202202
const RawSyntax *getRaw() const;
203203

204-
/// Get an ID for this node that is stable across incremental parses
205-
SyntaxNodeId getId() const { return getRaw()->getId(); }
206-
207204
/// Get the number of child nodes in this piece of syntax, not including
208205
/// tokens.
209206
size_t getNumChildren() const;
@@ -370,13 +367,6 @@ class SyntaxRef {
370367
/// Get the kind of syntax.
371368
SyntaxKind getKind() const { return getRaw()->getKind(); }
372369

373-
/// Get an ID for the \c RawSyntax node backing this \c Syntax which is
374-
/// stable across incremental parses.
375-
/// Note that this is different from the \c AbsoluteRawSyntax's \c NodeId,
376-
/// which uniquely identifies this node in the tree, but is not stable across
377-
/// incremental parses.
378-
SyntaxNodeId getId() const { return getRaw()->getId(); }
379-
380370
/// Return the number of bytes this node takes when spelled out in the source,
381371
/// including trivia.
382372
size_t getTextLength() const { return getRaw()->getTextLength(); }

lib/Parse/SyntaxParsingCache.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ llvm::Optional<Syntax> SyntaxParsingCache::lookUp(size_t NewPosition,
111111

112112
auto Node = lookUpFrom(OldSyntaxTree, /*NodeStart=*/0, *OldPosition, Kind);
113113
if (Node.hasValue()) {
114-
ReusedNodeIds.insert(Node->getId());
114+
ReusedNodes.insert(Node->getRaw());
115115
}
116116
return Node;
117117
}
@@ -120,16 +120,16 @@ std::vector<SyntaxReuseRegion>
120120
SyntaxParsingCache::getReusedRegions(const SourceFileSyntax &SyntaxTree) const {
121121
/// Determines the reused source regions from reused syntax node IDs
122122
class ReusedRegionsCollector : public SyntaxVisitor {
123-
std::unordered_set<SyntaxNodeId> ReusedNodeIds;
123+
std::unordered_set<const RawSyntax *> ReusedNodes;
124124
std::vector<SyntaxReuseRegion> ReusedRegions;
125125

126-
bool didReuseNode(SyntaxNodeId NodeId) {
127-
return ReusedNodeIds.count(NodeId) > 0;
126+
bool didReuseNode(const RawSyntax *Node) {
127+
return ReusedNodes.count(Node) > 0;
128128
}
129129

130130
public:
131-
ReusedRegionsCollector(std::unordered_set<SyntaxNodeId> ReusedNodeIds)
132-
: ReusedNodeIds(ReusedNodeIds) {}
131+
ReusedRegionsCollector(std::unordered_set<const RawSyntax *> ReusedNodes)
132+
: ReusedNodes(ReusedNodes) {}
133133

134134
const std::vector<SyntaxReuseRegion> &getReusedRegions() {
135135
std::sort(ReusedRegions.begin(), ReusedRegions.end(),
@@ -141,7 +141,7 @@ SyntaxParsingCache::getReusedRegions(const SourceFileSyntax &SyntaxTree) const {
141141
}
142142

143143
void visit(Syntax Node) override {
144-
if (didReuseNode(Node.getId())) {
144+
if (didReuseNode(Node.getRaw())) {
145145
// Node has been reused, add it to the list
146146
auto Start = Node.getAbsolutePositionBeforeLeadingTrivia();
147147
auto End = Node.getAbsoluteEndPositionAfterTrailingTrivia();
@@ -158,7 +158,7 @@ SyntaxParsingCache::getReusedRegions(const SourceFileSyntax &SyntaxTree) const {
158158
}
159159
};
160160

161-
ReusedRegionsCollector ReuseRegionsCollector(getReusedNodeIds());
161+
ReusedRegionsCollector ReuseRegionsCollector(getReusedNodes());
162162
ReuseRegionsCollector.collectReusedRegions(SyntaxTree);
163163
return ReuseRegionsCollector.getReusedRegions();
164164
}

lib/Syntax/RawSyntax.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ Trivia lexTrivia(StringRef TriviaStr) {
8787
return SyntaxTrivia;
8888
}
8989

90-
// FIXME: If we want thread-safety for tree creation, this needs to be atomic.
91-
unsigned RawSyntax::NextFreeNodeId = 1;
92-
9390
Trivia RawSyntax::getLeadingTriviaPieces() const {
9491
return lexTrivia(getLeadingTrivia());
9592
}

0 commit comments

Comments
 (0)