about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-02-25 03:01:40 +0000
committerbors <bors@rust-lang.org>2023-02-25 03:01:40 +0000
commit6ffabf3c8f2ed81d0248c54365445f28bd9507e5 (patch)
treeddb8ffa096ea639edf748dad3b95925d10f26488
parentf0bc76ac41a0a832c9ee621e31aaf1f515d3d6a5 (diff)
parente107ca0f0b6dff35efdefc2c6bdaec75b13c1109 (diff)
downloadrust-6ffabf3c8f2ed81d0248c54365445f28bd9507e5.tar.gz
rust-6ffabf3c8f2ed81d0248c54365445f28bd9507e5.zip
Auto merge of #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
```
-rw-r--r--library/core/src/slice/sort.rs24
1 files changed, 14 insertions, 10 deletions
diff --git a/library/core/src/slice/sort.rs b/library/core/src/slice/sort.rs
index 4ca4eb86bde..7b8062c431e 100644
--- a/library/core/src/slice/sort.rs
+++ b/library/core/src/slice/sort.rs
@@ -673,19 +673,23 @@ where
 fn break_patterns<T>(v: &mut [T]) {
     let len = v.len();
     if len >= 8 {
-        // Pseudorandom number generator from the "Xorshift RNGs" paper by George Marsaglia.
-        let mut random = len as u32;
-        let mut gen_u32 = || {
-            random ^= random << 13;
-            random ^= random >> 17;
-            random ^= random << 5;
-            random
-        };
+        let mut seed = len;
         let mut gen_usize = || {
+            // Pseudorandom number generator from the "Xorshift RNGs" paper by George Marsaglia.
             if usize::BITS <= 32 {
-                gen_u32() as usize
+                let mut r = seed as u32;
+                r ^= r << 13;
+                r ^= r >> 17;
+                r ^= r << 5;
+                seed = r as usize;
+                seed
             } else {
-                (((gen_u32() as u64) << 32) | (gen_u32() as u64)) as usize
+                let mut r = seed as u64;
+                r ^= r << 13;
+                r ^= r >> 7;
+                r ^= r << 17;
+                seed = r as usize;
+                seed
             }
         };