rust-lang / rust-lang/rust

`[T]::partial_sort_unstable()` performance on short prefixes

Open
#151,841 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-collections C-optimization T-libs
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:

  • u64
  • string: 10-byte String representation created from the u64
  • kibibyte: 1 KiB on the stack, where the first 8 bytes are the u64

Scenarios:

  • random: Uniform over full range
  • random_z1: Zipf with exponent 1
  • random_d20: Uniform over 0..20
  • random_p5: 5% of elements uniform over full range, 95% fixed middle value
  • random_p95: 95% of elements uniform over full range, 5% fixed middle value
  • ascending: 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.

Image Image Image Image Image Image Image

Code and raw data: https://github.com/niklasf/partial_sort_vendored_std

cc @sundy-li @tisonkun

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.