Skip to content

Commit f05e6bf

Browse files
committed
Rollup merge of rust-lang#58074 - scottmcm:stabilize-sort_by_cached_key, r=SimonSapin
Stabilize slice_sort_by_cached_key I was going to ask on the tracking issue (rust-lang#34447), but decided to just send this and hope for an FCP here. The method was added last March by rust-lang#48639. Signature: https://doc.rust-lang.org/std/primitive.slice.html#method.sort_by_cached_key ```rust impl [T] { pub fn sort_by_cached_key<K, F>(&mut self, f: F) where F: FnMut(&T) -> K, K: Ord; } ``` That's an identical signature to the existing `sort_by_key`, so I think the questions are just naming, implementation, and the usual "do we want this?". The implementation seems to have proven its use in rustc at least, which many uses: https://github.com/rust-lang/rust/search?l=Rust&q=sort_by_cached_key (I'm asking because it's exactly what I just needed the other day: ```rust all_positions.sort_by_cached_key(|&n| data::CITIES.iter() .map(|x| *metric_closure.get_edge(n, x.pos).unwrap()) .sum::<usize>() ); ``` since caching that key is a pretty obviously good idea.) Closes rust-lang#34447
2 parents 84e88da + 317f153 commit f05e6bf

File tree

14 files changed

+9
-14
lines changed

14 files changed

+9
-14
lines changed

src/liballoc/benches/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![feature(repr_simd)]
2-
#![feature(slice_sort_by_cached_key)]
32
#![feature(test)]
43

54
extern crate rand;

src/liballoc/slice.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ impl<T> [T] {
257257
/// This sort is stable (i.e., does not reorder equal elements) and `O(m n log(m n))`
258258
/// worst-case, where the key function is `O(m)`.
259259
///
260+
/// For expensive key functions (e.g. functions that are not simple property accesses or
261+
/// basic operations), [`sort_by_cached_key`](#method.sort_by_cached_key) is likely to be
262+
/// significantly faster, as it does not recompute element keys.
263+
///
260264
/// When applicable, unstable sorting is preferred because it is generally faster than stable
261265
/// sorting and it doesn't allocate auxiliary memory.
262266
/// See [`sort_unstable_by_key`](#method.sort_unstable_by_key).
@@ -312,15 +316,14 @@ impl<T> [T] {
312316
/// # Examples
313317
///
314318
/// ```
315-
/// #![feature(slice_sort_by_cached_key)]
316319
/// let mut v = [-5i32, 4, 32, -3, 2];
317320
///
318321
/// v.sort_by_cached_key(|k| k.to_string());
319322
/// assert!(v == [-3, -5, 2, 32, 4]);
320323
/// ```
321324
///
322325
/// [pdqsort]: https://github.com/orlp/pdqsort
323-
#[unstable(feature = "slice_sort_by_cached_key", issue = "34447")]
326+
#[stable(feature = "slice_sort_by_cached_key", since = "1.34.0")]
324327
#[inline]
325328
pub fn sort_by_cached_key<K, F>(&mut self, f: F)
326329
where F: FnMut(&T) -> K, K: Ord

src/liballoc/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#![feature(exact_size_is_empty)]
55
#![feature(pattern)]
66
#![feature(repeat_generic_slice)]
7-
#![feature(slice_sort_by_cached_key)]
87
#![feature(try_reserve)]
98
#![feature(unboxed_closures)]
109
#![feature(vecdeque_rotate)]

src/libcore/slice/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,10 @@ impl<T> [T] {
15631563
/// randomization to avoid degenerate cases, but with a fixed seed to always provide
15641564
/// deterministic behavior.
15651565
///
1566+
/// Due to its key calling strategy, [`sort_unstable_by_key`](#method.sort_unstable_by_key)
1567+
/// is likely to be slower than [`sort_by_cached_key`](#method.sort_by_cached_key) in
1568+
/// cases where the key function is expensive.
1569+
///
15661570
/// # Examples
15671571
///
15681572
/// ```

src/librustc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
#![feature(rustc_diagnostic_macros)]
4848
#![feature(rustc_attrs)]
4949
#![feature(slice_patterns)]
50-
#![feature(slice_sort_by_cached_key)]
5150
#![feature(specialization)]
5251
#![feature(unboxed_closures)]
5352
#![feature(thread_local)]

src/librustc_codegen_llvm/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#![feature(nll)]
1818
#![feature(range_contains)]
1919
#![feature(rustc_diagnostic_macros)]
20-
#![feature(slice_sort_by_cached_key)]
2120
#![feature(optin_builtin_traits)]
2221
#![feature(concat_idents)]
2322
#![feature(link_args)]

src/librustc_codegen_ssa/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#![feature(libc)]
77
#![feature(rustc_diagnostic_macros)]
88
#![feature(in_band_lifetimes)]
9-
#![feature(slice_sort_by_cached_key)]
109
#![feature(nll)]
1110
#![allow(unused_attributes)]
1211
#![allow(dead_code)]

src/librustc_driver/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#![cfg_attr(unix, feature(libc))]
1111
#![feature(nll)]
1212
#![feature(rustc_diagnostic_macros)]
13-
#![feature(slice_sort_by_cached_key)]
1413
#![feature(set_stdio)]
1514
#![feature(no_debug)]
1615
#![feature(integer_atomics)]

src/librustc_metadata/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#![feature(proc_macro_internals)]
77
#![feature(proc_macro_quote)]
88
#![feature(rustc_diagnostic_macros)]
9-
#![feature(slice_sort_by_cached_key)]
109
#![feature(crate_visibility_modifier)]
1110
#![feature(specialization)]
1211
#![feature(rustc_private)]

src/librustc_mir/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
77
#![feature(nll)]
88
#![feature(in_band_lifetimes)]
99
#![feature(slice_patterns)]
10-
#![feature(slice_sort_by_cached_key)]
1110
#![feature(box_patterns)]
1211
#![feature(box_syntax)]
1312
#![feature(crate_visibility_modifier)]

src/librustc_resolve/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#![feature(label_break_value)]
55
#![feature(nll)]
66
#![feature(rustc_diagnostic_macros)]
7-
#![feature(slice_sort_by_cached_key)]
87

98
#![recursion_limit="256"]
109

src/librustc_typeck/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ This API is completely unstable and subject to change.
6767
#![feature(refcell_replace_swap)]
6868
#![feature(rustc_diagnostic_macros)]
6969
#![feature(slice_patterns)]
70-
#![feature(slice_sort_by_cached_key)]
7170
#![feature(never_type)]
7271

7372
#![recursion_limit="256"]

src/librustdoc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#![feature(box_syntax)]
88
#![feature(nll)]
99
#![feature(set_stdio)]
10-
#![feature(slice_sort_by_cached_key)]
1110
#![feature(test)]
1211
#![feature(vec_remove_item)]
1312
#![feature(ptr_offset_from)]

src/libsyntax/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#![feature(nll)]
1515
#![feature(rustc_attrs)]
1616
#![feature(rustc_diagnostic_macros)]
17-
#![feature(slice_sort_by_cached_key)]
1817
#![feature(step_trait)]
1918
#![feature(try_trait)]
2019
#![feature(unicode_internals)]

0 commit comments

Comments
 (0)