From 9dc0c4b9aa0c0eb556a51b48ce5d9130c501e363 Mon Sep 17 00:00:00 2001 From: Joe Date: Fri, 17 Oct 2025 13:13:11 -0700 Subject: [PATCH] back to atomics, no perf improvement --- benches/benchmarks.rs | 4 ++-- src/lib.rs | 55 ++++++++++++++++++++----------------------- src/main.rs | 44 +--------------------------------- 3 files changed, 29 insertions(+), 74 deletions(-) diff --git a/benches/benchmarks.rs b/benches/benchmarks.rs index b7bf739..6ff7834 100644 --- a/benches/benchmarks.rs +++ b/benches/benchmarks.rs @@ -29,7 +29,7 @@ fn new_index(c: &mut Criterion) { }); } -fn _update_positions(c: &mut Criterion) { +fn update_positions(c: &mut Criterion) { c.bench_function("update positions", move |b| { let points: Vec<_> = create_random_points(DEFAULT_NUM_POINTS, SEED_1) .into_iter() @@ -60,5 +60,5 @@ fn _update_positions(c: &mut Criterion) { }); } -criterion_group!(benches, new_index); +criterion_group!(benches, new_index, update_positions); criterion_main!(benches); diff --git a/src/lib.rs b/src/lib.rs index a1cd9d5..feb41f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,8 +40,8 @@ impl Ord for PVal { #[derive(Debug, Default, Clone)] pub struct Spindex2d { - xpoints: Arc>>, - ypoints: Arc>>, + xpoints: Arc>, + ypoints: Arc>, xs: Arc>>, ys: Arc>>, } @@ -64,13 +64,13 @@ impl Spindex2d { }; ys.push(y); - xpoints.push(i); - ypoints.push(i); + xpoints.push(i.into()); + ypoints.push(i.into()); } let index = Self { - xpoints: Arc::new(RwLock::new(xpoints)), - ypoints: Arc::new(RwLock::new(ypoints)), + xpoints: Arc::new(xpoints), + ypoints: Arc::new(ypoints), xs: Arc::new(RwLock::new(xs)), ys: Arc::new(RwLock::new(ys)), }; @@ -84,27 +84,25 @@ impl Spindex2d { /// when constructing this index. May panic if the updated points argument is longer than the /// original points. pub fn update(&self, points: &[Point2d]) { - let mut xs = self.xs.write().unwrap(); - let mut ys = self.ys.write().unwrap(); - let xpoints = self.xpoints.read().unwrap(); - let ypoints = self.ypoints.read().unwrap(); - for (i, point) in points.iter().enumerate() { - println!("hello: {i}"); - let xi = xpoints[i]; - let yi = ypoints[i]; - let x = PVal { - point: i, - val: point.x, - }; - xs[xi] = x; + { + let mut xs = self.xs.write().unwrap(); + let mut ys = self.ys.write().unwrap(); + for (i, point) in points.iter().enumerate() { + let xi = self.xpoints[i].load(Relaxed); + let yi = self.ypoints[i].load(Relaxed); + let x = PVal { + point: i, + val: point.x, + }; + xs[xi] = x; - let y = PVal { - point: i, - val: point.y, - }; - ys[yi] = y; + let y = PVal { + point: i, + val: point.y, + }; + ys[yi] = y; + } } - println!("done updating points"); self.sort(); } @@ -114,10 +112,9 @@ impl Spindex2d { let xhandle = thread::spawn(move || { let mut xs = xs.write().unwrap(); xs.sort_unstable(); - let mut points = points.write().unwrap(); for (i, x) in xs.iter().enumerate() { let p = x.point; - points[p] = i; + points[p].store(i, Relaxed); } }); @@ -126,10 +123,10 @@ impl Spindex2d { let yhandle = thread::spawn(move || { let mut ys = ys.write().unwrap(); ys.sort_unstable(); - let mut points = points.write().unwrap(); + for (i, y) in ys.iter().enumerate() { let p = y.point; - points[p] = i; + points[p].store(i, Relaxed); } }); diff --git a/src/main.rs b/src/main.rs index 5dc2e72..f328e4d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,43 +1 @@ -use rand::{Rng, SeedableRng}; -use rand_hc::Hc128Rng; - -use spacedog::*; - -fn create_random_points(num_points: usize, seed: &[u8; 32]) -> Vec<[f64; 2]> { - let mut rng = Hc128Rng::from_seed(*seed); - (0..num_points).map(|_| rng.random()).collect() -} - -const SEED_1: &[u8; 32] = b"Gv0aHMtHkBGsUXNspGU9fLRuCWkZWHZx"; -// const SEED_2: &[u8; 32] = b"km7DO4GeaFZfTcDXVpnO7ZJlgUY7hZiS"; - -const DEFAULT_NUM_POINTS: usize = 1_000_000; - -fn main() { - let points: Vec<_> = create_random_points(DEFAULT_NUM_POINTS, SEED_1) - .into_iter() - .map(|[x, y]| Point2d { x, y }) - .collect(); - - let mut rng = Hc128Rng::from_seed(*SEED_1); - - let jitters: Vec<_> = points - .iter() - .map(|p| { - let x = p.x + rng.random_range(-5.0..=5.0); - let y = p.y + rng.random_range(-5.0..=5.0); - Point2d { x, y } - }) - .collect(); - - let index = Spindex2d::new(&points); - - for round in 0..100 { - dbg!(round); - if round % 2 == 0 { - index.update(&jitters); - } else { - index.update(&points); - } - } -} +fn main() {}