Skip to content

[libSyntax] Incremental Syntax Parsing #16340

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 45 commits into from
May 22, 2018

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented May 3, 2018

This is a work in progress PR to add incremental syntax parsing to the Swift compiler. It accompanies the proposal here and discussion thread here.

@@ -232,18 +232,24 @@ class RawSyntax final
/// memory.
unsigned ManualMemory : 1;
};
enum { NumRawSyntaxBits = bitmax(NumSyntaxKindBits, 8) + 1 };
enum { NumRawSyntaxBits = bitmax(NumSyntaxKindBits, 8) + 1 + 1 };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

@ahoppen ahoppen force-pushed the incremental-syntax-parsing branch 4 times, most recently from 01d51a8 to b53baca Compare May 4, 2018 23:19
@@ -15,6 +15,45 @@
using namespace swift;
using namespace swift::syntax;

ArrayRef<TriviaPiece> getNextChildsLeadingTrivia(const Syntax *Node) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to make this getNextChild, because that's a traversal I've found very necessary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I will keep it in mind when I refactor these methods to provide a (hopefully faster) cache based lookup.

@ahoppen ahoppen force-pushed the incremental-syntax-parsing branch 6 times, most recently from 95a5195 to 6c1434b Compare May 11, 2018 21:58
@ahoppen
Copy link
Member Author

ahoppen commented May 15, 2018

@swift-ci Please smoke test

@ahoppen
Copy link
Member Author

ahoppen commented May 21, 2018

@swift-ci Please smoke test

@ahoppen ahoppen force-pushed the incremental-syntax-parsing branch from 7a1b1ee to 59bfeb3 Compare May 21, 2018 17:01
@ahoppen
Copy link
Member Author

ahoppen commented May 21, 2018

@swift-ci Please smoke test

1 similar comment
@ahoppen
Copy link
Member Author

ahoppen commented May 21, 2018

@swift-ci Please smoke test

@ahoppen ahoppen force-pushed the incremental-syntax-parsing branch from 59bfeb3 to a0776e6 Compare May 21, 2018 18:37
@ahoppen
Copy link
Member Author

ahoppen commented May 21, 2018

@swift-ci Please smoke test

@@ -434,6 +442,28 @@ class RawSyntax final
return getLayout()[Index];
}

/// Return the number of bytes this node takes when spelled out in the source
size_t getTextLength() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we re-use accumulateAbsolutePosition to calculate the length?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using accumulateAbsolutePosition now to compute the length of a token. For all other nodes I'm still computing the value while constructing the tree because it is faster.

Copy link
Contributor

@nkcsgexi nkcsgexi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good overall! Some comments so far.

@@ -71,6 +71,8 @@ bool isTokenKind(SyntaxKind Kind);

bool isUnknownKind(SyntaxKind Kind);

bool shallBeOmittedWhenNoChildren(SyntaxKind Kind);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I presume only Parser can omit. Can we move the definition to Parser? leaving it in the node may mislead clients.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to create a gyb file in the parser for this. It is currently still nice and gyb-free.

Maybe we could add this function to the swift namespace instead of the swift::syntax one and call it parserShallOmitIfNoChildern

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it works too, as long as we make it explicit that it's a parser thing rather than for syntax nodes in general.

//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: 2018

llvm::Optional<Syntax> lookUp(size_t NewPosition, SyntaxKind Kind);

/// Turn recording of reused ranges on
void recordReuseInformation() { RecordReuseInformation = true; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better name is setRecordReuseInformation().

@@ -112,6 +113,9 @@ class alignas(1 << SyntaxAlignInBits) SyntaxParsingContext {
// Reference to the
SyntaxParsingContext *&CtxtHolder;

/// A cache of nodes that can be reused when creating the current syntax tree
SyntaxParsingCache *SyntaxParsingCache = nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could move this instance to RootContextData. It will make it clear that all SyntaxParsingContext share a single cache entry. Same with Arena.

return 0;
}
// Disable this node and push the cached nodes into the storage
disable();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of calling disable(), can we define another AccumulationMode called LoadedFromCache to be more specific? Probably, we can assert LoadedFromCache context doesn't spawn any child context.

@ahoppen ahoppen force-pushed the incremental-syntax-parsing branch 2 times, most recently from 8040453 to 8cc0d25 Compare May 21, 2018 22:37
@ahoppen ahoppen changed the title [WIP] Incremental Syntax Parsing [libSyntax] Incremental Syntax Parsing May 21, 2018
@ahoppen ahoppen force-pushed the incremental-syntax-parsing branch from 8cc0d25 to e49f2b2 Compare May 21, 2018 22:54
@ahoppen
Copy link
Member Author

ahoppen commented May 21, 2018

@swift-ci Please smoke test

@nkcsgexi nkcsgexi requested a review from rintaro May 21, 2018 23:38
@ahoppen ahoppen force-pushed the incremental-syntax-parsing branch from 4eb19c6 to e2e8972 Compare May 22, 2018 16:02
@ahoppen
Copy link
Member Author

ahoppen commented May 22, 2018

@swift-ci Please smoke test

@ahoppen ahoppen force-pushed the incremental-syntax-parsing branch from e2e8972 to 4e44e68 Compare May 22, 2018 16:08
@ahoppen
Copy link
Member Author

ahoppen commented May 22, 2018

@swift-ci Please smoke test

@ahoppen ahoppen merged commit 9d6ff6d into swiftlang:master May 22, 2018
@harlanhaskins
Copy link
Contributor

🎉🎉🎉

@ahoppen ahoppen deleted the incremental-syntax-parsing branch June 11, 2018 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants