Skip to content

optimize large elements for slice rotating - #162754

Open
ywxt wants to merge 1 commit into
rust-lang:mainfrom
ywxt:slice-rotate
Open

ywxt wants to merge 1 commit into
rust-lang:mainfrom
ywxt:slice-rotate

Conversation

@ywxt

@ywxt ywxt commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

This PR introduced two fixes to improve performance of slice::rotate.

Fix A:

For large elements, interrupter them as *MaybeUninit<usize>, which makes them choose the swap algorithm and obtain better cache/TLB locality.

Fix B:

When a side in swap algorithm is tiny, it might be a lot of small swaps. The fix B switches tail swap to memmove.

Benchmark Original (ns) Fix A (Δ) Fix B (Δ) Fix A+B (ns) A+B Improvement
rotate_medium_half_plus_one 6267.62 1779.07 (+71.6%) 6323.59 (-0.9%) 1621.24 +74.1%
rotate_huge_by9199_big 8218230 8109777 (+1.3%) 4281360 (+47.9%) 4244542 +48.4%
rotate_huge_half_plus_one 6268406 4799403 (+23.4%) 6221231 (+0.8%) 4758676 +24.1%
rotate_huge_by1234577_big 7950044 7846722 (+1.3%) 6111909 (+23.1%) 6102005 +23.2%
rotate_medium_by727_u64 3702.35 3606.24 (+2.6%) 3139.26 (+15.2%) 3031.55 +18.1%
rotate_medium_by727_bytes 3704.06 3605.89 (+2.7%) 3147.87 (+15.0%) 3033.77 +18.1%
rotate_medium_by727_strings 3608.34 3578.12 (+0.8%) 3035.60 (+15.9%) 3005.55 +16.7%
rotate_medium_half 1474.78 1476.64 (-0.1%) 1370.02 (+7.1%) 1354.31 +8.2%
rotate_huge_half 3179321 3150365 (+0.9%) 3077309 (+3.2%) 3038567 +4.4%
rotate_huge_by9199_bytes 4636449 4568776 (+1.5%) 4509977 (+2.7%) 4477535 +3.4%
rotate_huge_by1234577_strings 6276064 6182101 (+1.5%) 6090534 (+3.0%) 6076560 +3.2%
rotate_huge_by1234577_bytes 6266812 6218410 (+0.8%) 6236837 (+0.5%) 6097584 +2.7%
rotate_huge_by9199_strings 4727975 4698212 (+0.6%) 4676240 (+1.1%) 4610863 +2.5%
rotate_huge_by1234577_u64 6318677 6264397 (+0.9%) 6236986 (+1.3%) 6169595 +2.4%
rotate_huge_by9199_u64 4594580 4516849 (+1.7%) 4510532 (+1.8%) 4527773 +1.5%
rotate_medium_by1 1486.18 1469.57 (+1.1%) 1468.76 (+1.2%) 1467.72 +1.2%
rotate_huge_by1 3327706 3327696 (~0%) 3319754 (+0.2%) 3290266 +1.1%
rotate_tiny_by1 8.28 8.64 (-4.3%) 8.64 (-4.3%) 8.63 -4.2%
rotate_tiny_half 7.94 8.29 (-4.4%) 8.29 (-4.4%) 8.28 -4.3%
rotate_tiny_half_plus_one 9.41 9.41 (0%) 9.43 (-0.2%) 9.69 -3.0%

r? @Amanieu

@rustbot

rustbot commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

⚠️ #[rustc_allow_const_fn_unstable] needs careful audit to avoid accidentally exposing unstable
implementation details on stable.

cc @rust-lang/wg-const-eval

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants