`[T]::partial_sort_unstable()` performance on short prefixes
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
One of the most common use cases for the new nightly [T]::partial_sort_unstable() (Tracking issue: #149046) is to sort the start of the slice, ..n.
I extensively benchmarked this vs. the heap-sort based partial_sort crate (v1.0.0).
Generally, it looks like std significantly outperforms the heap-based implementation, except on very short prefixes, where it is significantly slower. This goes more than 4x in either direction.
Is it clear why that happens? Can it be fixed without impacting overall performance?
Setup
Similar to https://github.com/Voultapher/sort-research-rs.
Data types:
u64string: 10-byteStringrepresentation created from theu64kibibyte: 1 KiB on the stack, where the first 8 bytes are theu64
Scenarios:
random: Uniform over full rangerandom_z1: Zipf with exponent 1random_d20: Uniform over0..20random_p5: 5% of elements uniform over full range, 95% fixed middle valuerandom_p95: 95% of elements uniform over full range, 5% fixed middle valueascending: 0, 1, 2, 3, ...descending
Results on Zen 5: Speedup of partial_sort 1.0.0 over nightly-2026-01-15
Green: std is faster
Dashed lines at prefix sizes 1, 2, 4, 8, 16, 32, 64.
Code and raw data: https://github.com/niklasf/partial_sort_vendored_std
cc @sundy-li @tisonkun
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the benchmark code and raw data in the linked partial_sort_vendored_std repository, then inspect the nightly std implementation of [T]::partial_sort_unstable() for the ..n case. Reproduce the short-prefix comparisons across the listed data types and distributions. Done means identifying the cause and demonstrating a fix that improves short prefixes without reducing overall performance.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100