Skip to content

Commit c05676b

Browse files
committed
Add an in-place rotate method for slices to libcore
A helpful primitive for moving chunks of data around inside a slice. In particular, adding elements to the end of a Vec then moving them somewhere else, as a way to do efficient multiple-insert. (There's drain for efficient block-remove, but no easy way to block-insert.) Talk with another example: <https://youtu.be/qH6sSOr-yk8?t=560>
1 parent 0bd9e1f commit c05676b

File tree

12 files changed

+327
-0
lines changed

12 files changed

+327
-0
lines changed

src/doc/unstable-book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@
195195
- [sip_hash_13](library-features/sip-hash-13.md)
196196
- [slice_concat_ext](library-features/slice-concat-ext.md)
197197
- [slice_get_slice](library-features/slice-get-slice.md)
198+
- [slice_rotate](library-features/slice-rotate.md)
198199
- [slice_rsplit](library-features/slice-rsplit.md)
199200
- [sort_internals](library-features/sort-internals.md)
200201
- [sort_unstable](library-features/sort-unstable.md)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# `slice_rotate`
2+
3+
The tracking issue for this feature is: [#123456789]
4+
5+
[#123456789]: https://github.com/rust-lang/rust/issues/123456789
6+
7+
------------------------

src/libcollections/benches/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![feature(i128_type)]
1414
#![feature(rand)]
1515
#![feature(repr_simd)]
16+
#![feature(slice_rotate)]
1617
#![feature(sort_unstable)]
1718
#![feature(test)]
1819

src/libcollections/benches/slice.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ fn gen_random(len: usize) -> Vec<u64> {
195195
rng.gen_iter::<u64>().take(len).collect()
196196
}
197197

198+
fn gen_random_bytes(len: usize) -> Vec<u8> {
199+
let mut rng = thread_rng();
200+
rng.gen_iter::<u8>().take(len).collect()
201+
}
202+
198203
fn gen_mostly_ascending(len: usize) -> Vec<u64> {
199204
let mut rng = thread_rng();
200205
let mut v = gen_ascending(len);
@@ -315,3 +320,39 @@ reverse!(reverse_u64, u64, |x| x as u64);
315320
reverse!(reverse_u128, u128, |x| x as u128);
316321
#[repr(simd)] struct F64x4(f64, f64, f64, f64);
317322
reverse!(reverse_simd_f64x4, F64x4, |x| { let x = x as f64; F64x4(x,x,x,x) });
323+
324+
macro_rules! rotate {
325+
($name:ident, $gen:expr, $len:expr, $mid:expr) => {
326+
#[bench]
327+
fn $name(b: &mut Bencher) {
328+
let size = mem::size_of_val(&$gen(1)[0]);
329+
let mut v = $gen($len * 8 / size);
330+
b.iter(|| black_box(&mut v).rotate(($mid*8+size-1)/size));
331+
b.bytes = (v.len() * size) as u64;
332+
}
333+
}
334+
}
335+
336+
rotate!(rotate_tiny_by1, gen_random, 16, 1);
337+
rotate!(rotate_tiny_half, gen_random, 16, 16/2);
338+
rotate!(rotate_tiny_half_plus_one, gen_random, 16, 16/2+1);
339+
340+
rotate!(rotate_medium_by1, gen_random, 9158, 1);
341+
rotate!(rotate_medium_by727_u64, gen_random, 9158, 727);
342+
rotate!(rotate_medium_by727_bytes, gen_random_bytes, 9158, 727);
343+
rotate!(rotate_medium_by727_strings, gen_strings, 9158, 727);
344+
rotate!(rotate_medium_half, gen_random, 9158, 9158/2);
345+
rotate!(rotate_medium_half_plus_one, gen_random, 9158, 9158/2+1);
346+
347+
// Intended to use more RAM than the machine has cache
348+
rotate!(rotate_huge_by1, gen_random, 5*1024*1024, 1);
349+
rotate!(rotate_huge_by9199_u64, gen_random, 5*1024*1024, 9199);
350+
rotate!(rotate_huge_by9199_bytes, gen_random_bytes, 5*1024*1024, 9199);
351+
rotate!(rotate_huge_by9199_strings, gen_strings, 5*1024*1024, 9199);
352+
rotate!(rotate_huge_by9199_big, gen_big_random, 5*1024*1024, 9199);
353+
rotate!(rotate_huge_by1234577_u64, gen_random, 5*1024*1024, 1234577);
354+
rotate!(rotate_huge_by1234577_bytes, gen_random_bytes, 5*1024*1024, 1234577);
355+
rotate!(rotate_huge_by1234577_strings, gen_strings, 5*1024*1024, 1234577);
356+
rotate!(rotate_huge_by1234577_big, gen_big_random, 5*1024*1024, 1234577);
357+
rotate!(rotate_huge_half, gen_random, 5*1024*1024, 5*1024*1024/2);
358+
rotate!(rotate_huge_half_plus_one, gen_random, 5*1024*1024, 5*1024*1024/2+1);

src/libcollections/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#![feature(shared)]
5656
#![feature(slice_get_slice)]
5757
#![feature(slice_patterns)]
58+
#![cfg_attr(not(test), feature(slice_rotate))]
5859
#![feature(slice_rsplit)]
5960
#![cfg_attr(not(test), feature(sort_unstable))]
6061
#![feature(specialization)]

src/libcollections/slice.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,58 @@ impl<T> [T] {
13371337
core_slice::SliceExt::sort_unstable_by_key(self, f);
13381338
}
13391339

1340+
/// Permutes the slice in-place such that `self[mid..]` move to the
1341+
/// beginning of the slice while `self[..mid]` move to the end of the
1342+
/// slice. Equivalently, rotates the slice `mid` places to the left
1343+
/// or `k = self.len() - mid` places to the right.
1344+
///
1345+
/// Rotation by `mid` and rotation by `k` are inverse operations.
1346+
/// The method returns `k`, which is also the new location of
1347+
/// the formerly-first element.
1348+
///
1349+
/// This is a "k-rotation", a permutation in which item `i` moves to
1350+
/// position `i + k`, modulo the length of the slice. See _Elements
1351+
/// of Programming_ [§10.4][eop].
1352+
///
1353+
/// [eop]: https://books.google.com/books?id=CO9ULZGINlsC&pg=PA178&q=k-rotation
1354+
///
1355+
/// # Panics
1356+
///
1357+
/// This function will panic if `mid` is greater than the length of the
1358+
/// slice. (Note that `mid == self.len()` does _not_ panic; it's a nop
1359+
/// rotation with `k == 0`, the inverse of a rotation with `mid == 0`.)
1360+
///
1361+
/// # Complexity
1362+
///
1363+
/// Takes linear (in `self.len()`) time.
1364+
///
1365+
/// # Examples
1366+
///
1367+
/// ```
1368+
/// #![feature(slice_rotate)]
1369+
///
1370+
/// let mut a = [1, 2, 3, 4, 5, 6, 7];
1371+
/// let k = a.rotate(2);
1372+
/// assert_eq!(&a, &[3, 4, 5, 6, 7, 1, 2]);
1373+
/// a.rotate(k);
1374+
/// assert_eq!(&a, &[1, 2, 3, 4, 5, 6, 7]);
1375+
///
1376+
/// fn extend_at<T, I>(v: &mut Vec<T>, index: usize, iter: I)
1377+
/// where I: Iterator<Item=T>
1378+
/// {
1379+
/// let mid = v.len() - index;
1380+
/// v.extend(iter);
1381+
/// v[index..].rotate(mid);
1382+
/// }
1383+
/// let mut v = (0..10).collect();
1384+
/// extend_at(&mut v, 7, 100..104);
1385+
/// assert_eq!(&v, &[0, 1, 2, 3, 4, 5, 6, 100, 101, 102, 103, 7, 8, 9]);
1386+
/// ```
1387+
#[unstable(feature = "slice_rotate", issue = "123456789")]
1388+
pub fn rotate(&mut self, mid: usize) -> usize {
1389+
core_slice::SliceExt::rotate(self, mid)
1390+
}
1391+
13401392
/// Copies the elements from `src` into `self`.
13411393
///
13421394
/// The length of `src` must be the same as `self`.

src/libcollections/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#![feature(pattern)]
2121
#![feature(placement_in_syntax)]
2222
#![feature(rand)]
23+
#![feature(slice_rotate)]
2324
#![feature(splice)]
2425
#![feature(step_by)]
2526
#![feature(str_escape)]

src/libcollections/tests/slice.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,42 @@ fn test_sort_stability() {
466466
}
467467
}
468468

469+
#[test]
470+
fn test_rotate() {
471+
let expected: Vec<_> = (0..13).collect();
472+
let mut v = Vec::new();
473+
474+
// no-ops
475+
v.clone_from(&expected);
476+
v.rotate(0);
477+
assert_eq!(v, expected);
478+
v.rotate(expected.len());
479+
assert_eq!(v, expected);
480+
let mut zst_array = [(), (), ()];
481+
zst_array.rotate(2);
482+
483+
// happy path
484+
v = (5..13).chain(0..5).collect();
485+
let k = v.rotate(8);
486+
assert_eq!(v, expected);
487+
assert_eq!(k, 5);
488+
489+
let expected: Vec<_> = (0..1000).collect();
490+
491+
// small rotations in large slice, uses ptr::copy
492+
v = (2..1000).chain(0..2).collect();
493+
v.rotate(998);
494+
assert_eq!(v, expected);
495+
v = (998..1000).chain(0..998).collect();
496+
v.rotate(2);
497+
assert_eq!(v, expected);
498+
499+
// non-small prime rotation, has a few rounds of swapping
500+
v = (389..1000).chain(0..389).collect();
501+
v.rotate(1000-389);
502+
assert_eq!(v, expected);
503+
}
504+
469505
#[test]
470506
fn test_concat() {
471507
let v: [Vec<i32>; 0] = [];

src/libcore/slice/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use mem;
5151
use marker::{Copy, Send, Sync, Sized, self};
5252
use iter_private::TrustedRandomAccess;
5353

54+
mod rotate;
5455
mod sort;
5556

5657
#[repr(C)]
@@ -202,6 +203,9 @@ pub trait SliceExt {
202203
#[stable(feature = "core", since = "1.6.0")]
203204
fn ends_with(&self, needle: &[Self::Item]) -> bool where Self::Item: PartialEq;
204205

206+
#[unstable(feature = "slice_rotate", issue = "123456789")]
207+
fn rotate(&mut self, mid: usize) -> usize;
208+
205209
#[stable(feature = "clone_from_slice", since = "1.7.0")]
206210
fn clone_from_slice(&mut self, src: &[Self::Item]) where Self::Item: Clone;
207211

@@ -635,6 +639,18 @@ impl<T> SliceExt for [T] {
635639
self.binary_search_by(|p| p.borrow().cmp(x))
636640
}
637641

642+
fn rotate(&mut self, mid: usize) -> usize {
643+
assert!(mid <= self.len());
644+
let k = self.len() - mid;
645+
646+
unsafe {
647+
let p = self.as_mut_ptr();
648+
rotate::ptr_rotate(mid, p.offset(mid as isize), k);
649+
}
650+
651+
k
652+
}
653+
638654
#[inline]
639655
fn clone_from_slice(&mut self, src: &[T]) where T: Clone {
640656
assert!(self.len() == src.len(),

src/libcore/slice/rotate.rs

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// Copyright 2012-2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use cmp;
12+
use mem;
13+
use ptr;
14+
15+
/// Rotation is much faster if it has access to a little bit of memory. This
16+
/// union provides a RawVec-like interface, but to a fixed-size stack buffer.
17+
#[allow(unions_with_drop_fields)]
18+
union RawArray<T> {
19+
/// Ensure this is appropriately aligned for T, and is big
20+
/// enough for two elements even if T is enormous.
21+
typed: [T; 2],
22+
/// For normally-sized types, especially things like u8, having more
23+
/// than 2 in the buffer is necessary for usefulness, so pad it out
24+
/// enough to be helpful, but not so big as to risk overflow.
25+
_extra: [usize; 32],
26+
}
27+
28+
impl<T> RawArray<T> {
29+
fn new() -> Self {
30+
unsafe { mem::uninitialized() }
31+
}
32+
fn ptr(&self) -> *mut T {
33+
unsafe { &self.typed as *const T as *mut T }
34+
}
35+
fn cap() -> usize {
36+
if mem::size_of::<T>() == 0 {
37+
usize::max_value()
38+
} else {
39+
mem::size_of::<Self>() / mem::size_of::<T>()
40+
}
41+
}
42+
}
43+
44+
/// Rotates the range `[mid-left, mid+right)` such that the element at `mid`
45+
/// becomes the first element. Equivalently, rotates the range `left`
46+
/// elements to the left or `right` elements to the right.
47+
///
48+
/// # Safety
49+
///
50+
/// The specified range must be valid for reading and writing.
51+
/// The type `T` must have non-zero size.
52+
///
53+
/// # Algorithm
54+
///
55+
/// For longer rotations, swap the left-most `delta = min(left, right)`
56+
/// elements with the right-most `delta` elements. LLVM vectorizes this,
57+
/// which is profitable as we only reach this step for a "large enough"
58+
/// rotation. Doing this puts `delta` elements on the larger side into the
59+
/// correct position, leaving a smaller rotate problem. Demonstration:
60+
///
61+
/// ```text
62+
/// [ 6 7 8 9 10 11 12 13 . 1 2 3 4 5 ]
63+
/// 1 2 3 4 5 [ 11 12 13 . 6 7 8 9 10 ]
64+
/// 1 2 3 4 5 [ 8 9 10 . 6 7 ] 11 12 13
65+
/// 1 2 3 4 5 6 7 [ 10 . 8 9 ] 11 12 13
66+
/// 1 2 3 4 5 6 7 [ 9 . 8 ] 10 11 12 13
67+
/// 1 2 3 4 5 6 7 8 [ . ] 9 10 11 12 13
68+
/// ```
69+
///
70+
/// Once the rotation is small enough, copy some elements into a stack
71+
/// buffer, `memmove` the others, and move the ones back from the buffer.
72+
pub unsafe fn ptr_rotate<T>(mut left: usize, mid: *mut T, mut right: usize) {
73+
loop {
74+
let delta = cmp::min(left, right);
75+
if delta <= RawArray::<T>::cap() {
76+
break;
77+
}
78+
79+
ptr_swap_n(
80+
mid.offset(-(left as isize)),
81+
mid.offset((right-delta) as isize),
82+
delta);
83+
84+
if left <= right {
85+
right -= delta;
86+
} else {
87+
left -= delta;
88+
}
89+
}
90+
91+
let rawarray = RawArray::new();
92+
let buf = rawarray.ptr();
93+
94+
let dim = mid.offset(-(left as isize)).offset(right as isize);
95+
if left <= right {
96+
ptr::copy_nonoverlapping(mid.offset(-(left as isize)), buf, left);
97+
ptr::copy(mid, mid.offset(-(left as isize)), right);
98+
ptr::copy_nonoverlapping(buf, dim, left);
99+
}
100+
else {
101+
ptr::copy_nonoverlapping(mid, buf, right);
102+
ptr::copy(mid.offset(-(left as isize)), dim, left);
103+
ptr::copy_nonoverlapping(buf, mid.offset(-(left as isize)), right);
104+
}
105+
}
106+
107+
unsafe fn ptr_swap_u8(a: *mut u8, b: *mut u8, n: usize) {
108+
for i in 0..n {
109+
ptr::swap(a.offset(i as isize), b.offset(i as isize));
110+
}
111+
}
112+
unsafe fn ptr_swap_u16(a: *mut u16, b: *mut u16, n: usize) {
113+
for i in 0..n {
114+
ptr::swap(a.offset(i as isize), b.offset(i as isize));
115+
}
116+
}
117+
unsafe fn ptr_swap_u32(a: *mut u32, b: *mut u32, n: usize) {
118+
for i in 0..n {
119+
ptr::swap(a.offset(i as isize), b.offset(i as isize));
120+
}
121+
}
122+
unsafe fn ptr_swap_u64(a: *mut u64, b: *mut u64, n: usize) {
123+
for i in 0..n {
124+
ptr::swap(a.offset(i as isize), b.offset(i as isize));
125+
}
126+
}
127+
128+
unsafe fn ptr_swap_n<T>(a: *mut T, b: *mut T, n: usize) {
129+
// Doing this as a generic is 16% & 40% slower in two of the `String`
130+
// benchmarks, as (based on the block names) LLVM doesn't vectorize it.
131+
// Since this is just operating on raw memory, dispatch to a version
132+
// with appropriate alignment. Helps with code size as well, by
133+
// avoiding monomorphizing different unrolled loops for `i32`,
134+
// `u32`, `f32`, `[u32; 1]`, etc.
135+
let size_of_t = mem::size_of::<T>();
136+
let align_of_t = mem::align_of::<T>();
137+
138+
let a64 = mem::align_of::<u64>();
139+
if a64 == 8 && align_of_t % a64 == 0 {
140+
return ptr_swap_u64(a as *mut u64, b as *mut u64, n * (size_of_t / 8));
141+
}
142+
143+
let a32 = mem::align_of::<u32>();
144+
if a32 == 4 && align_of_t % a32 == 0 {
145+
return ptr_swap_u32(a as *mut u32, b as *mut u32, n * (size_of_t / 4));
146+
}
147+
148+
let a16 = mem::align_of::<u16>();
149+
if a16 == 2 && align_of_t % a16 == 0 {
150+
return ptr_swap_u16(a as *mut u16, b as *mut u16, n * (size_of_t / 2));
151+
}
152+
153+
ptr_swap_u8(a as *mut u8, b as *mut u8, n * size_of_t);
154+
}

src/libcore/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#![feature(raw)]
3030
#![feature(sip_hash_13)]
3131
#![feature(slice_patterns)]
32+
#![feature(slice_rotate)]
3233
#![feature(sort_internals)]
3334
#![feature(sort_unstable)]
3435
#![feature(step_by)]

0 commit comments

Comments
 (0)