Commit 06293ff
committed
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 #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 8317fef
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>3 files changed
Lines changed: 77 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1496 | 1496 | | |
1497 | 1497 | | |
1498 | 1498 | | |
1499 | | - | |
| 1499 | + | |
| 1500 | + | |
| 1501 | + | |
| 1502 | + | |
| 1503 | + | |
| 1504 | + | |
| 1505 | + | |
1500 | 1506 | | |
1501 | 1507 | | |
1502 | 1508 | | |
| |||
1508 | 1514 | | |
1509 | 1515 | | |
1510 | 1516 | | |
1511 | | - | |
| 1517 | + | |
| 1518 | + | |
| 1519 | + | |
| 1520 | + | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
1512 | 1524 | | |
1513 | 1525 | | |
1514 | 1526 | | |
| |||
1583 | 1595 | | |
1584 | 1596 | | |
1585 | 1597 | | |
1586 | | - | |
| 1598 | + | |
| 1599 | + | |
| 1600 | + | |
| 1601 | + | |
| 1602 | + | |
| 1603 | + | |
| 1604 | + | |
| 1605 | + | |
1587 | 1606 | | |
1588 | 1607 | | |
1589 | 1608 | | |
| |||
1595 | 1614 | | |
1596 | 1615 | | |
1597 | 1616 | | |
1598 | | - | |
| 1617 | + | |
| 1618 | + | |
| 1619 | + | |
| 1620 | + | |
| 1621 | + | |
| 1622 | + | |
1599 | 1623 | | |
1600 | 1624 | | |
1601 | 1625 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
0 commit comments