Skip to content

[libSyntax] Reenable caching of token nodes #18684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions lib/Syntax/SyntaxArena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ struct SyntaxArena::Implementation {
/// List of pointers to the allocated RawSyntax
std::vector<RawSyntax *> AllocatedRawSyntaxList;

/// Cached Tokens.
llvm::FoldingSet<RawSyntaxCacheNode> CachedTokens;

Implementation() = default;
void *Allocate(size_t size, size_t alignment) {
return Allocator.Allocate(size, alignment);
Expand Down Expand Up @@ -122,6 +125,38 @@ RC<RawSyntax> RawSyntax::getToken(SyntaxArena &Arena, tok TokKind,
llvm::ArrayRef<TriviaPiece> LeadingTrivia,
llvm::ArrayRef<TriviaPiece> TrailingTrivia) {

return RawSyntax::make(TokKind, Text, LeadingTrivia, TrailingTrivia,
SourcePresence::Present, &Arena);
// Determine whether this token is worth to cache.
if (
// Is string_literal with >16 length.
(TokKind == tok::string_literal && Text.size() > 16) ||
// Has leading comment trivia et al.
any_of(LeadingTrivia,
[](const syntax::TriviaPiece &T) { return T.getText().size(); }) ||
// Has trailing comment trivia et al.
any_of(TrailingTrivia,
[](const syntax::TriviaPiece &T) { return T.getText().size(); })) {

// Do not use cache.
return RawSyntax::make(TokKind, Text, LeadingTrivia, TrailingTrivia,
SourcePresence::Present, &Arena);
}

// This node is cacheable. Get or create.
auto &CachedTokens = Arena.Impl.CachedTokens;

llvm::FoldingSetNodeID ID;
RawSyntax::Profile(ID, TokKind, Text, LeadingTrivia, TrailingTrivia);

void *insertPos = nullptr;
if (auto existing = CachedTokens.FindNodeOrInsertPos(ID, insertPos))
// Found in the cache. Just return it.
return existing->get();

// Could not found in the cache. Create it.
auto Raw = RawSyntax::make(TokKind, Text, LeadingTrivia, TrailingTrivia,
SourcePresence::Present, &Arena);
auto IDRef = ID.Intern(Arena.getAllocator());
auto CacheNode = new (Arena) RawSyntaxCacheNode(Raw.get(), IDRef);
CachedTokens.InsertNode(CacheNode, insertPos);
return Raw;
}
18 changes: 9 additions & 9 deletions test/Syntax/Inputs/serialize_multiple_decls.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"id": 20,
"id": 18,
"kind": "SourceFile",
"layout": [
{
"id": 19,
"id": 17,
"kind": "CodeBlockItemList",
"layout": [
{
Expand Down Expand Up @@ -110,11 +110,11 @@
"presence": "Present"
},
{
"id": 16,
"id": 14,
"kind": "CodeBlockItem",
"layout": [
{
"id": 15,
"id": 13,
"kind": "StructDecl",
"layout": [
null,
Expand Down Expand Up @@ -157,11 +157,11 @@
null,
null,
{
"id": 14,
"id": 12,
"kind": "MemberDeclBlock",
"layout": [
{
"id": 11,
"id": 3,
"tokenKind": {
"kind": "l_brace"
},
Expand All @@ -170,13 +170,13 @@
"presence": "Present"
},
{
"id": 12,
"id": 11,
"kind": "MemberDeclList",
"layout": [],
"presence": "Present"
},
{
"id": 13,
"id": 5,
"tokenKind": {
"kind": "r_brace"
},
Expand Down Expand Up @@ -204,7 +204,7 @@
"presence": "Present"
},
{
"id": 18,
"id": 16,
"tokenKind": {
"kind": "eof",
"text": ""
Expand Down
46 changes: 23 additions & 23 deletions test/Syntax/Inputs/serialize_struct_decl.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"id": 41,
"id": 40,
"kind": "SourceFile",
"layout": [
{
"id": 40,
"id": 39,
"kind": "CodeBlockItemList",
"layout": [
{
"id": 37,
"id": 36,
"kind": "CodeBlockItem",
"layout": [
{
"id": 36,
"id": 35,
"kind": "StructDecl",
"layout": [
null,
Expand Down Expand Up @@ -66,7 +66,7 @@
null,
null,
{
"id": 35,
"id": 34,
"kind": "MemberDeclBlock",
"layout": [
{
Expand All @@ -79,7 +79,7 @@
"presence": "Present"
},
{
"id": 33,
"id": 32,
"kind": "MemberDeclList",
"layout": [
{
Expand Down Expand Up @@ -201,11 +201,11 @@
"presence": "Present"
},
{
"id": 32,
"id": 31,
"kind": "MemberDeclListItem",
"layout": [
{
"id": 31,
"id": 30,
"kind": "VariableDecl",
"layout": [
null,
Expand Down Expand Up @@ -234,11 +234,11 @@
"presence": "Present"
},
{
"id": 30,
"id": 29,
"kind": "PatternBindingList",
"layout": [
{
"id": 29,
"id": 28,
"kind": "PatternBinding",
"layout": [
{
Expand All @@ -264,11 +264,11 @@
"presence": "Present"
},
{
"id": 28,
"id": 27,
"kind": "TypeAnnotation",
"layout": [
{
"id": 18,
"id": 7,
"tokenKind": {
"kind": "colon"
},
Expand All @@ -282,11 +282,11 @@
"presence": "Present"
},
{
"id": 27,
"id": 26,
"kind": "SimpleTypeIdentifier",
"layout": [
{
"id": 19,
"id": 18,
"tokenKind": {
"kind": "identifier",
"text": "Array"
Expand All @@ -301,11 +301,11 @@
"presence": "Present"
},
{
"id": 26,
"id": 25,
"kind": "GenericArgumentClause",
"layout": [
{
"id": 20,
"id": 19,
"tokenKind": {
"kind": "l_angle"
},
Expand All @@ -319,19 +319,19 @@
"presence": "Present"
},
{
"id": 24,
"id": 23,
"kind": "GenericArgumentList",
"layout": [
{
"id": 23,
"id": 22,
"kind": "GenericArgument",
"layout": [
{
"id": 22,
"id": 21,
"kind": "SimpleTypeIdentifier",
"layout": [
{
"id": 21,
"id": 20,
"tokenKind": {
"kind": "identifier",
"text": "Int"
Expand All @@ -357,7 +357,7 @@
"presence": "Present"
},
{
"id": 25,
"id": 24,
"tokenKind": {
"kind": "r_angle"
},
Expand Down Expand Up @@ -394,7 +394,7 @@
"presence": "Present"
},
{
"id": 34,
"id": 33,
"tokenKind": {
"kind": "r_brace"
},
Expand Down Expand Up @@ -426,7 +426,7 @@
"presence": "Present"
},
{
"id": 39,
"id": 38,
"tokenKind": {
"kind": "eof",
"text": ""
Expand Down
Loading