Skip to content

Simplify LazyAttrTokenStream #127516

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 3 commits into from
Apr 30, 2025

Conversation

nnethercote
Copy link
Contributor

@nnethercote nnethercote commented Jul 9, 2024

LazyAttrTokenStream is an unpleasant type: Lrc<Box<dyn ToAttrTokenStream>>. Why does it look like that?

  • There are two ToAttrTokenStream impls, one for the lazy case, and one for the case where we already have an AttrTokenStream.
  • The lazy case (LazyAttrTokenStreamImpl) is implemented in rustc_parse, but LazyAttrTokenStream is defined in rustc_ast, which does not depend on rustc_parse. The use of the trait lets rustc_ast implicitly depend on rustc_parse. This explains the dyn.
  • LazyAttrTokenStream must have a size_of as small as possible, because it's used in many AST nodes. This explains the Lrc<Box<_>>, which keeps it to one word. (It's required Lrc<dyn _> would be a fat pointer.)

This PR moves LazyAttrTokenStreamImpl (and a few other token stream things) from rustc_parse to rustc_ast. This lets us replace the ToAttrTokenStream trait with a two-variant enum and also remove the Box, changing LazyAttrTokenStream to Lrc<LazyAttrTokenStreamInner>. Plus it does a few cleanups.

r? @petrochenkov

@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. labels Jul 9, 2024
@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 Jul 9, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 9, 2024
…ream, r=<try>

Simplify `LazyAttrTokenStream`

`LazyAttrTokenStream` is an unpleasant type: `Lrc<Box<dyn ToAttrTokenStream>>`. Why does it look like that?
- There are two `ToAttrTokenStream` impls, one for the lazy case, and one for the case where we already have an `AttrTokenStream`.
- The lazy case (`LazyAttrTokenStreamImpl`) is implemented in `rustc_parse`, but `LazyAttrTokenStream` is defined in `rustc_ast`, which does not depend on `rustc_parse`. The use of the trait lets `rustc_ast` implicitly depend on `rustc_parse`. This explains the `dyn`.
- `LazyAttrTokenStream` must have a `size_of` as small as possible, because it's used in many AST nodes. This explains the `Lrc<Box<_>>`, which keeps it to one word. (It's required `Lrc<dyn _>` would be a fat pointer.)

This PR moves `LazyAttrTokenStreamImpl` (and a few other token stream things) from `rustc_parse` to `rustc_ast`. This lets us replace the `ToAttrTokenStream` trait with a two-variant enum and also remove the `Box`, changing `LazyAttrTokenStream` to `Lrc<LazyAttrTokenStreamInner>`.

r? `@petrochenkov`
@bors
Copy link
Collaborator

bors commented Jul 9, 2024

⌛ Trying commit ad11b0b with merge f05273a...

@nnethercote
Copy link
Contributor Author

I tried a simpler approach in #127478: just removing LazyAttrTokenStream altogether, in favour of AttrTokenStream. The code ends up nicer but there is just enough of a performance regression that I'm not comfortable with it.

@rust-log-analyzer

This comment has been minimized.

@nnethercote nnethercote force-pushed the simplify-LazyAttrTokenStream branch from ad11b0b to 54c1d40 Compare July 9, 2024 12:24
@nnethercote
Copy link
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jul 9, 2024

⌛ Trying commit 54c1d40 with merge c0bb39c...

bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 9, 2024
…ream, r=<try>

Simplify `LazyAttrTokenStream`

`LazyAttrTokenStream` is an unpleasant type: `Lrc<Box<dyn ToAttrTokenStream>>`. Why does it look like that?
- There are two `ToAttrTokenStream` impls, one for the lazy case, and one for the case where we already have an `AttrTokenStream`.
- The lazy case (`LazyAttrTokenStreamImpl`) is implemented in `rustc_parse`, but `LazyAttrTokenStream` is defined in `rustc_ast`, which does not depend on `rustc_parse`. The use of the trait lets `rustc_ast` implicitly depend on `rustc_parse`. This explains the `dyn`.
- `LazyAttrTokenStream` must have a `size_of` as small as possible, because it's used in many AST nodes. This explains the `Lrc<Box<_>>`, which keeps it to one word. (It's required `Lrc<dyn _>` would be a fat pointer.)

This PR moves `LazyAttrTokenStreamImpl` (and a few other token stream things) from `rustc_parse` to `rustc_ast`. This lets us replace the `ToAttrTokenStream` trait with a two-variant enum and also remove the `Box`, changing `LazyAttrTokenStream` to `Lrc<LazyAttrTokenStreamInner>`. Plus it does a few cleanups.

r? `@petrochenkov`
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jul 9, 2024

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

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (c0bb39c): comparison URL.

Overall result: ❌✅ regressions and improvements - no action needed

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.

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

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.5% [2.5%, 2.5%] 1
Improvements ✅
(primary)
-0.2% [-0.2%, -0.2%] 1
Improvements ✅
(secondary)
-2.2% [-2.8%, -1.3%] 4
All ❌✅ (primary) -0.2% [-0.2%, -0.2%] 1

Max RSS (memory usage)

Results (primary 1.4%, secondary 0.1%)

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)
3.5% [3.5%, 3.5%] 1
Regressions ❌
(secondary)
3.4% [2.6%, 4.2%] 2
Improvements ✅
(primary)
-0.6% [-0.6%, -0.6%] 1
Improvements ✅
(secondary)
-3.2% [-3.3%, -3.1%] 2
All ❌✅ (primary) 1.4% [-0.6%, 3.5%] 2

Cycles

Results (primary 3.7%)

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)
3.7% [3.7%, 3.7%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 3.7% [3.7%, 3.7%] 1

Binary size

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

Bootstrap: 703.117s -> 703.335s (0.03%)
Artifact size: 328.74 MiB -> 328.86 MiB (0.04%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 10, 2024
@nnethercote nnethercote marked this pull request as draft July 10, 2024 06:06
@nnethercote
Copy link
Contributor Author

I'm not sure yet if I want this to be merged. I've marked this as a draft for now.

@petrochenkov
Copy link
Contributor

I'm not sure yet if I want this to be merged. I've marked this as a draft for now.

What are you concerns?

Some parts of the moved code definitely feel like a parser logic - the last token breaking, or the flat token cursor (to a least extent), but there's not too much of it.
The resulting "dyn trait" -> "enum" conversion looks like an improvement to me.

Blocked on #127558.
@rustbot blocked

@rustbot rustbot added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 13, 2024
@nnethercote
Copy link
Contributor Author

What are your concerns?

Some parts of the moved code definitely feel like a parser logic - the last token breaking, or the flat token cursor (to a least extent), but there's not too much of it.

Those are the concerns, pretty much.

@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-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Jul 16, 2024
@nnethercote nnethercote force-pushed the simplify-LazyAttrTokenStream branch from 54c1d40 to c71202b Compare September 11, 2024 04:20
@nnethercote nnethercote marked this pull request as ready for review September 11, 2024 04:20
@petrochenkov petrochenkov removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 29, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 29, 2025
…ream, r=<try>

Simplify `LazyAttrTokenStream`

`LazyAttrTokenStream` is an unpleasant type: `Lrc<Box<dyn ToAttrTokenStream>>`. Why does it look like that?
- There are two `ToAttrTokenStream` impls, one for the lazy case, and one for the case where we already have an `AttrTokenStream`.
- The lazy case (`LazyAttrTokenStreamImpl`) is implemented in `rustc_parse`, but `LazyAttrTokenStream` is defined in `rustc_ast`, which does not depend on `rustc_parse`. The use of the trait lets `rustc_ast` implicitly depend on `rustc_parse`. This explains the `dyn`.
- `LazyAttrTokenStream` must have a `size_of` as small as possible, because it's used in many AST nodes. This explains the `Lrc<Box<_>>`, which keeps it to one word. (It's required `Lrc<dyn _>` would be a fat pointer.)

This PR moves `LazyAttrTokenStreamImpl` (and a few other token stream things) from `rustc_parse` to `rustc_ast`. This lets us replace the `ToAttrTokenStream` trait with a two-variant enum and also remove the `Box`, changing `LazyAttrTokenStream` to `Lrc<LazyAttrTokenStreamInner>`. Plus it does a few cleanups.

r? `@petrochenkov`
@bors
Copy link
Collaborator

bors commented Apr 29, 2025

⌛ Trying commit 0f03af0 with merge f0cba53...

@bors
Copy link
Collaborator

bors commented Apr 29, 2025

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

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f0cba53): 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)
3.0% [3.0%, 3.0%] 1
Regressions ❌
(secondary)
0.5% [0.3%, 0.7%] 9
Improvements ✅
(primary)
-0.3% [-0.3%, -0.2%] 3
Improvements ✅
(secondary)
-2.1% [-4.0%, -1.4%] 16
All ❌✅ (primary) 0.5% [-0.3%, 3.0%] 4

Max RSS (memory usage)

Results (primary 0.2%, secondary -0.6%)

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)
0.7% [0.5%, 0.8%] 5
Regressions ❌
(secondary)
3.8% [2.0%, 5.5%] 6
Improvements ✅
(primary)
-0.5% [-0.5%, -0.4%] 3
Improvements ✅
(secondary)
-3.9% [-5.9%, -2.2%] 8
All ❌✅ (primary) 0.2% [-0.5%, 0.8%] 8

Cycles

Results (primary 0.8%, secondary -2.7%)

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)
1.1% [0.4%, 3.0%] 5
Regressions ❌
(secondary)
2.2% [2.2%, 2.2%] 1
Improvements ✅
(primary)
-0.4% [-0.4%, -0.4%] 1
Improvements ✅
(secondary)
-3.5% [-4.5%, -2.5%] 6
All ❌✅ (primary) 0.8% [-0.4%, 3.0%] 6

Binary size

Results (primary 1.1%)

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)
1.1% [1.1%, 1.1%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.1% [1.1%, 1.1%] 1

Bootstrap: 764.09s -> 765.668s (0.21%)
Artifact size: 365.39 MiB -> 365.21 MiB (-0.05%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Apr 29, 2025
@petrochenkov
Copy link
Contributor

r=me with #127516 (comment) addressed.
@rustbot author

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 29, 2025
@rustbot
Copy link
Collaborator

rustbot commented Apr 29, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

This commit does the following.
- Changes it from `Lrc<Box<dyn ToAttrTokenStream>>` to
  `Lrc<LazyAttrTokenStreamInner>`.
- Reworks `LazyAttrTokenStreamImpl` as `LazyAttrTokenStreamInner`, which
  is a two-variant enum.
- Removes the `ToAttrTokenStream` trait and the two impls of it.

The recursion limit must be increased in some crates otherwise rustdoc
aborts.
@nnethercote nnethercote force-pushed the simplify-LazyAttrTokenStream branch from 0f03af0 to 880e6f7 Compare April 29, 2025 22:19
@nnethercote
Copy link
Contributor Author

I removed LazyAttrTokenStreamImpl.

@bors r=petrochenkov

@bors
Copy link
Collaborator

bors commented Apr 29, 2025

📌 Commit 880e6f7 has been approved by petrochenkov

It is now in the queue for this repository.

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 29, 2025
@bors
Copy link
Collaborator

bors commented Apr 30, 2025

⌛ Testing commit 880e6f7 with merge f242d6c...

@bors
Copy link
Collaborator

bors commented Apr 30, 2025

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

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 30, 2025
@bors bors merged commit f242d6c into rust-lang:master Apr 30, 2025
7 checks passed
@rustbot rustbot added this to the 1.88.0 milestone Apr 30, 2025
Copy link

What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing 0fbb922 (parent) -> f242d6c (this PR)

Test differences

Show 8 test diffs

8 doctest diffs were found. These are ignored, as they are noisy.

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard f242d6c26cc6fc187257bd1be9590b4b39632425 --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. dist-aarch64-linux: 7748.2s -> 5519.1s (-28.8%)
  2. x86_64-apple-1: 8448.5s -> 6471.1s (-23.4%)
  3. dist-apple-various: 7265.2s -> 6199.5s (-14.7%)
  4. dist-arm-linux: 4550.9s -> 5122.3s (12.6%)
  5. x86_64-gnu-distcheck: 4380.6s -> 4766.3s (8.8%)
  6. dist-aarch64-apple: 5159.2s -> 5500.5s (6.6%)
  7. x86_64-gnu-llvm-19-1: 5657.9s -> 5313.2s (-6.1%)
  8. x86_64-apple-2: 5064.3s -> 4779.7s (-5.6%)
  9. x86_64-msvc-ext1: 7469.0s -> 7116.3s (-4.7%)
  10. x86_64-gnu-aux: 5915.3s -> 6161.3s (4.2%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@nnethercote nnethercote deleted the simplify-LazyAttrTokenStream branch April 30, 2025 03:23
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f242d6c): comparison URL.

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

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

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.6% [0.3%, 0.8%] 7
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.1% [-4.0%, -1.4%] 16
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary -0.3%, secondary -1.4%)

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)
0.7% [0.4%, 0.9%] 4
Regressions ❌
(secondary)
3.3% [2.5%, 4.5%] 4
Improvements ✅
(primary)
-1.1% [-2.7%, -0.4%] 5
Improvements ✅
(secondary)
-4.5% [-9.4%, -2.2%] 6
All ❌✅ (primary) -0.3% [-2.7%, 0.9%] 9

Cycles

Results (primary -0.2%, secondary 2.3%)

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)
0.5% [0.4%, 0.5%] 3
Regressions ❌
(secondary)
2.3% [2.3%, 2.3%] 1
Improvements ✅
(primary)
-0.9% [-1.2%, -0.4%] 3
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.2% [-1.2%, 0.5%] 6

Binary size

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

Bootstrap: 769.061s -> 770.064s (0.13%)
Artifact size: 365.55 MiB -> 365.60 MiB (0.01%)

@nnethercote
Copy link
Contributor Author

Improvements outweigh regressions, and all the movement is in secondary benchmarks.

@rustbot label: +perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. 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.

7 participants