Skip to content
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

Overhaul token cursors #134161

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

nnethercote
Copy link
Contributor

r? @ghost

As it happens, lookahead values of 0, 1, and 2 all work fine here, due
to the structure of the code. (Values or 3 or greater cause test
failures.) This commit changes the lookahead to zero because that will
facilitate cleanups in subsequent commits.
It's only ever used with a lookahead of 0, so this commit removes the
lookahead and renames it `peek`.
Because `TokenStreamIter` is a much better name for a `TokenStream`
iterator. Also rename the `TokenStream::trees` method as
`TokenStream::iter`, and some local variables.
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Dec 11, 2024
Currently there are two ways to peek at a `TokenStreamIter`.
- Wrap it in a `Peekable` and use that traits `peek` method.
- Use `TokenStreamIter`'s inherent `peek` method.

Some code uses one, some use the other. This commit converts all places
to the inherent method. This eliminates mixing of `TokenStreamIter` and
`Peekable<TokenStreamIter>` and some use of `impl Iterator` and `dyn
Iterator`.
@nnethercote nnethercote force-pushed the overhaul-token-cursors branch from efad936 to 7874297 Compare December 11, 2024 08:50
@nnethercote
Copy link
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 11, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 11, 2024
…r=<try>

Overhaul token cursors

r? `@ghost`
@bors
Copy link
Contributor

bors commented Dec 11, 2024

⌛ Trying commit 7874297 with merge 29ae3e8...

@bors
Copy link
Contributor

bors commented Dec 11, 2024

☀️ Try build successful - checks-actions
Build commit: 29ae3e8 (29ae3e83e7d9a22117bca546c540f18c2cb36a8b)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (29ae3e8): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

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 may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

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

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.4% [0.3%, 0.4%] 3
Improvements ✅
(primary)
-0.2% [-0.2%, -0.1%] 2
Improvements ✅
(secondary)
-0.3% [-0.5%, -0.2%] 4
All ❌✅ (primary) -0.2% [-0.2%, -0.1%] 2

Max RSS (memory usage)

Results (primary 1.3%, secondary -0.2%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
2.0% [1.5%, 2.6%] 4
Regressions ❌
(secondary)
1.8% [1.6%, 2.3%] 4
Improvements ✅
(primary)
-1.1% [-1.1%, -1.1%] 1
Improvements ✅
(secondary)
-4.3% [-5.1%, -3.4%] 2
All ❌✅ (primary) 1.3% [-1.1%, 2.6%] 5

Cycles

Results (primary 6.0%, secondary -0.5%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
6.0% [3.3%, 9.8%] 3
Regressions ❌
(secondary)
9.4% [9.4%, 9.4%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.0% [-3.5%, -2.5%] 4
All ❌✅ (primary) 6.0% [3.3%, 9.8%] 3

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 767.862s -> 766.941s (-0.12%)
Artifact size: 331.04 MiB -> 331.06 MiB (0.01%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Dec 11, 2024
@petrochenkov petrochenkov self-assigned this Dec 11, 2024
@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 11, 2024
@nnethercote
Copy link
Contributor Author

I saw small but consistent icount improvements locally:
Screenshot from 2024-12-12 07-21-37

The most likely explanation when local wins aren't seen on CI is that PGO is involved. But not every platform has PGO applied, and the improvements here are also to readability, so I think this is still worth going forward.

- Move it to `rustc_parse`, which is the only crate that uses it. This
  lets us remove all the `pub` markers from it.

- Change `next_ref` and `look_ahead` to `get` and `bump`, which work
  better for the `rustc_parse` uses.

- This requires adding a `TokenStream::get` method, which is simple.

- In `TokenCursor`, we currently duplicate the
  `DelimSpan`/`DelimSpacing`/`Delimiter` from the surrounding
  `TokenTree::Delimited` in the stack. This isn't necessary so long as
  we don't prematurely move past the `Delimited`, and is a small perf
  win on a very hot code path.

- In `parse_token_tree`, we can just clone the relevant
  `TokenTree::Delimited` instead of constructing an identical one from
  pieces.
@nnethercote nnethercote force-pushed the overhaul-token-cursors branch from 7874297 to 999cb16 Compare December 11, 2024 20:40
@nnethercote nnethercote marked this pull request as ready for review December 11, 2024 20:40
@rustbot
Copy link
Collaborator

rustbot commented Dec 11, 2024

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

@nnethercote
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot 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 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf-regression Performance regression. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants