Skip to content

Optimize rustc_lexer #91393

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 3, 2021
Merged

Conversation

Julian-Wollersberger
Copy link
Contributor

The cursor.first() method in rustc_lexer now calls the chars.next() method instead of chars.nth_char(0).

This allows LLVM to optimize the code better. The biggest win is that eat_while() is now fully inlined and generates better assembly. This improves the lexer's performance by 35% in a micro-benchmark I made (Lexing all 18MB of code in the compiler directory). But lexing is only a small part of the overall compilation time, so I don't know how significant it is.

Big thanks to criterion and cargo asm.

@rust-highfive
Copy link
Contributor

r? @petrochenkov

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 30, 2021
@the8472
Copy link
Member

the8472 commented Nov 30, 2021

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 30, 2021
@bors
Copy link
Collaborator

bors commented Nov 30, 2021

⌛ Trying commit 89bc19127c3cd88ad0ecae2957206172993729fe with merge f5bcd39ff4304330b2f8d73eced51275ccac2db6...

@bors
Copy link
Collaborator

bors commented Nov 30, 2021

☀️ Try build successful - checks-actions
Build commit: f5bcd39ff4304330b2f8d73eced51275ccac2db6 (f5bcd39ff4304330b2f8d73eced51275ccac2db6)

@rust-timer
Copy link
Collaborator

Queued f5bcd39ff4304330b2f8d73eced51275ccac2db6 with parent 1c02878, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f5bcd39ff4304330b2f8d73eced51275ccac2db6): comparison url.

Summary: This change led to moderate relevant improvements 🎉 in compiler performance.

  • Moderate improvement in instruction counts (up to -0.9% on incr-unchanged builds of coercions)

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR led to changes in compiler perf.

@bors rollup=never
@rustbot label: +S-waiting-on-review -S-waiting-on-perf -perf-regression

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 30, 2021
@Julian-Wollersberger
Copy link
Contributor Author

Perf says it's a 0.3% to 0.9% improvement, without any regressions. That's actually better than I expected :-)

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 1, 2021
and optimize the iterator returned by `tokenize().

This improves lexer performance by 35%
@apiraino apiraino added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Dec 1, 2021
@petrochenkov petrochenkov added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 2, 2021
@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Collaborator

bors commented Dec 2, 2021

📌 Commit 1f147a2 has been approved by petrochenkov

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 2, 2021

/// Eats symbols while predicate returns true or until the end of file is reached.
pub(crate) fn eat_while(&mut self, mut predicate: impl FnMut(char) -> bool) {
// It was tried making optimized version of this for eg. line comments, but
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// It was tried making optimized version of this for eg. line comments, but
// There was an attempt to make an optimized version of this for e.g. line comments, but

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would have worked to.
But sorry, I saw this to late.

@bors
Copy link
Collaborator

bors commented Dec 3, 2021

⌛ Testing commit 1f147a2 with merge 2a9e083...

@bors
Copy link
Collaborator

bors commented Dec 3, 2021

☀️ Test successful - checks-actions
Approved by: petrochenkov
Pushing 2a9e083 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 3, 2021
@bors bors merged commit 2a9e083 into rust-lang:master Dec 3, 2021
@rustbot rustbot added this to the 1.59.0 milestone Dec 3, 2021
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (2a9e083): comparison url.

Summary: This change led to moderate relevant improvements 🎉 in compiler performance.

  • Moderate improvement in instruction counts (up to -1.0% on incr-unchanged builds of coercions)

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

@rustbot label: -perf-regression

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.