Skip to content

Optimize display column calculation - #164729

Open
ogoffart wants to merge 3 commits into
rust-lang:mainfrom
ogoffart:olivier/col-display
Open

ogoffart wants to merge 3 commits into
rust-lang:mainfrom
ogoffart:olivier/col-display

Conversation

@ogoffart

@ogoffart ogoffart commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

We are computing the display column of a position by decoding every
char from the start of its line for the location of every possible panic,
so a file with very long lines takes quadratic time.
(This is the case for a generated file that can get very long lines)

For most code (ascii without tabs) the column is the byte index.
So optimize the fast path.

On a ~5 MB generated single line file, the build goes from 161s to 15s

LLM disclosure: an AI agent was used to diagnose the issue and first iteration, but the patch was then iterated and simplified by me.

We are computing the display column of a position by decoding every
char from the start of its line for the location of every possible panic,
so a file with very long lines takes quadratic time.
(This is the case for a generated file that can get very long lines)

For most code (ascii without tabs) the column is the byte index.
So optimize the fast path.

On a ~5 MB generated single line file, the build goes from 161s to 15s
@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 Oct 22, 2026
@rustbot

rustbot commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 77 candidates
  • Random selection from 20 candidates

}

let (lines, multibyte_chars) = analyze_source_file::analyze_source_file(&src);
let may_contain_tabs = src.contains('\t');

@ogoffart ogoffart Oct 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I considered trying to do this in analyze_source_file::analyze_source_file doing that at the same time as breaking lines and detecting non-ascii, but that is more involved and i hope contains is optimized enough already.

View changes since the review

@panstromek

Copy link
Copy Markdown
Contributor

I assume you want to measure this?

@bors try @rust-timer queue

Also note that this likely interacts with LLM policy: https://forge.rust-lang.org/policies/llm-usage.html

@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 Oct 22, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Oct 22, 2026
@rust-bors

rust-bors Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: b0e2506 (b0e2506816d5b90bd35fe35a8f05b58d1fae4d2a)
Base parent: 2b548e4 (2b548e49fcdf52bed4446b3e245563681f5bc45d)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (b0e2506): 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.2% [0.2%, 0.3%] 2
Regressions ❌
(secondary)
1.3% [0.1%, 4.6%] 24
Improvements ✅
(primary)
-0.4% [-1.0%, -0.2%] 39
Improvements ✅
(secondary)
-1.0% [-3.7%, -0.1%] 16
All ❌✅ (primary) -0.4% [-1.0%, 0.3%] 41

Max RSS (memory usage)

Results (primary 0.4%, secondary 1.7%)

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

mean range count
Regressions ❌
(primary)
1.1% [0.5%, 2.0%] 10
Regressions ❌
(secondary)
1.9% [0.4%, 4.3%] 19
Improvements ✅
(primary)
-1.2% [-1.6%, -0.5%] 4
Improvements ✅
(secondary)
-1.7% [-1.7%, -1.7%] 1
All ❌✅ (primary) 0.4% [-1.6%, 2.0%] 14

Cycles

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

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

mean range count
Regressions ❌
(primary)
0.8% [0.5%, 1.2%] 3
Regressions ❌
(secondary)
1.0% [0.7%, 1.4%] 4
Improvements ✅
(primary)
-0.9% [-1.7%, -0.4%] 11
Improvements ✅
(secondary)
-0.9% [-1.5%, -0.5%] 5
All ❌✅ (primary) -0.5% [-1.7%, 1.2%] 14

Binary size

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

Bootstrap: 487.289s -> 487.926s (0.13%)
Artifact size: 406.27 MiB -> 406.31 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 Oct 22, 2026
They don't belong here and so we skip trying to find new lines and
non-ascii char.

The refactor also mean that `include_str!` will be added after utf8
parsing. as a result, we now see the contents for the note. I believe
this is the intended behavior of `utf8_error`.
@ogoffart

Copy link
Copy Markdown
Contributor Author

The regression hapenned in include-blob which includes_bytes a large file and the contains('\t') has a big impact. It is not even needed since we never need the locations within a binary.
So i made a followup commit to not call SourceFile::new for include_bytes. The refactor also meant that include_str! will be added after utf8 parsing. as a result, we now see the contents for the note. I believe this is the intended behavior of utf8_error.

@ogoffart

Copy link
Copy Markdown
Contributor Author

Could I request another benchmark run?

@lqd

lqd commented Oct 22, 2026

Copy link
Copy Markdown
Member

@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 Oct 22, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Oct 22, 2026
@rust-bors

rust-bors Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: a0c8c8c (a0c8c8c416b57d52e4cda2b714e6437e9dd65994)
Base parent: afe686d (afe686de2708699b69dd31ed14be326f4e95edfc)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (a0c8c8c): 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.2% [0.2%, 0.2%] 1
Regressions ❌
(secondary)
0.9% [0.1%, 3.0%] 24
Improvements ✅
(primary)
-0.4% [-0.9%, -0.2%] 37
Improvements ✅
(secondary)
-1.2% [-3.7%, -0.1%] 14
All ❌✅ (primary) -0.4% [-0.9%, 0.2%] 38

Max RSS (memory usage)

Results (primary -0.1%, 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.7% [0.4%, 1.3%] 7
Regressions ❌
(secondary)
1.7% [0.6%, 3.7%] 6
Improvements ✅
(primary)
-1.5% [-2.6%, -0.8%] 4
Improvements ✅
(secondary)
-2.1% [-2.8%, -1.6%] 3
All ❌✅ (primary) -0.1% [-2.6%, 1.3%] 11

Cycles

Results (primary -0.4%, secondary -0.6%)

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

mean range count
Regressions ❌
(primary)
0.6% [0.5%, 0.8%] 4
Regressions ❌
(secondary)
0.9% [0.6%, 1.2%] 5
Improvements ✅
(primary)
-0.8% [-1.6%, -0.5%] 10
Improvements ✅
(secondary)
-2.1% [-6.5%, -0.4%] 5
All ❌✅ (primary) -0.4% [-1.6%, 0.8%] 14

Binary size

Results (primary 0.0%, secondary 0.0%)

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

mean range count
Regressions ❌
(primary)
0.0% [0.0%, 0.0%] 1
Regressions ❌
(secondary)
0.0% [0.0%, 0.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.0% [0.0%, 0.0%] 1

Bootstrap: 486.908s -> 488.286s (0.28%)
Artifact size: 406.54 MiB -> 406.61 MiB (0.02%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 22, 2026
/// Locations of multi-byte characters in the source code.
pub multibyte_chars: Vec<MultiByteChar>,
/// Whether the source may contain a tab. Always true when the source text isn't available.
may_contain_tabs: bool,

@clarfonthey clarfonthey Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is just incorrect because tabs are not the only issue, also non-ASCII characters as well. In particular, a useful test case is to add /* 悪 */ to the middle of a line, where the middle character is:

  • one char
  • two columns
  • three bytes

Instead of may_contain_tabs, this should be columns_are_always_byte_offsets, which is technically an inverse but more reasonable to understand.

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So, rereading the code, I do see that you use the multi-byte character positions, but at this point why not just rename them to multi-column chars and treat tabs as multi-column?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because non-ascii is already handled separately. And it is common (imho) that a file has either lots of tabs, or none.
But I can imagine files with a few non-ASCII in comments or strings in a couple of lines and it would be sad to pessimise this optimisation once you have a single non-ascii in your file.

@JohnTitor

Copy link
Copy Markdown
Member

r? clarfonthey in favor of prior review

@rustbot rustbot assigned clarfonthey and unassigned JohnTitor Oct 24, 2026
@clarfonthey

Copy link
Copy Markdown
Contributor

Side note: not formally on the compiler team but have been looking into this code and happy to review. Will probably pass onto someone on compiler again when I'm done with it.

@clarfonthey

Copy link
Copy Markdown
Contributor

Yeah, I don't think I'll have time to look into this.

r? compiler

@JonathanBrouwer JonathanBrouwer added the llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. label Nov 12, 2026
@JonathanBrouwer

JonathanBrouwer commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

While the perf results for rustdoc are clearly green, those on the compiler show a clear regression. So it is not entirely clear to me whether the change is worth it.

image

On a ~5 MB generated single line file, the build goes from 161s to 15s

While this is great, I don't think it's worth regressing normal benchmarks for such an outlier.

LLM disclosure: an AI agent was used to diagnose the issue and first iteration, but the patch was then iterated and simplified by me.

Thanks for honestly disclosing this! This is however in not allowed by the LLM policy. Specifically this change would require following the experiment rules which requires a pre-arranged reviewer.

@rustbot author

@rustbot rustbot 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 Nov 12, 2026
@rustbot

rustbot commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

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

@rust-bors

rust-bors Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

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

This branch has not been deployed

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

Labels

llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. 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.

8 participants