Skip to content

Commit 6ffabf3

Browse files
committed
Auto merge of rust-lang#107638 - zhangyunhao116:pdqsort-rand, r=cuviper
Optimize break patterns Use `wyrand` instead of calling `XORSHIFT` 2 times in break patterns for the 64-bit platform. The new PRNG is 2x faster than the previous one. Bench result(via https://gist.github.com/zhangyunhao116/11ef41a150f5c23bb47d86255fbeba89): ``` old time: [1.3258 ns 1.3262 ns 1.3266 ns] change: [+0.5901% +0.6731% +0.7791%] (p = 0.00 < 0.05) Change within noise threshold. Found 13 outliers among 100 measurements (13.00%) 7 (7.00%) high mild 6 (6.00%) high severe new time: [657.65 ps 657.89 ps 658.18 ps] change: [-1.6910% -1.6110% -1.5256%] (p = 0.00 < 0.05) Performance has improved. Found 6 outliers among 100 measurements (6.00%) 2 (2.00%) high mild 4 (4.00%) high severe ```
2 parents f0bc76a + e107ca0 commit 6ffabf3

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

library/core/src/slice/sort.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -673,19 +673,23 @@ where
673673
fn break_patterns<T>(v: &mut [T]) {
674674
let len = v.len();
675675
if len >= 8 {
676-
// Pseudorandom number generator from the "Xorshift RNGs" paper by George Marsaglia.
677-
let mut random = len as u32;
678-
let mut gen_u32 = || {
679-
random ^= random << 13;
680-
random ^= random >> 17;
681-
random ^= random << 5;
682-
random
683-
};
676+
let mut seed = len;
684677
let mut gen_usize = || {
678+
// Pseudorandom number generator from the "Xorshift RNGs" paper by George Marsaglia.
685679
if usize::BITS <= 32 {
686-
gen_u32() as usize
680+
let mut r = seed as u32;
681+
r ^= r << 13;
682+
r ^= r >> 17;
683+
r ^= r << 5;
684+
seed = r as usize;
685+
seed
687686
} else {
688-
(((gen_u32() as u64) << 32) | (gen_u32() as u64)) as usize
687+
let mut r = seed as u64;
688+
r ^= r << 13;
689+
r ^= r >> 7;
690+
r ^= r << 17;
691+
seed = r as usize;
692+
seed
689693
}
690694
};
691695

0 commit comments

Comments
 (0)