Skip to content

Reduce usage of currentToken #1909

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

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Jul 17, 2023

The motivation for these changes is to reduce the usage of Parser.currentToken because in general the at primitives should be preferred to checking the currentToken itself. But independent of these motivations, I think the changes simplify the codebase on their own.

  • Change LoopProgress.evaluate to take a TokenConsumer (i.e. Parser or Lookahead) instead of currentToken. This shortens the call sides slightly
  • Add atStartOfLine to Parser to shorten self.currentToken.isAtStartOfLine to self.atStartOfLine

I measured performance and there’s no significant performance regression associated with this change (based on parsing MovieSwiftUI).

@ahoppen ahoppen requested a review from bnbarham July 17, 2023 13:53
@ahoppen
Copy link
Member Author

ahoppen commented Jul 17, 2023

@swift-ci Please test

/// In assert builds, this traps if the loop has not made any parser progress in between two iterations,
/// ie. it checks if the parser's `currentToken` has changed in between two calls to `evaluate`.
/// In non-assert builds, `evaluate()` returns `false` if the loop has not made progress, thus aborting the loop.
mutating func evaluate(_ currentToken: Lexer.Lexeme) -> Bool {
mutating func evaluate(_ parser: some TokenConsumer) -> Bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Parser and Parser.Lookahead are both structs right? Do we really want to copy that in here on every evaluate?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, interesting. I actually just played around with it again and was able to improve performance by 0.5% with a @inline(__always) entry function. Not entirely sure why, but 🏎️

Copy link
Contributor

Choose a reason for hiding this comment

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

My preference here would be a Parser function eg. hasProgressed that takes a LoopProgressCondition instead. The latter probably isn't the correct name any more either though 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

Done. I kept the name as LoopProgressCondition because I didn’t have a better idea and it does describe what the condition is being used for.

@ahoppen ahoppen force-pushed the ahoppen/parser-instead-of-currenttoken branch 2 times, most recently from 3d0d3d1 to 4418dc6 Compare July 17, 2023 16:30
@ahoppen
Copy link
Member Author

ahoppen commented Jul 17, 2023

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Jul 17, 2023

@swift-ci Please test Windows

@ahoppen ahoppen force-pushed the ahoppen/parser-instead-of-currenttoken branch from 4418dc6 to 0028719 Compare July 18, 2023 16:34
@ahoppen
Copy link
Member Author

ahoppen commented Jul 18, 2023

@swift-ci Please test

@ahoppen ahoppen force-pushed the ahoppen/parser-instead-of-currenttoken branch from 0028719 to f9982f0 Compare July 18, 2023 20:48
@ahoppen
Copy link
Member Author

ahoppen commented Jul 18, 2023

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Jul 18, 2023

@swift-ci Please test Windows

Copy link
Contributor

@bnbarham bnbarham left a comment

Choose a reason for hiding this comment

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

PR message is out of date now, but otherwise LGTM

@ahoppen ahoppen force-pushed the ahoppen/parser-instead-of-currenttoken branch from f9982f0 to 526ce24 Compare July 22, 2023 04:50
@ahoppen
Copy link
Member Author

ahoppen commented Jul 22, 2023

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Jul 22, 2023

@swift-ci Please test Windows

@ahoppen ahoppen force-pushed the ahoppen/parser-instead-of-currenttoken branch from 526ce24 to e85b5d2 Compare July 23, 2023 03:07
@ahoppen ahoppen force-pushed the ahoppen/parser-instead-of-currenttoken branch from e85b5d2 to 0554638 Compare July 23, 2023 03:11
@ahoppen
Copy link
Member Author

ahoppen commented Jul 23, 2023

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Jul 23, 2023

@swift-ci Please test Windows

@ahoppen ahoppen merged commit e8b12a6 into swiftlang:main Jul 23, 2023
@ahoppen ahoppen deleted the ahoppen/parser-instead-of-currenttoken branch July 23, 2023 15:26
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