Skip to content

Optimize escape_string_symbol()#159609

Open
nikic wants to merge 1 commit into
rust-lang:mainfrom
nikic:opt-escape
Open

Optimize escape_string_symbol()#159609
nikic wants to merge 1 commit into
rust-lang:mainfrom
nikic:opt-escape

Conversation

@nikic

@nikic nikic commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Going through escape_default() + to_string(), which involves going through formatting machinery and a flat_map of a complex iterator, ends up being quite slow. Directly pushing to a string, even with a naive implementation, is significantly faster.

This shows up in the include-blob benchmark, which mostly exercises this one function.

@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 20, 2026
@nikic

nikic commented Jul 20, 2026

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

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jul 20, 2026
Optimize escape_string_symbol()
@rust-bors

rust-bors Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: ab58777 (ab587773e58bf51cb3d98b08ae776394d0df33db)
Base parent: 9e71b3b (9e71b3bc704eea68d39bd0f6a46703c7d22f5d3b)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (ab58777): comparison URL.

Overall result: ✅ improvements - no action needed

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.

@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
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-21.2% [-33.6%, -6.0%] 13
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary 4.9%, secondary -2.7%)

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

mean range count
Regressions ❌
(primary)
4.9% [4.9%, 4.9%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.7% [-4.8%, -0.8%] 4
All ❌✅ (primary) 4.9% [4.9%, 4.9%] 1

Cycles

Results (secondary -5.9%)

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.6% [5.6%, 5.6%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-11.7% [-12.2%, -11.3%] 2
All ❌✅ (primary) - - 0

Binary size

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

Bootstrap: 486.168s -> 486.987s (0.17%)
Artifact size: 392.47 MiB -> 391.76 MiB (-0.18%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 20, 2026
@nikic
nikic marked this pull request as ready for review July 20, 2026 18:40
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 20, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jul 20, 2026
@rustbot

rustbot commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

r? @folkertdev

rustbot has assigned @folkertdev.
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, parser
  • compiler, parser expanded to 74 candidates
  • Random selection from 17 candidates

@nikic nikic mentioned this pull request Jul 20, 2026
9 tasks
@Kobzol

Kobzol commented Jul 20, 2026

Copy link
Copy Markdown
Member

Nice find! I wonder if .default_escape().collect() would be similar? 🤔

Edit: it doesn't, I tried.

@folkertdev

Copy link
Copy Markdown
Contributor

Neat!

@bors r+

@rust-bors

rust-bors Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

📌 Commit bfe8ccb has been approved by folkertdev

It is now in the queue for this repository.

@rust-bors rust-bors Bot 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-review Status: Awaiting review from the assignee but also interested parties. labels Jul 20, 2026
@ZuseZ4

ZuseZ4 commented Jul 21, 2026

Copy link
Copy Markdown
Member

Would it be reasonable to bump the priority a bit, given that this seems to be the last PR blocking LLVM 23? 23 gives a nice perf improvement and it would unblock some offload work? Plus, there are some autodiff CI PRs where I have a slight preference to enable them after the bump. @Kobzol

@Kobzol

Kobzol commented Jul 22, 2026

Copy link
Copy Markdown
Member

Sure, makes sense.

@bors p=1 Blocks LLVM 23 update.

@rust-bors

rust-bors Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

⌛ Testing commit bfe8ccb with merge b803f36...

Workflow: https://github.com/rust-lang/rust/actions/runs/29896556061

rust-bors Bot pushed a commit that referenced this pull request Jul 22, 2026
Optimize escape_string_symbol()

Going through escape_default() + to_string(), which involves going through formatting machinery and a flat_map of a complex iterator, ends up being quite slow. Directly pushing to a string, even with a naive implementation, is significantly faster.

This shows up in the include-blob benchmark, which mostly exercises this one function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

6 participants