nessex / rdst

A flexible, native Rust implementation of unstable radix sort.
Apache License 2.0
4 stars 1 forks source link

Panic when sorting a billion items #10

Closed vigna closed 10 months ago

vigna commented 10 months ago

The following program panics:


use rand::{rngs::SmallRng, Rng, SeedableRng};
use rdst::*;
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct SigVal {
    pub sig: [u64; 2],
    pub val: usize,
}

impl RadixKey for SigVal {
    const LEVELS: usize = 16;

    fn get_level(&self, level: usize) -> u8 {
        (self.sig[level / 8] >> level % 8) as u8
    }
}

fn main() {
    const N: usize = 1_000_000_000;
    let mut r = SmallRng::seed_from_u64(0);
    let mut v: Vec<_> = (0..N)
        .map(|i| SigVal {
            sig: [r.gen(), r.gen()],
            val: i,
        })
        .collect();
    v.radix_sort_unstable();
}

Result:

thread '<unnamed>' panicked at /Users/vigna/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rdst-0.20.11/src/sorts/out_of_place_sort.rs:207:19:
attempt to subtract with overflow
vigna commented 10 months ago

My mistake, the implementation of RadixKey was wrong (sometimes Copilot gets in the way).