rust/src/liballoc/tests
kennytm 42de36d4aa
Rollup merge of #48639 - varkor:sort_by_key-cached, r=bluss
Add slice::sort_by_cached_key as a memoised sort_by_key

At present, `slice::sort_by_key` calls its key function twice for each comparison that is made. When the key function is expensive (which can often be the case when `sort_by_key` is chosen over `sort_by`), this can lead to very suboptimal behaviour.

To address this, I've introduced a new slice method, `sort_by_cached_key`, which has identical semantic behaviour to `sort_by_key`, except that it guarantees the key function will only be called once per element.

Where there are `n` elements and the key function is `O(m)`:
- `slice::sort_by_cached_key` time complexity is `O(m n log m n)`, compared to `slice::sort_by_key`'s `O(m n + n log n)`.
- `slice::sort_by_cached_key` space complexity remains at `O(n + m)`. (Technically, it now reserves a slice of size `n`, whereas before it reserved a slice of size `n/2`.)

`slice::sort_unstable_by_key` has not been given an analogue, as it is important that unstable sorts are in-place, which is not a property that is guaranteed here. However, this also means that `slice::sort_unstable_by_key` is likely to be slower than `slice::sort_by_cached_key` when the key function does not have negligible complexity. We might want to explore this trade-off further in the future.

Benchmarks (for a vector of 100 `i32`s):
```
# Lexicographic: `|x| x.to_string()`
test bench_sort_by_key ... bench:      112,638 ns/iter (+/- 19,563)
test bench_sort_by_cached_key ... bench:       15,038 ns/iter (+/- 4,814)

# Identity: `|x| *x`
test bench_sort_by_key ... bench:        1,346 ns/iter (+/- 238)
test bench_sort_by_cached_key ... bench:        1,839 ns/iter (+/- 765)

# Power: `|x| x.pow(31)`
test bench_sort_by_key ... bench:        3,624 ns/iter (+/- 738)
test bench_sort_by_cached_key ... bench:        1,997 ns/iter (+/- 311)

# Abs: `|x| x.abs()`
test bench_sort_by_key ... bench:        1,546 ns/iter (+/- 174)
test bench_sort_by_cached_key ... bench:        1,668 ns/iter (+/- 790)
```
(So it seems functions that are single operations do perform slightly worse with this method, but for pretty much any more complex key, you're better off with this optimisation.)

I've definitely found myself using expensive keys in the past and wishing this optimisation was made (e.g. for https://github.com/rust-lang/rust/pull/47415). This feels like both desirable and expected behaviour, at the small cost of slightly more stack allocation and minute degradation in performance for extremely trivial keys.

Resolves #34447.
2018-03-27 10:47:44 +02:00
..
btree Add support for ..= syntax 2017-09-22 22:05:18 +02:00
binary_heap.rs Update Cargo submodule 2018-03-11 10:59:28 -07:00
cow_str.rs Add trailing newlines to files which have no trailing newlines. 2017-12-30 15:50:52 +08:00
fmt.rs Merge crate collections into alloc 2017-06-13 23:37:34 -07:00
heap.rs alloc_jemalloc: don’t assume MIN_ALIGN for small sizes 2017-11-20 16:22:17 +01:00
lib.rs Rollup merge of #48639 - varkor:sort_by_key-cached, r=bluss 2018-03-27 10:47:44 +02:00
linked_list.rs Implement LinkedList::drain_filter 2017-11-25 21:28:49 +01:00
slice.rs Rollup merge of #48639 - varkor:sort_by_key-cached, r=bluss 2018-03-27 10:47:44 +02:00
str.rs fix some typos 2017-11-21 15:33:45 +01:00
string.rs setting ABORTING_MALLOC for asmjs backend 2018-03-15 17:43:05 +00:00
vec.rs setting ABORTING_MALLOC for asmjs backend 2018-03-15 17:43:05 +00:00
vec_deque.rs setting ABORTING_MALLOC for asmjs backend 2018-03-15 17:43:05 +00:00