Skip to content

Optimize display column calculation - #164729

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

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

Conversation

@ogoffart

Copy link
Copy Markdown
Contributor

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

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

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants