Skip to content

[perf] Use a flat token stream representation when lexing - #159378

Draft
Kobzol wants to merge 8 commits into
rust-lang:mainfrom
Kobzol:lexer-flat
Draft

[perf] Use a flat token stream representation when lexing#159378
Kobzol wants to merge 8 commits into
rust-lang:mainfrom
Kobzol:lexer-flat

Conversation

@Kobzol

@Kobzol Kobzol commented Jul 16, 2026

Copy link
Copy Markdown
Member

View all comments

Opening for a perf experiment, nothing to see here.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 16, 2026
@Kobzol

Kobzol commented Jul 16, 2026

Copy link
Copy Markdown
Member 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 16, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jul 16, 2026
[perf] Use a flat token stream representation when lexing
@rust-bors

rust-bors Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 8374c7c (8374c7ca6b40db24e90f1c62568ddd867a1d48ce)
Base parent: ab4bbeb (ab4bbeb07dc7dc2d30e9f83d3e0326df981e0ee2)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (8374c7c): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

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

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.4% [0.1%, 1.2%] 73
Regressions ❌
(secondary)
0.6% [0.1%, 1.4%] 59
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.8% [-2.4%, -1.3%] 13
All ❌✅ (primary) 0.4% [0.1%, 1.2%] 73

Max RSS (memory usage)

Results (secondary -6.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.9% [1.8%, 8.5%] 14
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-13.6% [-22.8%, -2.3%] 21
All ❌✅ (primary) - - 0

Cycles

Results (primary 1.1%, secondary -7.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.6% [2.4%, 3.0%] 3
Regressions ❌
(secondary)
3.5% [2.1%, 5.2%] 8
Improvements ✅
(primary)
-3.4% [-3.4%, -3.4%] 1
Improvements ✅
(secondary)
-13.6% [-19.9%, -4.9%] 14
All ❌✅ (primary) 1.1% [-3.4%, 3.0%] 4

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 489.393s -> 489.02s (-0.08%)
Artifact size: 389.94 MiB -> 389.88 MiB (-0.02%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jul 16, 2026
@Kobzol

Kobzol commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

Seems like doing the flat representation in the lexer doesn't help much when we still have to convert it back to all the allocations required for the parser. The whole thing would have to be flattened to have the desired perf. effect.

To avoid unnecessary allocations during lexing.
@rustbot rustbot added the A-attributes Area: Attributes (`#[…]`, `#![…]`) label Aug 25, 2026
@Kobzol

Kobzol commented Aug 25, 2026

Copy link
Copy Markdown
Member 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 Aug 25, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 25, 2026
[perf] Use a flat token stream representation when lexing
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

💔 Test for 5588d8a failed: CI. Failed job:

@rust-log-analyzer

This comment has been minimized.

@Kobzol

Kobzol commented Aug 25, 2026

Copy link
Copy Markdown
Member Author

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 25, 2026
[perf] Use a flat token stream representation when lexing
@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 26, 2026
[perf] Use a flat token stream representation when lexing
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

💔 Test for 8bcb09e failed: CI. Failed job:

@rust-log-analyzer

This comment has been minimized.

@Kobzol

Kobzol commented Aug 26, 2026

Copy link
Copy Markdown
Member Author

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 26, 2026
[perf] Use a flat token stream representation when lexing
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 32559b2 (32559b2e346e755d7cfe72e6d57ace068ce16a1a)
Base parent: e081d66 (e081d6616f3be5a96c6b42eecc6410f1a5378b7e)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (32559b2): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

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

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
1.8% [0.1%, 11.2%] 127
Regressions ❌
(secondary)
4.1% [0.1%, 38.0%] 99
Improvements ✅
(primary)
-0.6% [-2.5%, -0.2%] 60
Improvements ✅
(secondary)
-1.4% [-5.9%, -0.1%] 55
All ❌✅ (primary) 1.0% [-2.5%, 11.2%] 187

Max RSS (memory usage)

Results (primary -2.2%, secondary -0.5%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.7% [1.7%, 1.7%] 1
Regressions ❌
(secondary)
4.8% [0.8%, 16.6%] 18
Improvements ✅
(primary)
-2.3% [-4.3%, -0.8%] 88
Improvements ✅
(secondary)
-3.4% [-10.2%, -1.4%] 33
All ❌✅ (primary) -2.2% [-4.3%, 1.7%] 89

Cycles

Results (primary 6.0%, secondary 16.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
6.0% [1.9%, 14.0%] 33
Regressions ❌
(secondary)
20.4% [1.8%, 85.8%] 40
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.4% [-5.8%, -2.1%] 7
All ❌✅ (primary) 6.0% [1.9%, 14.0%] 33

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 473.971s -> 476.786s (0.59%)
Artifact size: 402.61 MiB -> 402.69 MiB (0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 26, 2026
@Kobzol

Kobzol commented Aug 29, 2026

Copy link
Copy Markdown
Member 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 Aug 29, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 29, 2026
[perf] Use a flat token stream representation when lexing
@rust-bors

rust-bors Bot commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 41bfd27 (41bfd27447b6da2214d1c73b14037a50d34b84c3)
Base parent: fd7ed57 (fd7ed57dfd3bdebb745a1d8158638727b0e7047a)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (41bfd27): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

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

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.6% [0.1%, 1.6%] 45
Regressions ❌
(secondary)
1.1% [0.1%, 3.3%] 41
Improvements ✅
(primary)
-0.3% [-0.4%, -0.1%] 8
Improvements ✅
(secondary)
-2.4% [-4.7%, -0.1%] 22
All ❌✅ (primary) 0.5% [-0.4%, 1.6%] 53

Max RSS (memory usage)

Results (secondary 0.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
5.7% [2.9%, 10.0%] 8
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-5.0% [-10.1%, -1.5%] 8
All ❌✅ (primary) - - 0

Cycles

Results (primary 2.3%, secondary 2.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.3% [1.3%, 2.6%] 9
Regressions ❌
(secondary)
2.4% [1.9%, 3.0%] 7
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.3% [1.3%, 2.6%] 9

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 476.157s -> 473.87s (-0.48%)
Artifact size: 402.84 MiB -> 402.90 MiB (0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 29, 2026
@rust-bors

This comment has been minimized.

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job test-x86_64-gnu-llvm-21 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
   Compiling tinyvec_macros v0.1.1
warning: unused import: `TokenArena`
  --> compiler/rustc_ast/src/tokenstream.rs:24:53
   |
24 |     ArenaTokenTree, DelimitedBounds, DelimitedData, TokenArena, TokenArenaView,
   |                                                     ^^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` (part of `#[warn(unused)]`) on by default

warning: unreachable call
   --> compiler/rustc_ast/src/attr/mod.rs:371:17
    |
371 |                 MetaItemKind::list_from_tokens(todo!()) //args.tokens.clone())
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unreachable call
    |
   --> /rustc/cbae9b4cae2b108f6a3d18cfe6075714bb739463/library/core/src/macros/mod.rs:886:8
    |
    = note: any code following this expression is unreachable
    |
    = note: `#[warn(unreachable_code)]` (part of `#[warn(unused)]`) on by default

error[E0308]: mismatched types
  --> compiler/rustc_ast/src/tokenarena.rs:71:31
   |
71 |     TokenArenaStream { arena, trees }
   |                               ^^^^^ expected `Arc<Vec<ArenaTokenTree>>`, found `Vec<ArenaTokenTree>`
   |
   = note: expected struct `Arc<Vec<_>>`
              found struct `Vec<_>`
help: call `Into::into` on this expression to convert `Vec<ArenaTokenTree>` into `Arc<Vec<ArenaTokenTree>>`
   |
71 |     TokenArenaStream { arena, trees: trees.into() }
   |                               ++++++      +++++++

error[E0599]: the method `stable_hash` exists for reference `&Arc<TokenArena>`, but its trait bounds were not satisfied
   --> compiler/rustc_ast/src/tokenarena.rs:216:46
    |
 77 |   pub struct TokenArena {
    |   --------------------- doesn't satisfy `TokenArena: StableHash`
...
216 |   #[derive(Clone, Encodable, Decodable, Debug, StableHash)]
    |                                                -^^^^^^^^^
    |                                                |
    |                                                method cannot be called on `&Arc<TokenArena>` due to unsatisfied trait bounds
    |                                                in this derive macro expansion
    |
   ::: /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/synstructure-0.13.2/src/macros.rs:95:9
    |
 95 | /         pub fn $derives(
 96 | |             i: $crate::macros::TokenStream
 97 | |         ) -> $crate::macros::TokenStream {
    | |________________________________________- in this expansion of `#[derive(StableHash)]`
    |
    = note: the following trait bounds were not satisfied:
            `TokenArena: StableHash`
            which is required by `Arc<TokenArena>: StableHash`
            `Arc<TokenArena>: StableHash`
            which is required by `&Arc<TokenArena>: StableHash`
note: the trait `StableHash` must be implemented
   --> compiler/rustc_data_structures/src/stable_hash.rs:74:1
    |
 74 | pub trait StableHash {
    | ^^^^^^^^^^^^^^^^^^^^

error[E0599]: no method named `get_innermost_elem_at` found for enum `TokenArenaView` in the current scope
   --> compiler/rustc_ast/src/tokenstream.rs:934:35
    |
934 |             let elem = self.arena.get_innermost_elem_at(index);
    |                                   ^^^^^^^^^^^^^^^^^^^^^ method not found in `TokenArenaView`
    |
   ::: compiler/rustc_ast/src/tokenarena.rs:217:1
    |
217 | pub enum TokenArenaView {
    | ----------------------- method `get_innermost_elem_at` not found for this enum
    |
note: the method `get_innermost_elem_at` exists on the type `Arc<TokenArena>`
   --> compiler/rustc_ast/src/tokenarena.rs:167:5
    |
167 |     pub fn get_innermost_elem_at(&self, index: usize) -> Option<&ArenaTokenTree> {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0599]: no method named `get_innermost_elem_at` found for enum `TokenArenaView` in the current scope
   --> compiler/rustc_ast/src/tokenstream.rs:953:26
    |
953 |         match self.arena.get_innermost_elem_at(index) {
    |                          ^^^^^^^^^^^^^^^^^^^^^ method not found in `TokenArenaView`
    |
   ::: compiler/rustc_ast/src/tokenarena.rs:217:1
    |
217 | pub enum TokenArenaView {
    | ----------------------- method `get_innermost_elem_at` not found for this enum
    |
note: the method `get_innermost_elem_at` exists on the type `Arc<TokenArena>`
   --> compiler/rustc_ast/src/tokenarena.rs:167:5
    |
167 |     pub fn get_innermost_elem_at(&self, index: usize) -> Option<&ArenaTokenTree> {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0599]: no method named `get_innermost_elem_at` found for enum `TokenArenaView` in the current scope
   --> compiler/rustc_ast/src/tokenstream.rs:964:20
    |
964 |         self.arena.get_innermost_elem_at(bounds.index_of_next_token_tree())
    |                    ^^^^^^^^^^^^^^^^^^^^^ method not found in `TokenArenaView`
    |
   ::: compiler/rustc_ast/src/tokenarena.rs:217:1
    |
217 | pub enum TokenArenaView {
    | ----------------------- method `get_innermost_elem_at` not found for this enum
    |
note: the method `get_innermost_elem_at` exists on the type `Arc<TokenArena>`
   --> compiler/rustc_ast/src/tokenarena.rs:167:5
    |
167 |     pub fn get_innermost_elem_at(&self, index: usize) -> Option<&ArenaTokenTree> {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0599]: no method named `length` found for enum `TokenArenaView` in the current scope
   --> compiler/rustc_ast/src/tokenstream.rs:981:37
    |
981 |             self.index = self.arena.length();
    |                                     ^^^^^^ method not found in `TokenArenaView`
    |
   ::: compiler/rustc_ast/src/tokenarena.rs:217:1
    |
217 | pub enum TokenArenaView {
    | ----------------------- method `length` not found for this enum
    |
note: the method `length` exists on the type `Arc<TokenArena>`
   --> compiler/rustc_ast/src/tokenarena.rs:171:5
    |
171 |     pub fn length(&self) -> usize {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0599]: no method named `get_innermost_elem_at` found for enum `TokenArenaView` in the current scope
    --> compiler/rustc_ast/src/tokenstream.rs:1008:44
     |
1008 |             if let Some(tree) = self.arena.get_innermost_elem_at(self.index) {
     |                                            ^^^^^^^^^^^^^^^^^^^^^ method not found in `TokenArenaView`
     |
    ::: compiler/rustc_ast/src/tokenarena.rs:217:1
     |
 217 | pub enum TokenArenaView {
     | ----------------------- method `get_innermost_elem_at` not found for this enum
     |
note: the method `get_innermost_elem_at` exists on the type `Arc<TokenArena>`
    --> compiler/rustc_ast/src/tokenarena.rs:167:5
     |
 167 |     pub fn get_innermost_elem_at(&self, index: usize) -> Option<&ArenaTokenTree> {
     |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: unused variable: `args`
    --> compiler/rustc_ast/src/ast.rs:2089:33
     |
2089 |             AttrArgs::Delimited(args) => todo!(), //args.tokens.clone(),
     |                                 ^^^^ help: if this is intentional, prefix it with an underscore: `_args`
     |
     = note: `#[warn(unused_variables)]` (part of `#[warn(unused)]`) on by default

Some errors have detailed explanations: E0308, E0599.
For more information about an error, try `rustc --explain E0308`.

@rust-bors

rust-bors Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #162269) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) perf-regression Performance regression. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

4 participants