Skip to content

Make RawTokenKind a trivial enum #1334

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
Feb 12, 2023

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Feb 9, 2023

This changes RawTokenKind to be a trivial enum again, after I introduced an associated value for keyword a few months ago. To avoid repeatedly comparing strings or accessing Keyword.defaultText and thus regressing in performance, before matching a lexeme against a bunc of TokenSpec that refer to keywords, we pre-compute the keyword it might represent by wrapping it in PrepareForKeywordMatch.

Performance-wise I have seen slight improvements on on the order of ~2%-3% locally but nothing that I would eyeball as being statistically relevant - but there doesn’t seem to be a performance regression.

rdar://104659543

@ahoppen ahoppen changed the title Make RawTokenKind a trivial enum 🚥 #1333 Make RawTokenKind a trivial enum Feb 10, 2023
@ahoppen ahoppen force-pushed the ahoppen/rawtokenkind-trivial-enum branch from 219196b to 7090074 Compare February 10, 2023 06:18
@ahoppen
Copy link
Member Author

ahoppen commented Feb 10, 2023

@swift-ci Please test

@ahoppen ahoppen requested review from rintaro and bnbarham February 10, 2023 06:18
Comment on lines +15 to +17
/// Pre-computes the keyword a lexeme might represent. This makes matching
/// a lexeme that has been converted into `PrepareForKeyword` match cheaper to
/// match against multiple `TokenSpec` that assume a keyword.
Copy link
Contributor

Choose a reason for hiding this comment

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

This would remove the main advantage I see in not just converting to keyword in the lexer - ie. for identifier cases, we don't need to do the string -> keyword conversion. But with PrepareForKeywordMatch we're now ignoring that. So why not just always convert to a keyword in the lexer once and simplify the rest?

Copy link
Member Author

Choose a reason for hiding this comment

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

My impression is that the entire “how should we represent things that might be keywords in the lexer” discussion is related to this PR but it’s fundamentally an orthogonal issue.

IIUC we all agree that RawTokenKind should be a trivial enum, so I would propose that we merge this PR and continue the discussion of how keywords should be represented in a follow-up PR. WDYT?

Copy link
Contributor

@bnbarham bnbarham Feb 12, 2023

Choose a reason for hiding this comment

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

To be clear, I'm not arguing against merging this PR. Yes, I agree we want a trivial enum here. I was just
responding to your comment in the other PR about this addressing it 😅

@@ -201,7 +201,7 @@ extension Parser {
} else {
unexpectedBeforeDetail = nil
detail = RawTokenSyntax(
missing: .keyword(.set),
missing: .keyword,
text: "set",
Copy link
Contributor

Choose a reason for hiding this comment

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

Keyword.set.defaultText? (and the try/etc ones)

Or alternatively have a RawTokenSyntax(mising:arena:) where missing is a Keyword that does automatically.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea. I added missingToken(Keyword) as a method on Parser.

@ahoppen ahoppen force-pushed the ahoppen/rawtokenkind-trivial-enum branch from 7090074 to fdbc043 Compare February 11, 2023 09:15
@ahoppen
Copy link
Member Author

ahoppen commented Feb 11, 2023

@swift-ci Please test

This changes `RawTokenKind` to be a trivial enum again, after I introduced an associated value for `keyword` a few months ago. To avoid repeatedly comparing strings or accessing `Keyword.defaultText` and thus regressing in performance, before matching a lexeme against a bunc of `TokenSpec` that refer to keywords, we pre-compute the keyword it might represent by wrapping it in `PrepareForKeywordMatch`.

Performance-wise I have seen slight improvements on on the order of ~2%-3% locally but nothing that I would eyeball as being statistically relevant - but there doesn’t seem to be a performance regression.

rdar://104659543
@ahoppen ahoppen force-pushed the ahoppen/rawtokenkind-trivial-enum branch from fdbc043 to 667de33 Compare February 12, 2023 07:41
@ahoppen
Copy link
Member Author

ahoppen commented Feb 12, 2023

@swift-ci Please test

@ahoppen ahoppen merged commit 5fbdde7 into swiftlang:main Feb 12, 2023
@ahoppen ahoppen deleted the ahoppen/rawtokenkind-trivial-enum branch February 12, 2023 19:18
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.

2 participants