Skip to content

Commit 2e5c869

Browse files
committed
[libSyntax] Don't cache token nodes
It turns out that caching is actually more expensive than just creating new nodes.
1 parent 682f3a7 commit 2e5c869

File tree

9 files changed

+85
-278
lines changed

9 files changed

+85
-278
lines changed

include/swift/SyntaxParse/SyntaxTreeCreator.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "llvm/ADT/StringRef.h"
1919

2020
namespace swift {
21-
class RawSyntaxTokenCache;
2221
class SourceManager;
2322
class SyntaxParsingCache;
2423
class SourceFile;
@@ -49,10 +48,6 @@ class SyntaxTreeCreator: public SyntaxParseActions {
4948
/// tree.
5049
SyntaxParsingCache *SyntaxCache;
5150

52-
/// Tokens nodes that have already been created and may be reused in other
53-
/// parts of the syntax tree.
54-
std::unique_ptr<RawSyntaxTokenCache> TokenCache;
55-
5651
public:
5752
SyntaxTreeCreator(SourceManager &SM, unsigned bufferID,
5853
SyntaxParsingCache *syntaxCache,

lib/SyntaxParse/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
set_swift_llvm_is_available()
22
add_swift_host_library(swiftSyntaxParse STATIC
3-
RawSyntaxTokenCache.cpp
43
SyntaxTreeCreator.cpp)
54
target_link_libraries(swiftSyntaxParse PRIVATE
65
swiftParse

lib/SyntaxParse/RawSyntaxTokenCache.cpp

Lines changed: 0 additions & 87 deletions
This file was deleted.

lib/SyntaxParse/RawSyntaxTokenCache.h

Lines changed: 0 additions & 97 deletions
This file was deleted.

lib/SyntaxParse/SyntaxTreeCreator.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@
2222
#include "swift/AST/Module.h"
2323
#include "swift/AST/SourceFile.h"
2424
#include "swift/Basic/OwnedString.h"
25-
#include "RawSyntaxTokenCache.h"
2625

2726
using namespace swift;
2827
using namespace swift::syntax;
2928

3029
SyntaxTreeCreator::SyntaxTreeCreator(SourceManager &SM, unsigned bufferID,
3130
SyntaxParsingCache *syntaxCache,
3231
RC<syntax::SyntaxArena> arena)
33-
: SM(SM), BufferID(bufferID),
34-
Arena(std::move(arena)),
35-
SyntaxCache(syntaxCache),
36-
TokenCache(new RawSyntaxTokenCache()) {
32+
: SM(SM), BufferID(bufferID), Arena(std::move(arena)),
33+
SyntaxCache(syntaxCache) {
3734
StringRef BufferContent = SM.getEntireTextForBuffer(BufferID);
3835
const char *Data = BufferContent.data();
3936
Arena->copyStringToArenaIfNecessary(Data, BufferContent.size());
@@ -126,9 +123,9 @@ OpaqueSyntaxNode SyntaxTreeCreator::recordToken(tok tokenKind,
126123
StringRef trailingTriviaText = ArenaSourceBuffer.substr(
127124
trailingTriviaStartOffset, trailingTrivia.size());
128125

129-
auto raw =
130-
TokenCache->getToken(Arena, tokenKind, range.getByteLength(), tokenText,
131-
leadingTriviaText, trailingTriviaText);
126+
auto raw = RawSyntax::make(tokenKind, tokenText, range.getByteLength(),
127+
leadingTriviaText, trailingTriviaText,
128+
SourcePresence::Present, Arena);
132129
return static_cast<OpaqueSyntaxNode>(raw);
133130
}
134131

test/Syntax/Inputs/serialize_multiple_decls.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"id": 17,
2+
"id": 19,
33
"kind": "SourceFile",
44
"layout": [
55
{
6-
"id": 16,
6+
"id": 18,
77
"kind": "CodeBlockItemList",
88
"layout": [
99
{
@@ -78,11 +78,11 @@
7878
"presence": "Present"
7979
},
8080
{
81-
"id": 14,
81+
"id": 16,
8282
"kind": "CodeBlockItem",
8383
"layout": [
8484
{
85-
"id": 13,
85+
"id": 15,
8686
"kind": "StructDecl",
8787
"layout": [
8888
null,
@@ -110,11 +110,11 @@
110110
null,
111111
null,
112112
{
113-
"id": 12,
113+
"id": 14,
114114
"kind": "MemberDeclBlock",
115115
"layout": [
116116
{
117-
"id": 3,
117+
"id": 11,
118118
"tokenKind": {
119119
"kind": "l_brace"
120120
},
@@ -123,13 +123,13 @@
123123
"presence": "Present"
124124
},
125125
{
126-
"id": 11,
126+
"id": 12,
127127
"kind": "MemberDeclList",
128128
"layout": [],
129129
"presence": "Present"
130130
},
131131
{
132-
"id": 5,
132+
"id": 13,
133133
"tokenKind": {
134134
"kind": "r_brace"
135135
},
@@ -152,7 +152,7 @@
152152
"presence": "Present"
153153
},
154154
{
155-
"id": 15,
155+
"id": 17,
156156
"tokenKind": {
157157
"kind": "eof",
158158
"text": ""

0 commit comments

Comments
 (0)