Skip to content

[ASTGen] Start using 'TokenSyntax.rawText' #70201

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
Dec 5, 2023

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Dec 4, 2023

TokenSyntax.text causes instantiation of a heap allocated Swift.String for each access. TokenSyntax.rawText is faster than that because it's just a reference to a slice of the pre-allocated buffer. Also, converting to BridgedString is simplar, and guaranteed to be free, unlike bridging via String.withUTF8(_:)

@rintaro
Copy link
Member Author

rintaro commented Dec 4, 2023

swiftlang/swift-syntax#2383
@swift-ci Please smoke test

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Ideally, we would expose the TokenSpecSets for the keywords. Then ASTGen could exhaustively switch over all the choices that such a token might have.

https://github.com/apple/swift-syntax/blob/046a60308c5444f13e1a4cf6354a161207c38f47/Sources/SwiftParser/generated/Parser%2BTokenSpecSet.swift#L2091-L2093

`TokenSyntax.text` causes intiation of heap allocated `Swift.String` for
each access. `TokenSyntax.rawText` is faster than that because it's just
a reference to the pre-allocated buffer. Also, converting to
`BridgedString` is simplar, and guaranteed to be free, unlike bridging
via `String.withUTF8(_:)`
@rintaro
Copy link
Member Author

rintaro commented Dec 4, 2023

swiftlang/swift-syntax#2383
@swift-ci Please smoke test

@rintaro
Copy link
Member Author

rintaro commented Dec 4, 2023

Ideally, we would expose the TokenSpecSets for the keywords. Then ASTGen could exhaustively switch over all the choices that such a token might have.

Maybe, we should factor out the portion of it. i.e. not relying on Lexeme.

@ahoppen
Copy link
Member

ahoppen commented Dec 4, 2023

I agree that some design work would need to be done here. That’s why we haven’t exposed these enums as public API yet.

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