Skip to content
This repository was archived by the owner on Nov 9, 2025. It is now read-only.

Optimise StrExt::replace_smolstr, StrExt::replacen_smolstr for single ascii replace#100

Merged
alexheretic merged 4 commits intorust-analyzer:masterfrom
alexheretic:faster-1-ascii-replace
Oct 22, 2025
Merged

Optimise StrExt::replace_smolstr, StrExt::replacen_smolstr for single ascii replace#100
alexheretic merged 4 commits intorust-analyzer:masterfrom
alexheretic:faster-1-ascii-replace

Conversation

@alexheretic
Copy link
Copy Markdown
Contributor

@alexheretic alexheretic commented Sep 15, 2025

This PR ports a single ascii fast path optimisation from std str::replace highlighted by benchmarks in #96. I also made it a little more general applying to both StrExt::replace_smolstr & StrExt::replacen_smolstr.

PR bench

group                       faster-1-ascii-replace    master
-----                       ----------------------    ------
replace_smolstr len=12      1.00      6.8±0.03ns      3.51     23.7±0.27ns
replace_smolstr len=50      1.00     29.3±0.05ns      2.84     83.1±0.04ns
replace_smolstr len=1000    1.00    200.2±0.01ns      3.17    635.1±5.58ns

The change provides a significant perf improvement particularly for inline reprs. Also faster than String for len=12 now 🙂 (I measured previously as 10.8ns).

Copy link
Copy Markdown
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

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

Thanks!

@alexheretic alexheretic force-pushed the faster-1-ascii-replace branch from 79daa40 to c30409b Compare October 22, 2025 13:28
@alexheretic alexheretic merged commit 8f4deab into rust-analyzer:master Oct 22, 2025
1 check passed
@alexheretic alexheretic deleted the faster-1-ascii-replace branch October 22, 2025 13:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants