Unnamed repository; edit this file 'description' to name the repository.
Auto merge of #155607 - KowalskiThomas:kowalski/perf-use-get_unchecked-for-pattern, r=Mark-Simulacrum
perf: use `get_unchecked` for `TwoWaySearcher`
## What is this PR?
*This is related to rust-lang/rust#27721.*
This PR is a proposal for a performance improvement in `std::pattern`.
Profiling of [https://github.com/quickwit-oss/quickwit](https://github.com/quickwit-oss/quickwit) in production shows that `TwoWaySearcher::next` is one of the most CPU-time-consuming functions, so I thought I would give it a look.
I read the [contribution guide](https://std-dev-guide.rust-lang.org/development/perf-benchmarking.html) and this seems to be a fitting proposal.
It seems like `TwoWaySearcher::next` and `TwoWaySearcher::next_back` could be made faster by using `get_unchecked` in the inner loop comparisons instead of regular indexing, which is safe in the conditions where it would be done (indices are within bounds by construction).
I added some `SAFETY` comments in the code to explain why this is safe, as I believe is customary in those cases (and according to [this page as well](https://std-dev-guide.rust-lang.org/policy/safety-comments.html)).
### Benchmarks
I ran the existing bencharmks before/after the changes (only on my laptop, I can run them in other places if that's necessary).
```
./x.py bench library/coretests -- pattern::
```
We seem to be getting a ~7.5-12% performance improvement at a very low cost, which sounds worthwhile to me.
But this is the first time I'm proposing a change in Rust, so I'm looking forward to feedback on this.
```
BEFORE CHANGES
pattern::ends_with_char 3398.91ns/iter +/- 526.28
pattern::ends_with_str 3545.04ns/iter +/- 1108.76
pattern::starts_with_char 3348.31ns/iter +/- 352.38
pattern::starts_with_str 3710.59ns/iter +/- 435.57
AFTER CHANGES
pattern::ends_with_char 3125.99ns/iter +/- 567.09 (-8.03%)
pattern::ends_with_str 3106.43ns/iter +/- 258.33 (-12.38%)
pattern::starts_with_char 3094.55ns/iter +/- 595.42 (-7.59%)
pattern::starts_with_str 3365.75ns/iter +/- 268.88 (-9.29%)
```
System info for the benchmarks run
<details>
```
Based on commit 8317fef20409adedaa7c385fa6e954867bf626fc
rustc 1.96.0-dev
binary: rustc
commit-hash: unknown
commit-date: unknown
host: aarch64-apple-darwin
release: 1.96.0-dev
LLVM version: 22.1.2
Apple M4 Max
16
64 GB
ProductName: macOS
ProductVersion: 26.3
BuildVersion: 25D125
(this was run on AC and without any heavy load from other apps or whatnot)
```
</details>